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