summaryrefslogtreecommitdiff
path: root/wordpress/export-json.bash
blob: 3852dea2e910ab65db4b90d570ce850ed3ea047e (plain)
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
#!/bin/bash
#
# Synopsis:
#
#   #!/bin/bash
#   . export-json.bash
#   export_JSON <variables>
#
# Example:
#
#   $ (
#           source export-json.bash &&
#           export_JSON SSH_TTY \
#                       SSH_CONNECTION \
#                       SSH_CLIENT \
#                       SSH_AUTH_SOCK
#     )
#
#   {
#     "SSH_TTY": "/dev/pts/0",
#     "SSH_CONNECTION": "192.0.2.140 41564 198.51.100.25 22",
#     "SSH_CLIENT": "192.0.2.140 41564 22",
#     "SSH_AUTH_SOCK": "/tmp/ssh-XXXXcePKJl/agent.36326"
#   }
#
# This bash function exports shell variables as
# JSON ("javascript object notation") strings.
#
# The string is printed to stdout.
#
# JSON is a format that represents data as
# Javascript source code so modern programmers
# can read it unlike bash source code.
#
# Variable names are given to the function as its
# argument list:
#
#       $ source export-json.bash &&
#         export_JSON PATH
#
# The output is a single JSON object containing
# key-value mappings between JSON strings:
#
#       {
#         "PATH": "/usr/local/sbin:[...]"
#       }
#
# It is possible to use a different name for the
# variable in the JSON object key field than in
# the shell environment, like this:
#
#       $ export_JSON DBUS_SESSION_BUS_ADDRESS=p
#       {
#         "p": "unix:path=/run/user/1000/bus"
#       }
#
# It uses the external tool "jq" to parse string
# values placed in jq's argument list by bash and
# then encode them as JSON string values. This
# is no accidental dependency. The jq program is
# the foundation of the trustworthiness of this
# code. If we were encoding JSON strings in bash
# we would have to be a lot more careful.

arg1_to_env0()
{
	case "$1" in
		*[^a-zA-Z0-9_=]* | *=*=* )
			echo "Error: invalid variable: '$1'" >&2
			return 10
			;;
		[a-zA-Z_]* )
			set -- "${1#*=}" "${1%%=*}"
			;;
		* )
			echo "Error: invalid variable: '$1'" >&2
			return 20
			;;
	esac
	if [ -v "$2" ]
	then
		printf '%s=%s\0' "$1" "${!2}"
	else
		echo "Warning: ignoring unset variable: '$2'" >&2
	fi
}

export_to_env0()
{
	while [ $# -gt 0 ]
	do
		arg1_to_env0 "$1" || return
		shift
	done
}

to_JSON_all()
{
	(
		if [ "$1" = '-a' ]
		then
			jq_env
		fi
		set -- $(compgen -A arrayvar)
		while [ $# -gt 0 ]
		do
			to_JSON "$1"
			shift
		done
	) |
	jq -s 'add'
}

to_JSON()
{
	case "$1" in
		-A | -a )
			to_JSON_all "$@"
			return
			;;
	esac
	[ $# = 1 ] || return
	case "${!1@a}" in
		*a* )
			(
				eval "set -- \"\$1\" --args \"\${${1}[@]}\"" &&
				jq -n '{ ($k): $ARGS.positional }' \
					--arg k "$@"
			)
			;;
		*A* )
			(
				eval 'set -- --arg k "'${1}'" --args \
					"${!'${1}'[@]}" \
					"${'${1}'[@]}"'
				if [ $# -gt 4 ]
				then
					jq -n "{ (\$k): $(jq_zip2) }" "$@"
				else
					jq -n '{ ($k): {} }' "$@"
				fi
			)
			;;
		* )
			[ -v "$1" ] &&
			jq -n '{ ($k): $v }' --arg k "$1" --arg v "${!1}"
			;;
	esac
}

jq_zip2()
{
	cat <<'END'
$ARGS.positional |
[
  .[ : length/2 ],
  .[ length/2 : ]
] |
transpose |
map ({ (.[0]): .[1] }) |
add
END
}

env0_to_JSON()
{
	set --
	while read -d ''
	do
		set -- "$@" --arg "${REPLY%%=*}" "${REPLY#*=}"
	done
	jq -n -r '$ARGS.named' "$@"
}

export_JSON_unsafe()
{
	(
		set -o pipefail
		export_to_env0 "$@" | env0_to_JSON
	)
}

safety_pipe()
{
	[ $# -ge 2 ] || return
	set -- "$(mktemp)" "$@" || return
	if (shift 2; "$@") > "$1"
	then
		"$2" < "$1"
	else
		return $?
	fi
}

export_JSON()
{
	safety_pipe env0_to_JSON export_to_env0 "$@"
}

filter_vars()
{
	while read
	do
		if [ -v "$REPLY" ]
		then
			printf '%s\n' "$REPLY"
		fi
	done
}

jq_env()
{
	export_JSON $(compgen -v | filter_vars) | jq "$@"
}

jq_exports()
{
	export_JSON $(compgen -e | filter_vars) | jq "$@"
}

try()
{
	"$@"
	printf '(Exit %s) <- [%s]\n' "$?" "${*@Q}" >&2
}

runtest()
{
	set -- SSH_CLIENT SSH_TTY SSH_AUTH_SOCK SSH_CONNECTION
	try export_JSON "$@"
	unset unsetvar
	try export_JSON SSH_TTY unsetvar
	try export_JSON unsetvar SSH_TTY
	try export_JSON
	try export_JSON ''
	try export_JSON 'invalid!' SSH_TTY
	try export_JSON SSH_TTY 'invalid!'
	try jq_env .unsetvar
	emptyvar= try jq_env .emptyvar
	try jq_exports '.|{TERM,LANG,HOSTTYPE,EDITOR,SHELL}'
	try jq_env '.|{TERM,LANG,HOSTTYPE,EDITOR,SHELL}'
	try to_JSON PATH BASH_ARGV BASH_VERSINFO BASH_ALIASES BASH_CMDS
}