-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathbash-env-json
executable file
·276 lines (236 loc) · 7.04 KB
/
bash-env-json
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env bash
#
# Copyright 2024 Simon Guest
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the “Software”), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, subl# icense, and/or sell copies of the
# Software, and to permit persons to whom the Software is furnished to
# do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
VERSION="0.9.1"
USAGE="bash-env-json [--help] [--shellfns <comma-separated-function-names>] [path]"
shopt -s extglob
function capture() {
local -n _capture_env="$1"
local -n _capture_shellvars="$2"
local _name _value
# some shell variables leak into our environment, so inhibit that:
local -A _inhibit_shellvars=([_value]=X [BASH_LINENO]=X [COLUMNS]=X [LINES]=X)
# environment variables
while IFS='=' read -r -d '' _name _value; do
_capture_env["$_name"]="${_value}"
done < <(env -0)
# shellvars
for _name in $(
set -o posix
set | sed -n -e '/^[a-zA-Z_][a-zA-Z_0-9]*=/s/=.*$//p'
set +o posix
); do
if test -v "$_name" -a ! "${_capture_env[$_name]+EXISTS}" -a ! "${_inhibit_shellvars[$_name]+EXISTS}"; then
_capture_shellvars["$_name"]="${!_name}"
fi
done
}
function emit_value() {
# jq -R produces nothing on empty input, but we want ""
if test -n "$1"; then
echo -n "$1" | jq -R
else
echo -n '""'
fi
}
function emit_meta() {
local _prefix="$1"
local _suffix="$2"
local _usage="$3"
echo -n "$_prefix\"meta\":{\"version\":"
emit_value "$VERSION"
test -n "$_usage" && {
echo -n ',"usage":'
emit_value "$USAGE"
}
echo "}$_suffix"
}
function emit_error() {
local _msg="$1"
local _usage="$2"
echo -n '{"error":'
emit_value "$_msg"
emit_meta , '}' "$_usage"
}
function emit_error_exit() {
emit_error "$@" | jq
exit 1
}
function emit_help_exit() {
emit_meta '{' '}' usage | jq
exit 0
}
function emit_version_exit() {
emit_meta '{' '}' | jq
exit 0
}
function emit() {
local _name
local -a _names
local _comma=""
local _sep="$1"
local _tag="$2"
local -n _emit_previous="$3"
local -n _emit_current="$4"
echo -n "$_sep\"$_tag\":{"
# changes
_names=("${!_emit_current[@]}")
for _name in "${_names[@]}"; do
if test "${_emit_current[$_name]}" != "${_emit_previous[$_name]}" -o "${_emit_previous[$_name]+EXISTS}" != EXISTS; then
if [[ "$_name" != BASH_FUNC_* ]]; then
echo -n "${_comma}\"$_name\":"
emit_value "${_emit_current[$_name]}"
_comma=","
fi
fi
done
# unset
for _name in "${!_emit_previous[@]}"; do
if test ! -v "$_name"; then
if [[ "$_name" != BASH_FUNC_* ]]; then
echo -n "${_comma}\"$_name\":null"
_comma=","
fi
fi
done
echo -n "}"
}
function eval_or_source() {
local _source _path _error_file
_path="$1"
_error_file=$(mktemp -u)
touch "$_error_file"
# shellcheck disable=SC2094
exec 3<"$_error_file" 4>"$_error_file"
rm -f "$_error_file"
if test -n "$_path"; then
# source from file if specified
if test ! -r "$_path"; then
emit_error_exit "no such file '$_path'"
fi
# ShellCheck can't cope with sourcing from an unknown path
# shellcheck disable=SC1090
if ! source "$_path" >/dev/null 2>&4; then
exec 4>&-
emit_error_exit "$(head -1 <&3)"
fi
else
# otherwise eval from stdin
_source=$(</dev/stdin)
if ! eval "$_source" >/dev/null 2>&4; then
exec 4>&-
# discard error location, because it is this file not the one sourced
emit_error_exit "$(sed -e 's/^.*line\s*[0-9]*:\s*//' <&3)"
fi
fi
}
function invoke_safely() {
local _fn="$1"
_error_file=$(mktemp -u)
touch "$_error_file"
# shellcheck disable=SC2094
exec 3<"$_error_file" 4>"$_error_file"
rm -f "$_error_file"
"$_fn" >/dev/null 2>&4 || {
exec 4>&-
emit_error_exit "$(head -1 <&3)"
}
}
function get_args() {
local -n _opt_path="$1"
local -n _opt_shellfn_names="$2"
shift 2
# process args
while test -n "$1"; do
case "$1" in
--help)
emit_help_exit
;;
--version)
emit_version_exit
;;
--shellfns)
test -z "$2" && {
bad_usage "--shellfns requires comma-separated list of function names"
}
mapfile -td, _opt_shellfn_names <<<"$2,"
unset '_opt_shellfn_names[-1]'
shift
;;
-*)
bad_usage "unexpected option: $1"
;;
*)
test -n "$_opt_path" && {
bad_usage "repeated path '$_opt_path'"
}
_opt_path="$1"
;;
esac
shift
done
}
function main() {
local _path _fn
declare -a _shellfn_names
get_args _path _shellfn_names "$@"
declare -A _env_previous
declare -A _env_current
declare -A _shellvars_previous
declare -A _shellvars_current
capture _env_previous _shellvars_previous
eval_or_source "$_path"
capture _env_current _shellvars_current
# accumulate result in a file until we know we are error-free
_result_file=$(mktemp -u)
touch "$_result_file"
# shellcheck disable=SC2094
exec 5<"$_result_file" 6>"$_result_file"
rm -f "$_result_file"
emit "{" env _env_previous _env_current >&6
emit "," shellvars _shellvars_previous _shellvars_current >&6
test "${#_shellfn_names[@]}" -gt 0 && {
echo ",\"fn\":{" >&6
}
local _fn_comma=""
for _fn in "${_shellfn_names[@]}"; do
capture _env_previous _shellvars_previous
invoke_safely "$_fn"
capture _env_current _shellvars_current
echo "$_fn_comma\"$_fn\":" >&6
emit "{" env _env_previous _env_current >&6
emit "," shellvars _shellvars_previous _shellvars_current >&6
echo "}" >&6
_fn_comma=","
done
test "${#_shellfn_names[@]}" -gt 0 && {
echo "}" >&6
}
emit_meta ',' '}' >&6
exec 6>&-
jq <&5
}
function bad_usage() {
emit_error_exit "$1" usage
}
main "$@"