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