-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2env
272 lines (225 loc) · 7.94 KB
/
json2env
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
#!/bin/sh
# Dependencies: jq tr sed
# Tested with: busybox bourne bash zsh
usage() {
if [ "${1:-}" ]; then echo "$1" >&2; fi
name="${0##*/}"
cat <<EOF
USAGE: $name [ OPTIONS ] [ FILENAMES ]
Kilna's swiss army knife for turning key-values in a JSON object into eval-able
shell code for setting environment variables.
Processes FILENAMES as JSON documents if provided, otherwise processes standard
input as a JSON document.
Options:
--export : Export shell variables
-x (prepend each key=val with shell's export keyword)
--path PATH : JSON sub-path in jq .path.to.the.object dot-prefix notation
-p PATH (e.g '.env_vars') - defaults to '.' for the root JSON
object. The referenced path must only be a JSON object
(dict/map/hash), not an array, string, etc.
--prefix PREFIX : Prepend this string the to names of output shell variables
-p PREFIX
--upper : Translate JSON keys into uppercase shell environment vars
-u
--lower : Translate JSON keys into lowercase shell environment vars
-l
--key KEY : Only output this single key in the object
-k KEY (defaults to all keys)
--env-name NAME : When outputting a single key with --key, force this value
-e NAME as the environment variable name
--text : JSON objects and arrays become newline delimited text
-t rather than the default behavior of setting the shell
environment variable to a JSON string representation
--kv-sep STRING : Key-value separator for list-style translation of JSON
-K STRING objects (defaults to ':')
--list-sep STRING : Record separator for list-style translation
-L STRING (defaults to newline)
--array : JSON arrays are translated into POSIX shell native arrays
-a (overrides --text)
--assoc : JSON objects are tranlated into bash-style native
-a associative arrays (overrides --text)
--force : Output shell native for --array or --assoc even if the
-f current shell does not support it
--strict : Fail on JSON keys which aren't alphanumeric + underscore
-s (defaults to translating keys)
--out-file FILE : Output to a file instead of STDOUT
-o FILE
--compact : Output JSON strings in compact mode
-c
--help : Show help
-h
This script defaults to /bin/sh, if you wish to run it under your current shell
instead, call it as such:
\$SHELL $name
EOF
if [ "${1:-}" != '' ]; then exit "${2:-1}"; else exit 0; fi
}
debug() { [ "$debug" ] && echo "$1" >&2; };
die() { echo "$1" >&2; [ -e "$outfile" ] && rm "$outfile"; exit "${2:-1}"; };
shell_quote() {
if [ "$(echo "$1" | tr -c -d a-zA-Z0-9)" == "$1" ]; then
echo "$1" # Simple shell values don't need quoting
else
echo "'$(echo "$1" | sed -e "s/'/'\\\\''/")'"
fi
}
# TODO:
# Add license
# Check --path is correct
# Fix it so options and filenames can be mixed (right now inputs must be last)
# Enable --key=val style params in addition to --key value
# Shell escape key names passed into jq
# Defaults environment variable / rcfile
# Fix chomped ending newlines from using $()
# Finish tests/
# Enable DockerHub automated tests?
# Create initial release tag
# Create dockerhub build from tag
# Create make target for release of tag
c='' # jq -c flag
case='as-is'
export debug=''
env_name=''
evaluate=''
force=''
kv_sep=' '
list_sep=$'\n'
native_array=''
native_assoc=''
outfile=''
path='.'
prefix=''
strict=''
text=''
only_key=''
x='' # declare -x flag
while [ $# -gt 0 ]; do case "$1" in
-a|--array) native_array=true;;
-A|--assoc) native_assoc=true;;
-c|--compact) c='c';;
-d|--debug) debug=true;;
-e|--env-name) env_name="$2"; shift;;
-f|--force) force=true;;
-h|--help) usage;;
-k|--key) only_key="$2"; shift;;
-K|--kv-sep) kv_sep="$2"; shift;;
-l|--lower) case='lower';;
-L|--list-sep) list_sep="$2"; shift;;
-o|--out-file) outfile="$2"; shift;;
-p|--path) path="$2"; shift;;
-P|--prefix) prefix="$2"; shift;;
-s|--strict) strict=true;;
-t|--text) text=true;;
-u|--upper) case='upper';;
-x|--export) x='x';;
-*) usage "Unknown flag: $1";;
*) break;;
esac; shift; done
if [ "${#@}" -eq 0 ]; then
if [ -t 0 ]; then
usage "Must be piped into or a JSON filename provided on command line" 3
else
set -- '-'
fi
fi
if [ "$native_array" ] && [ ! "$force" ]; then
if ! (eval 'declare -a zzz=(a b) && [ "${zzz[1]}" == "b" ]' >/dev/null 2>&1); then
die "$SHELL does not support arrays with --array, use --force to output anyway"
fi
fi
if [ "$native_assoc" ] && [ ! "$force" ]; then
if ! (eval 'declare -A zzz=([a]=b) && [ "${zzz[a]}" == "b" ]' >/dev/null 2>&1); then
die "$SHELL does not support associative arrays with --assoc, use --force to output anyway"
fi
fi
process_key() {
key="$1"
json="$(cat -)"
debug ''
debug " KEY: $key"
if [ "$env_name" ]; then varname="$env_name"; else varname="$key"; fi
# Clean up varname to translate space and - to underscore, and
# remove non-alphanum + underscore chars
varname="$(echo "$varname" | tr ' -' '__' | tr -c -d a-zA-Z0-9_)"
# If we're in strict mode, fail if the key has been changed
if [ "$strict" ] && [ "$varname" != "$key" ]; then
die "Invalid key '$(shell_quote "$key")'" 6
fi
[ "$case" == 'lower' ] && varname="$(echo "$varname" | tr A-Z a-z)"
[ "$case" == 'upper' ] && varname="$(echo "$varname" | tr a-z A-Z)"
varname="$prefix$varname"
debug " VARNAME: $varname"
translate='raw'
type="$(echo "$json" | jq -r "$path | .[\"$key\"] | type")"
debug " TYPE: $type"
if [ "$type" == "array" ]; then
[ "$text" ] && translate=text-array
[ "$native_array" ] && translate=native-array
elif [ "$type" == "object" ]; then
[ "$text" ] && translate=text-assoc
[ "$native_assoc" ] && translate=native-assoc
fi
debug " TRANSLATE: $translate"
case "$translate" in
raw)
[ "$x" ] && printf '%s' 'export ';
val="$(echo "$json" | jq -${c}r "$path | .[\"$key\"]")";
echo "$varname=$(shell_quote "$val")";
;;
native-assoc)
echo "declare -A$x $varname=(";
echo "$json" | jq -j "$path | .[\"$key\"] | keys[] | (. + \"\u0000\")" | \
while read -r -d '' objkey; do
objval="$(echo "$json" | jq -${c}r "$path | .[\"$key\"] | .[\"$objkey\"]")";
echo " [$(shell_quote "$objkey")]=$(shell_quote "$objval")";
done;
echo ')';
;;
native-array)
[ "$x" ] && printf '%s' 'export ';
echo "$varname=($(echo "$json" | jq -${c}r "$path | .[\"$key\"] | @sh"))";
;;
text-array)
[ "$x" ] && printf '%s' 'export ';
val="$(
echo "$json" | jq -j "$path | .[\"$key\"] | .[] | (. + \"\u0000\")" | \
while read -r -d '' thisval; do
printf '%s' "${thisval}${list_sep}"
done
)"
echo "$varname=$(shell_quote "${val%"${list_sep}"}")";
;;
text-assoc)
[ "$x" ] && printf '%s' 'export ';
val="$(
echo "$json" | jq -j "$path | .[\"$key\"] | keys[] | (. + \"\u0000\")" | \
while read -r -d '' thiskey; do
thisval="$(echo "$json" | jq -${c}r "$path | .[\"$key\"] | .[\"$thiskey\"]")"
printf '%s' "${thiskey}${kv_sep}${thisval}${list_sep}"
done
)"
echo "$varname=$(shell_quote "${val%"${list_sep}"}")";
;;
esac
}
for file in "$@"; do
debug "FILE: $file"
json="$(cat "$file")"
debug "JSON:"
debug "$json"
out="$(
if [ "$only_key" ]; then
echo "$json" | process_key "$only_key"
else
echo "$json" | jq -j "$path | keys[] | (. + \"\u0000\")" | \
while read -r -d '' key; do
echo "$json" | process_key "$key"
done
fi
)"
if [ "$outfile" ]; then
echo "$out" >"$outfile"
else
echo "$out"
fi
done