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
|
unalias () { builtin unalias "$@" 2>&- || true; } # no error if undefined
|
22
|
|
23
|
if self_not_included "${BASH_SOURCE[0]}"; then
|
24
|
|
25
|
shopt -s expand_aliases
|
26
|
|
27
|
#### arrays
|
28
|
|
29
|
join () { local IFS="$delim"; echo "$*"; } # usage: delim=... join elems...
|
30
|
|
31
|
reverse () # usage: array=($(reverse args...))
|
32
|
{
|
33
|
local i
|
34
|
for (( i=$#; i >= 1; i-- )); do printf '%q ' "${!i}"; done
|
35
|
}
|
36
|
|
37
|
#### verbose output
|
38
|
|
39
|
: "${verbosity:=$verbose}" "${verbosity:=0}"
|
40
|
|
41
|
echo_cmd () { echo "$PS4$*" >&2; }
|
42
|
|
43
|
echo_run () { echo_cmd "$@"; "$@"; }
|
44
|
|
45
|
if test "$verbosity" -ge 1; then
|
46
|
alias env="echo_run env" # automatically echo commands that use env
|
47
|
fi
|
48
|
|
49
|
canon_rel_path ()
|
50
|
{
|
51
|
local path="$1"
|
52
|
path="$(realpath "$path")" # canonicalize
|
53
|
path="${path#$(pwd -P)/}" # remove any shared prefix with the current dir
|
54
|
echo "$path"
|
55
|
}
|
56
|
|
57
|
echo_func_ ()
|
58
|
{
|
59
|
local script="$(canon_rel_path "${BASH_SOURCE[1]}")"
|
60
|
echo_cmd "$script:${BASH_LINENO[0]}" "${FUNCNAME[1]}" "$@"
|
61
|
}
|
62
|
alias echo_func='echo_func_ "$@"' # usage: func () { echo_func; ...; }
|
63
|
|
64
|
echo_stdin () # usage: input|echo_stdin|cmd
|
65
|
{
|
66
|
echo ----- >&2
|
67
|
tee -a /dev/stderr;
|
68
|
echo ----- >&2
|
69
|
}
|
70
|
|
71
|
echo_vars () # usage: echo_vars var...
|
72
|
{ { echo -n "$PS4"; declare -p "${@%%=*}";} >&2; }
|
73
|
|
74
|
echo_export ()
|
75
|
{
|
76
|
builtin export "$@"
|
77
|
echo_vars "$@"
|
78
|
}
|
79
|
|
80
|
if test "$verbosity" -ge 2; then
|
81
|
alias export="echo_export" # automatically echo env vars when they are set
|
82
|
fi
|
83
|
|
84
|
usage () { echo "Usage: $1" >&2; (exit 2); }
|
85
|
|
86
|
fi # load new aliases
|
87
|
if self_being_included; then
|
88
|
|
89
|
#### strings
|
90
|
|
91
|
sed_ere_flag="$(test "$(uname)" = Darwin && echo E || echo r)"
|
92
|
|
93
|
sed () { env sed -"$sed_ere_flag" "$@";}
|
94
|
|
95
|
#### vars
|
96
|
|
97
|
set_var () { eval "$1"'="$2"'; }
|
98
|
|
99
|
set_inv () { set_var no_"$1" "$(test -n "${!1}" || echo 1)"; }
|
100
|
|
101
|
# usage: local var=...; local_inv
|
102
|
alias local_inv='local "no_$var=$(test -n "${!var}" || echo 1)"'
|
103
|
|
104
|
# usage: local prefix=..._; import_vars
|
105
|
alias import_vars="$(cat <<'EOF'
|
106
|
: "${prefix:?}"
|
107
|
local src_var dest_var
|
108
|
for src_var in $(eval echo '${!'$prefix'*}'); do
|
109
|
dest_var="${src_var#$prefix}"
|
110
|
local "$dest_var=${!src_var}"; echo_vars "$dest_var"
|
111
|
done
|
112
|
EOF
|
113
|
)"
|
114
|
|
115
|
#### commands
|
116
|
|
117
|
top_dir="$(dirname "$0")" # outermost script
|
118
|
|
119
|
run_args_cmd () # runs the command line args command
|
120
|
{
|
121
|
test "$?" -eq 0 || return
|
122
|
eval set -- "$(reverse "${BASH_ARGV[@]}")"
|
123
|
test "$#" -ge 1 || set -- all
|
124
|
echo_cmd "$(canon_rel_path "$0")" "$@"; "$@"
|
125
|
}
|
126
|
|
127
|
fwd () # usage: subdirs=(...); fwd "$FUNCNAME" "$@"
|
128
|
{
|
129
|
echo_func
|
130
|
: "${subdirs?}"
|
131
|
|
132
|
for subdir in "${subdirs[@]}"; do
|
133
|
"$(dirname "${BASH_SOURCE[1]}")"/"$subdir"/run "$@"
|
134
|
done
|
135
|
}
|
136
|
|
137
|
#### make
|
138
|
|
139
|
# usage: target_filename/command () { echo_func; set_make_vars; ...; }
|
140
|
alias set_make_vars="$(cat <<'EOF'
|
141
|
local command="${FUNCNAME##*/}"; echo_vars command
|
142
|
local target_filename="${FUNCNAME%/*}"; echo_vars target_filename
|
143
|
local target="$top_dir/$target_filename"; echo_vars target
|
144
|
EOF
|
145
|
)"
|
146
|
|
147
|
make ()
|
148
|
{
|
149
|
echo_func
|
150
|
env make --directory="$top_dir" "$@"
|
151
|
}
|
152
|
|
153
|
if false; then ## usage:
|
154
|
inline_make 3<<'EOF'
|
155
|
target:
|
156
|
$(self_dir)/cmd >$@
|
157
|
EOF
|
158
|
# target will be run automatically because it's first in the makefile
|
159
|
fi ##
|
160
|
inline_make ()
|
161
|
{
|
162
|
echo_func
|
163
|
local self="$(readlink -f "${BASH_SOURCE[1]}")"
|
164
|
local self_dir="$(dirname "$self")"
|
165
|
export self self_dir
|
166
|
|
167
|
make --makefile=<((
|
168
|
cat /dev/fd/3
|
169
|
echo -n "
|
170
|
.SUFFIXES: # turn off built-in suffix rules
|
171
|
.SECONDARY: # don't automatically delete intermediate files
|
172
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
173
|
"
|
174
|
)|echo_stdin) "$@"
|
175
|
}
|
176
|
|
177
|
#### compression
|
178
|
|
179
|
### zip
|
180
|
|
181
|
alias zip="echo_run zip"
|
182
|
alias unzip="echo_run unzip"
|
183
|
set_inv force
|
184
|
alias zip_newer="zip${no_force:+ -u}"
|
185
|
alias unzip_newer="unzip${no_force:+ -u} -o"
|
186
|
# -o is safe because -u only extracts newer files
|
187
|
|
188
|
#### databases
|
189
|
|
190
|
# using prefixed connection vars
|
191
|
alias use_local="local prefix=local_; import_vars"
|
192
|
alias use_remote="local prefix=remote_; import_vars"
|
193
|
alias use_local_remote="use_local; use_remote"
|
194
|
|
195
|
quote='"'
|
196
|
|
197
|
esc_name () { echo "$quote${1//$quote/$quote$quote}$quote"; }
|
198
|
|
199
|
mk_esc_name () { set_var "$1"_esc "$(esc_name "${!1}")"; }
|
200
|
|
201
|
alias mk_schema_esc="local schema_esc; mk_esc_name schema"
|
202
|
alias mk_table_esc="local table_esc; mk_esc_name table"
|
203
|
|
204
|
fi # load new aliases
|
205
|
if self_being_included; then
|
206
|
|
207
|
log_sql () { test "$verbosity" -ge 2; }
|
208
|
|
209
|
### MySQL
|
210
|
|
211
|
# auto-adds connection/login opts when specified
|
212
|
mysql_cmd () # usage: mysql* () { ...; mysql_cmd "$@"; }
|
213
|
{
|
214
|
echo_func
|
215
|
if test _"$ssh_server" = _"$(hostname -f)"; then local ssh_server=; fi
|
216
|
if test -n "$ssh_server"; then
|
217
|
local ssh_dest="${ssh_dest-${ssh_user:+$ssh_user@}$ssh_server}"
|
218
|
fi
|
219
|
if test -n "$schema"; then local database="${database-$schema}"; fi
|
220
|
|
221
|
local var=ssh_dest; local_inv
|
222
|
echo_run ${no_ssh_dest:+env }${ssh_dest:+ssh "$ssh_dest" }"${FUNCNAME[1]}" \
|
223
|
${server:+ --host="$server" }${user:+--user="$user" } --password\
|
224
|
${password+="$password"} ${database:+--databases "$database" --tables } "$@"
|
225
|
}
|
226
|
|
227
|
mysql ()
|
228
|
{
|
229
|
echo_func
|
230
|
mysql_cmd --verbose "$@"
|
231
|
}
|
232
|
|
233
|
mysql_ANSI ()
|
234
|
{
|
235
|
echo_func
|
236
|
(echo "SET sql_mode = 'ANSI';"; cat)|mysql "$@"
|
237
|
}
|
238
|
|
239
|
mysqldump () # usage: [schema=1 | data=1] mysqldump db [table...]
|
240
|
{
|
241
|
echo_func
|
242
|
mysql_cmd --quick --lock-tables=false --set-charset \
|
243
|
${postgres_compat:+--compatible=postgresql --add-locks=false }\
|
244
|
${schema:+--no-data }${data:+--no-create-info }"$@"
|
245
|
}
|
246
|
|
247
|
mysqldump_diffable ()
|
248
|
{
|
249
|
echo_func
|
250
|
mysqldump "$@"|sed 's/^(-- Dump completed).*$/\1/'
|
251
|
}
|
252
|
|
253
|
### PostgreSQL
|
254
|
|
255
|
pg_copy_to ()
|
256
|
{
|
257
|
echo_func
|
258
|
if test -z "$source"; then
|
259
|
: "${table:?}"; mk_table_esc
|
260
|
if test -z "$limit"; then local source="$table_esc"
|
261
|
else local source="(SELECT * FROM $table_esc LIMIT $limit)"
|
262
|
fi
|
263
|
fi
|
264
|
local pg_copy_format="${pg_copy_format-CSV HEADER}"
|
265
|
|
266
|
psql "$@" <<<"COPY $source TO STDOUT $pg_copy_format;"
|
267
|
}
|
268
|
|
269
|
pg_header ()
|
270
|
{
|
271
|
echo_func
|
272
|
local pg_copy_format="CSV HEADER" limit=0
|
273
|
pg_copy_to "$@"|echo_stdin
|
274
|
}
|
275
|
|
276
|
pg_export_table_no_header ()
|
277
|
{
|
278
|
echo_func
|
279
|
local pg_copy_format="CSV"
|
280
|
pg_copy_to "$@"
|
281
|
}
|
282
|
|
283
|
pg_export_table_to_dir_no_header ()
|
284
|
{
|
285
|
echo_func
|
286
|
local table="$1"; shift; mk_table_esc
|
287
|
local cols="$(pg_header)"
|
288
|
pg_export_table_no_header "$@" >"$exports_dir/$table.no_header.cols=$cols.csv"
|
289
|
}
|
290
|
|
291
|
fi
|