-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.sh
253 lines (208 loc) · 5.16 KB
/
lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/bin/bash
##
# Determine if a program is in path, without any output.
#
# @uses which
# @param string executable name
# @returns success code
#
function program_exists() {
local program="$1"
[ -n "${program}" ] && which "${program}" >/dev/null
}
##
# Determine if a mac Application exists
#
# @uses program_exists
# @uses mdfind
# @param string bundle name
# @returns success code
#
function mac_app_exists() {
local bundle="$1"
program_exists "mdfind" && [ -n "$(mdfind "kMDItemCFBundleIdentifier == '${bundle}'")" ]
}
##
# Determine if a brew is installed.
#
# @uses brew
# @param string brew name
# @returns success code
#
function brew_installed() {
local brew="${1}"
brew list -1 | grep -q "^${brew}$"
}
##
# Convert to fully qualified path, expanding ../, symlinks, etc.
# Readlink of Mac is deficient, use ruby instead.
#
# @uses ruby
# @param string any path (can be . and such)
# @outputs absolute path
#
function realpath() {
local path="$1"
echo "require 'pathname'; puts Pathname.new('${path}').realpath" | ruby 2>/dev/null
}
##
# Prompt the user with a question, supporting default value
#
# @uses read
# @param string question
# @param string optional default value
# @outputs answer or default value
#
function prompt_value() {
local prompt="${1} "
local default="${2}"
[[ -n "${default}" ]] && prompt="${prompt}(${default}) "
read -p "${prompt}" reply
[[ -z "${reply}" ]] && reply="${default}"
if [[ -z "${reply}" ]]; then
prompt_value "${1}" "${2}"
else
echo "${reply}"
fi
}
##
# Prompt the user to confirm, default is yes
#
# @uses read
# @param string question
# @returns 0 if yes, 1 if no
#
function confirm() {
local prompt="${1}"
read -p "${prompt} (Y/n) " reply
[[ "${reply}" != "n" ]]
}
function get_config_var() {
local var_name="${1}"
local config_dir="${DOTFILES_CONFIG_DIR}/vars"
local file="${config_dir}/${var_name}"
if [[ -s "${file}" ]]; then
cat "${file}"
fi
}
function set_config_var() {
local var_name="${1}"
local value="${2}"
local config_dir="${DOTFILES_CONFIG_DIR}/vars"
local file="${config_dir}/${var_name}"
[[ -d "${config_dir}" ]] || mkdir -p "${config_dir}"
echo "${value}" > "${file}"
}
function ensure_config_var() {
local var="${1}"
local message="${2:-$var}"
local default="${3:-}"
value="$(get_config_var "${var}")"
if [[ -z "${value}" ]]; then
value="$(prompt_value "${message}" "${default}")"
set_config_var "${var}" "${value}"
fi
}
##
# Escape custom characters in a string
# WARNING: if \ is escaped, it must be the first
# Example: escape "ab'\c" '\' "'" ===> ab\'\\c
#
# @param string The string to be escaped
# @param char* All the characters to espace, multiple chars are specified as multiple params.
# @outputs Escaped string
#
function escape_chars() {
local content="${1}"
shift
for char in $@; do
if [ "${char}" = '$' -o "${char}" = '\' ]; then
char="\\${char}"
fi
content="$(echo "${content}" | sed -e "s/${char}/\\\\${char}/g")"
done
echo "${content}"
}
##
# Wrap a string with single quotes
# Single quotes inside it will be escaped properly
#
function wrap_single_quote() {
local content="${1}"
echo "'$(echo "${content}" | sed "s/'/'\"'\"'/g")'"
}
function echo_export_single_quote() {
local var="${1}"
local content="${2}"
local quoted="$(wrap_single_quote "${content}")"
echo "export ${var}=${quoted}"
}
function echo_export_double_quote() {
local var="${1}"
local content="${2}"
local escaped="$(escape_chars '\' '"')"
echo "export ${var}=\"${escaped}\""
}
##
# Sort all files by their filename, independently of their folder
# Example: ls -1 /a/* /b/* | sort_by_filename
#
# @input One path per line
# @outputs One path per line
function sort_by_filename() {
while read filename; do
echo "${filename}" | sed -E 's/^(.+)\/([^/]+)$/\2#\1\/\2/';
done | sort -n | cut -d '#' -f 2
}
##
# Get files, sorted by filename, of all folders
#
# @param string folder*
# @output One path per line
function get_sorted_files() {
local files=""
for folder in $@; do
if [[ -d "${folder}" ]]; then
files="${files}
$(ls -1 ${folder}/*)"
fi
done
echo "${files}" | sort_by_filename
}
##
# cat a file. If it ends with .sh, execute it instead
#
# @param string file path
# @outputs content
#
function cat_or_exec() {
local file="${1}"
if echo "${file}" | grep -q '\.sh$'; then
. "${file}"
else
cat "${file}"
fi
}
##
# Check if directory contains executable files
#
# @link http://stackoverflow.com/questions/4458120/unix-find-search-for-executable-files
# @uses find
# @returns 0 if contains, 1 if empty
#
function dir_has_executables() {
local dir="${1}"
[ -n "$(find -L "${dir}" -mindepth 1 -maxdepth 1 -perm -a=x -type f)" ]
}
##
# Print all passed arguments in reverse
#
# @param string* All the arguments to be printed
# @outputs One argument per line
#
function reverse_args() {
local items=($@)
for (( idx=${#items[@]}-1 ; idx>=0 ; idx-- )) ; do
echo "${items[idx]}"
done
}