diff options
author | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-01-26 10:33:27 -0500 |
---|---|---|
committer | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-01-26 10:33:27 -0500 |
commit | c1260ca34d9b41d617a4398759443a1df1abfab7 (patch) | |
tree | 1241b7c5d7ffb720e4dc2d93f913e1a24b577884 | |
parent | c0847adfdcc8501fc4bfe87cbf71a67cadd2ae98 (diff) |
factoring
-rw-r--r-- | wordpress/export-json.bash | 172 |
1 files changed, 122 insertions, 50 deletions
diff --git a/wordpress/export-json.bash b/wordpress/export-json.bash index 3852dea..5404ceb 100644 --- a/wordpress/export-json.bash +++ b/wordpress/export-json.bash | |||
@@ -85,6 +85,34 @@ arg1_to_env0() | |||
85 | fi | 85 | fi |
86 | } | 86 | } |
87 | 87 | ||
88 | # for_each - environmentally-hygienic loop | ||
89 | # | ||
90 | # The first argument is de-quoted as a command | ||
91 | # evaluated once against each of the other | ||
92 | # supplied arguments, in order, aborting if the | ||
93 | # command returns an error. | ||
94 | # | ||
95 | # Doesn't touch the bash environment. | ||
96 | for_each() | ||
97 | { | ||
98 | while [ $# -ge 2 ] | ||
99 | do | ||
100 | $1 "$2" || return | ||
101 | # This just removes $2 from the arglist: | ||
102 | eval 'shift 2; set -- '"${1@Q}"' "$@"' || return | ||
103 | done | ||
104 | } | ||
105 | |||
106 | # IDEA: bash module w/ some simple FORTH style | ||
107 | # point-free ops on arglist. To eliminate the | ||
108 | # evals. Of course, there is not much use for | ||
109 | # point-free code like this, it only makes | ||
110 | # sense if you care about the code providing a | ||
111 | # pristine environment for the callback -- which | ||
112 | # only matters for highly general library style | ||
113 | # code meant to be reused in unknown future | ||
114 | # environments. | ||
115 | |||
88 | export_to_env0() | 116 | export_to_env0() |
89 | { | 117 | { |
90 | while [ $# -gt 0 ] | 118 | while [ $# -gt 0 ] |
@@ -96,56 +124,14 @@ export_to_env0() | |||
96 | 124 | ||
97 | to_JSON_all() | 125 | to_JSON_all() |
98 | { | 126 | { |
99 | ( | 127 | jq -s 'add' < \ |
100 | if [ "$1" = '-a' ] | 128 | <( |
101 | then | 129 | if [ "$1" = '-a' ] |
102 | jq_env | 130 | then |
103 | fi | 131 | jq_env |
104 | set -- $(compgen -A arrayvar) | 132 | fi |
105 | while [ $# -gt 0 ] | 133 | for_each to_JSON1 $(compgen -A arrayvar) |
106 | do | 134 | ) |
107 | to_JSON "$1" | ||
108 | shift | ||
109 | done | ||
110 | ) | | ||
111 | jq -s 'add' | ||
112 | } | ||
113 | |||
114 | to_JSON() | ||
115 | { | ||
116 | case "$1" in | ||
117 | -A | -a ) | ||
118 | to_JSON_all "$@" | ||
119 | return | ||
120 | ;; | ||
121 | esac | ||
122 | [ $# = 1 ] || return | ||
123 | case "${!1@a}" in | ||
124 | *a* ) | ||
125 | ( | ||
126 | eval "set -- \"\$1\" --args \"\${${1}[@]}\"" && | ||
127 | jq -n '{ ($k): $ARGS.positional }' \ | ||
128 | --arg k "$@" | ||
129 | ) | ||
130 | ;; | ||
131 | *A* ) | ||
132 | ( | ||
133 | eval 'set -- --arg k "'${1}'" --args \ | ||
134 | "${!'${1}'[@]}" \ | ||
135 | "${'${1}'[@]}"' | ||
136 | if [ $# -gt 4 ] | ||
137 | then | ||
138 | jq -n "{ (\$k): $(jq_zip2) }" "$@" | ||
139 | else | ||
140 | jq -n '{ ($k): {} }' "$@" | ||
141 | fi | ||
142 | ) | ||
143 | ;; | ||
144 | * ) | ||
145 | [ -v "$1" ] && | ||
146 | jq -n '{ ($k): $v }' --arg k "$1" --arg v "${!1}" | ||
147 | ;; | ||
148 | esac | ||
149 | } | 135 | } |
150 | 136 | ||
151 | jq_zip2() | 137 | jq_zip2() |
@@ -162,6 +148,79 @@ add | |||
162 | END | 148 | END |
163 | } | 149 | } |
164 | 150 | ||
151 | json_encode_associative_array() | ||
152 | { | ||
153 | eval 'set -- --arg k "'"$1"'" --args \ | ||
154 | "${!'"$1"'[@]}" \ | ||
155 | "${'"$1"'[@]}"' && | ||
156 | if [ $# -gt 4 ] | ||
157 | then | ||
158 | jq -n "{ (\$k): $(jq_zip2) }" "$@" | ||
159 | else | ||
160 | jq -n '{ ($k): {} }' "$@" | ||
161 | fi | ||
162 | } | ||
163 | |||
164 | json_encode_indexed_array() | ||
165 | { | ||
166 | eval 'set -- '"${1@Q}"' --args "${'"$1"'[@]}"' && | ||
167 | jq -n '{ ($k): $ARGS.positional }' --arg k "$@" | ||
168 | } | ||
169 | |||
170 | # This uses the more complex implementation that, | ||
171 | # when used with multiple arguments, does not | ||
172 | # require extra calls to jq. Since this is only | ||
173 | # using a single argument, a more straightforward | ||
174 | # implementation could be used just as well. | ||
175 | json_encode_string() | ||
176 | { | ||
177 | [ -v "$1" ] && | ||
178 | export_JSON_unsafe "$1" | ||
179 | } | ||
180 | |||
181 | # The straightforward implementation: | ||
182 | json_encode_string() | ||
183 | { | ||
184 | [ -v "$1" ] && | ||
185 | jq -n '{ ($k): $v }' --arg k "$1" --arg v "${!1}" | ||
186 | } | ||
187 | # But is having two implementations really | ||
188 | # straightforward? And what about this | ||
189 | # parenthetical commentary? Moreso wobbly, if not | ||
190 | # winding, error-prone complexity finding itself | ||
191 | # grinding up on the accidents of time passing it | ||
192 | # might be anyway but the takeaway is don't touch | ||
193 | # it till it croak, the tests will run and the | ||
194 | # result will speak, may truth spread through the | ||
195 | # dendrites of Samizdat! | ||
196 | |||
197 | to_JSON() | ||
198 | { | ||
199 | case $# in | ||
200 | 0) | ||
201 | to_JSON_all -a | ||
202 | ;; | ||
203 | *) | ||
204 | for_each to_JSON1 "$@" | ||
205 | ;; | ||
206 | esac | ||
207 | } | ||
208 | |||
209 | to_JSON1() | ||
210 | { | ||
211 | case "${!1@a}" in | ||
212 | *a* ) | ||
213 | json_encode_indexed_array "$1" | ||
214 | ;; | ||
215 | *A* ) | ||
216 | json_encode_associative_array "$1" | ||
217 | ;; | ||
218 | * ) | ||
219 | json_encode_string "$1" | ||
220 | ;; | ||
221 | esac | ||
222 | } | ||
223 | |||
165 | env0_to_JSON() | 224 | env0_to_JSON() |
166 | { | 225 | { |
167 | set -- | 226 | set -- |
@@ -241,3 +300,16 @@ runtest() | |||
241 | try jq_env '.|{TERM,LANG,HOSTTYPE,EDITOR,SHELL}' | 300 | try jq_env '.|{TERM,LANG,HOSTTYPE,EDITOR,SHELL}' |
242 | try to_JSON PATH BASH_ARGV BASH_VERSINFO BASH_ALIASES BASH_CMDS | 301 | try to_JSON PATH BASH_ARGV BASH_VERSINFO BASH_ALIASES BASH_CMDS |
243 | } | 302 | } |
303 | |||
304 | # code poetry flowetry toiletry coiled spring load | ||
305 | # the home row write tight c with terry davis down | ||
306 | # in a basement univalent foundation concrete on | ||
307 | # the bottom with donald while they all on top of | ||
308 | # things they found up there still we founding | ||
309 | # here building tall stacks of calls out to all to | ||
310 | # any accepting returns of types unknown or known | ||
311 | # | ||
312 | # defensively typed values for anomic millennial | ||
313 | # hackers | ||
314 | # | ||
315 | # atomized castaways of aging social bodies | ||