1
|
#!/bin/bash -e
|
2
|
realpath () { readlink -f -- "$1"; }
|
3
|
|
4
|
include_guard_var () { realpath "$1"|sed 's/[^a-zA-Z0-9_]/_/g'; }
|
5
|
|
6
|
self_not_included () # usage: if self_not_included; then ... fi
|
7
|
{
|
8
|
test "$#" -ge 1 || set -- "${BASH_SOURCE[1]}"
|
9
|
local include_guard="$(include_guard_var "$1")"
|
10
|
alias self_being_included=false
|
11
|
test -z "${!include_guard+t}" && \
|
12
|
{ eval "$include_guard"=1; alias self_being_included=true; }
|
13
|
}
|
14
|
|
15
|
# to load newly-defined aliases for use in functions in the same file:
|
16
|
## fi # load new aliases
|
17
|
## if self_being_included; then
|
18
|
# this is needed because aliases defined inside an if statement are not
|
19
|
# available inside that if statement
|
20
|
|
21
|
if self_not_included "${BASH_SOURCE[0]}"; then
|
22
|
|
23
|
shopt -s expand_aliases
|
24
|
|
25
|
unalias () { builtin unalias "$@" 2>&- || true; } # no error if undefined
|
26
|
|
27
|
function extern () { (unset -f "$1"; "$@") || exit; }
|
28
|
|
29
|
#### exceptions
|
30
|
|
31
|
# usage: try cmd...; ignore status; if catch status; then ...; fi; end_try
|
32
|
|
33
|
try_ () { { "$@"; e="$?";} || true; }
|
34
|
alias try='declare e; try_ ' # trailing space alias-expands next word
|
35
|
|
36
|
catch () { test "$e" -eq "$1"; e=0; }
|
37
|
|
38
|
ignore () { catch "$@" || true; }
|
39
|
|
40
|
alias end_try='return "$e"'
|
41
|
alias end_try_subshell='exit "$e"'
|
42
|
|
43
|
#### integers
|
44
|
|
45
|
let () { builtin let "$@" || true; }
|
46
|
# "If the last ARG evaluates to 0, let returns 1" (`help let`)
|
47
|
|
48
|
#### arrays
|
49
|
|
50
|
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
|
51
|
|
52
|
reverse () # usage: array=($(reverse args...))
|
53
|
{
|
54
|
local i
|
55
|
for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
|
56
|
}
|
57
|
|
58
|
#### verbose output
|
59
|
|
60
|
: "${verbosity:=$verbose}" "${verbosity:=0}"
|
61
|
|
62
|
can_log () { test "$verbosity" -gt 0; } # verbosity=0 turns off all logging
|
63
|
|
64
|
# usage: in func: inc_log_level; ...
|
65
|
# outside func: inc_log_level; ...; dec_log_level
|
66
|
alias inc_log_level='declare verbosity="$verbosity"; let verbosity--'
|
67
|
alias dec_log_level='declare verbosity="$verbosity"; let verbosity++'
|
68
|
|
69
|
# usage: (log_stderr; cmd...) || exit
|
70
|
# `|| exit` needed on Mac because of bug where -e doesn't apply to ()
|
71
|
log_stderr () { if ! can_log; then exec 2>/dev/null; fi; }
|
72
|
|
73
|
echo_cmd () { if can_log; then echo "$PS4$*" >&2; fi; }
|
74
|
|
75
|
echo_run () { echo_cmd "$@"; "$@"; }
|
76
|
|
77
|
echo_run_extern () { (log_stderr; echo_run extern "$@") || exit; }
|
78
|
|
79
|
alias extern="echo_run_extern" # automatically echo external commands
|
80
|
|
81
|
if test "$verbosity" -ge 1; then
|
82
|
alias env="echo_run env" # automatically echo commands that use env
|
83
|
fi
|
84
|
|
85
|
canon_rel_path ()
|
86
|
{
|
87
|
local path="$1"
|
88
|
path="$(realpath "$path")" # canonicalize
|
89
|
path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
|
90
|
echo "$path"
|
91
|
}
|
92
|
|
93
|
fi # allow unalias to take effect
|
94
|
unalias echo_func
|
95
|
if self_being_included; then
|
96
|
|
97
|
echo_func ()
|
98
|
{
|
99
|
inc_log_level
|
100
|
local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
|
101
|
echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
|
102
|
}
|
103
|
|
104
|
fi # always restore the alias
|
105
|
alias echo_func='"echo_func" "$@"' # usage: func () { echo_func; ...; }
|
106
|
if self_being_included; then
|
107
|
|
108
|
echo_stdin () # usage: input|echo_stdin|cmd
|
109
|
{
|
110
|
inc_log_level
|
111
|
if can_log; then
|
112
|
echo ----- >&2
|
113
|
tee -a /dev/stderr;
|
114
|
echo ----- >&2
|
115
|
else cat
|
116
|
fi
|
117
|
}
|
118
|
|
119
|
echo_vars () # usage: echo_vars var...
|
120
|
{
|
121
|
inc_log_level
|
122
|
if can_log; then { echo -n "$PS4"; declare -p "${@%%=*}";} >&2; fi
|
123
|
}
|
124
|
|
125
|
echo_export ()
|
126
|
{
|
127
|
builtin export "$@"
|
128
|
echo_vars "$@"
|
129
|
}
|
130
|
|
131
|
if test "$verbosity" -ge 2; then
|
132
|
alias export="echo_export" # automatically echo env vars when they are set
|
133
|
fi
|
134
|
|
135
|
usage () { echo "Usage: $1" >&2; (exit 2); }
|
136
|
|
137
|
fi # load new aliases
|
138
|
if self_being_included; then
|
139
|
|
140
|
#### strings
|
141
|
|
142
|
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
|
143
|
|
144
|
sed () { env sed -"$sed_ere_flag" "$@";}
|
145
|
|
146
|
#### vars
|
147
|
|
148
|
set_var () { eval "$1"'="$2"'; }
|
149
|
|
150
|
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
|
151
|
|
152
|
# usage: local var=...; local_inv
|
153
|
alias local_inv='declare "no_$var=$(test -n "${!var}" || echo 1)"'
|
154
|
|
155
|
# usage: local prefix=..._; import_vars
|
156
|
alias import_vars="$(cat <<'EOF'
|
157
|
: "${prefix:?}"
|
158
|
local src_var dest_var
|
159
|
for src_var in $(eval echo '${!'$prefix'*}'); do
|
160
|
dest_var="${src_var#$prefix}"
|
161
|
local "$dest_var=${!src_var}"; echo_vars "$dest_var"
|
162
|
done
|
163
|
EOF
|
164
|
)"
|
165
|
|
166
|
#### commands
|
167
|
|
168
|
top_dir="$(dirname "$0")" # outermost script
|
169
|
|
170
|
run_args_cmd () # runs the command line args command
|
171
|
{
|
172
|
test "$?" -eq 0 || return
|
173
|
eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
174
|
test "$#" -ge 1 || set -- all
|
175
|
echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
|
176
|
}
|
177
|
|
178
|
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
|
179
|
{
|
180
|
echo_func
|
181
|
: "${subdirs?}"
|
182
|
|
183
|
for subdir in "${subdirs[@]}"; do
|
184
|
"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
|
185
|
done
|
186
|
}
|
187
|
|
188
|
#### make
|
189
|
|
190
|
# usage: target_filename/command () { echo_func; set_make_vars; ...; }
|
191
|
alias set_make_vars="$(cat <<'EOF'
|
192
|
local command="${FUNCNAME##*/}"; echo_vars command
|
193
|
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
|
194
|
local target="$top_dir/$target_filename"; echo_vars target
|
195
|
EOF
|
196
|
)"
|
197
|
|
198
|
make ()
|
199
|
{
|
200
|
echo_func
|
201
|
env make --directory="$top_dir" "$@"
|
202
|
}
|
203
|
|
204
|
if false; then ## usage:
|
205
|
inline_make 3<<'EOF'
|
206
|
target:
|
207
|
$(self_dir)/cmd >$@
|
208
|
EOF
|
209
|
# target will be run automatically because it's first in the makefile
|
210
|
fi ##
|
211
|
inline_make ()
|
212
|
{
|
213
|
echo_func
|
214
|
local self="$(readlink -f "${BASH_SOURCE[1]}")"
|
215
|
local self_dir="$(dirname "$self")"
|
216
|
export self self_dir
|
217
|
|
218
|
make --makefile=<((
|
219
|
cat /dev/fd/3
|
220
|
echo -n "
|
221
|
.SUFFIXES: # turn off built-in suffix rules
|
222
|
.SECONDARY: # don't automatically delete intermediate files
|
223
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
224
|
"
|
225
|
)|echo_stdin) "$@"
|
226
|
}
|
227
|
|
228
|
#### compression
|
229
|
|
230
|
### zip
|
231
|
|
232
|
zip ()
|
233
|
{
|
234
|
try env zip "$@" >&2
|
235
|
ignore 12 # "zip has nothing to do" (`man zip`)
|
236
|
end_try
|
237
|
}
|
238
|
|
239
|
unzip () { env unzip "$@" >&2; }
|
240
|
|
241
|
set_inv force
|
242
|
alias zip_newer="zip${no_force:+ -u}"
|
243
|
alias unzip_newer="unzip${no_force:+ -u} -o"
|
244
|
# -o is safe because -u only extracts newer files
|
245
|
|
246
|
#### databases
|
247
|
|
248
|
# using prefixed connection vars
|
249
|
alias use_local="declare prefix=local_; import_vars"
|
250
|
alias use_remote="declare prefix=remote_; import_vars"
|
251
|
alias use_local_remote="use_local; use_remote"
|
252
|
|
253
|
quote='"'
|
254
|
|
255
|
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
256
|
|
257
|
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
|
258
|
|
259
|
alias mk_schema_esc="declare schema_esc; mk_esc_name schema"
|
260
|
alias mk_table_esc="declare table_esc; mk_esc_name table"
|
261
|
|
262
|
fi # load new aliases
|
263
|
if self_being_included; then
|
264
|
|
265
|
log_sql () { test "$verbosity" -ge 2; }
|
266
|
|
267
|
### MySQL
|
268
|
|
269
|
# auto-adds connection/login opts when specified
|
270
|
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; }
|
271
|
{
|
272
|
echo_func
|
273
|
if test _"$ssh_server" = _"$(hostname -f)"; then local ssh_server=; fi
|
274
|
if test -n "$ssh_server"; then
|
275
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
276
|
fi
|
277
|
if test -n "$schema"; then local database="${database-$schema}"; fi
|
278
|
|
279
|
local var=ssh_dest; local_inv
|
280
|
echo_run ${no_ssh_dest:+env }${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
|
281
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
282
|
${password+="$password"} ${database:+--databases "$database" --tables } "$@"
|
283
|
}
|
284
|
|
285
|
mysql ()
|
286
|
{
|
287
|
echo_func
|
288
|
mysql_cmd --verbose "$@"
|
289
|
}
|
290
|
|
291
|
mysql_ANSI ()
|
292
|
{
|
293
|
echo_func
|
294
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
295
|
}
|
296
|
|
297
|
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...]
|
298
|
{
|
299
|
echo_func
|
300
|
mysql_cmd --quick --lock-tables=false --set-charset \
|
301
|
${postgres_compat:+--compatible=postgresql --add-locks=false }\
|
302
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
303
|
}
|
304
|
|
305
|
mysqldump_diffable ()
|
306
|
{
|
307
|
echo_func
|
308
|
mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/'
|
309
|
}
|
310
|
|
311
|
### PostgreSQL
|
312
|
|
313
|
pg_copy_to ()
|
314
|
{
|
315
|
echo_func
|
316
|
if test -z "$source"; then
|
317
|
: "${table:?}"; mk_table_esc
|
318
|
if test -z "$limit"; then local source="$table_esc"
|
319
|
else local source="(SELECT * FROM $table_esc LIMIT $limit)"
|
320
|
fi
|
321
|
fi
|
322
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
323
|
|
324
|
psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
|
325
|
}
|
326
|
|
327
|
pg_header ()
|
328
|
{
|
329
|
echo_func
|
330
|
local pg_copy_format="CSV HEADER" limit=0
|
331
|
pg_copy_to "$@"|echo_stdin
|
332
|
}
|
333
|
|
334
|
pg_export_table_no_header ()
|
335
|
{
|
336
|
echo_func
|
337
|
local pg_copy_format="CSV"
|
338
|
pg_copy_to "$@"
|
339
|
}
|
340
|
|
341
|
pg_export_table_to_dir_no_header ()
|
342
|
{
|
343
|
echo_func
|
344
|
local table="$1"; shift; mk_table_esc
|
345
|
local cols="$(pg_header)"
|
346
|
pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
|
347
|
}
|
348
|
|
349
|
fi
|