diff options
Diffstat (limited to 'wordpress/export-json.bash')
-rw-r--r-- | wordpress/export-json.bash | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/wordpress/export-json.bash b/wordpress/export-json.bash new file mode 100644 index 0000000..343e66d --- /dev/null +++ b/wordpress/export-json.bash | |||
@@ -0,0 +1,103 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | # This script allows bash to export environment | ||
4 | # variables as JSON. It uses the external tool | ||
5 | # "jq" to parse string values # placed in jq's | ||
6 | # argument list by bash and then encode them | ||
7 | # as JSON string values. This is no accidental | ||
8 | # dependency. The jq program is the foundation of | ||
9 | # the trustworthiness of this code. If we were | ||
10 | # encoding JSON strings in bash (and not just JSON | ||
11 | # objects containing strings) we would have to be | ||
12 | # a lot more careful. | ||
13 | |||
14 | # This is the simple code I wanted to replace: | ||
15 | # | ||
16 | # jq --arg SITE "$SITE" "$json" | ||
17 | # jq -n \ | ||
18 | # --arg h "$db_host" \ | ||
19 | # --arg u "$db_user" \ | ||
20 | # --arg p "$db_password" \ | ||
21 | # --arg n "$db_name" \ | ||
22 | # --arg t "$table_prefix" \ | ||
23 | # '{ db_host: $h | ||
24 | # , db_user: $u | ||
25 | # , db_password: $p | ||
26 | # , db_name: $n | ||
27 | # , table_prefix: $t | ||
28 | # }' | ||
29 | # | ||
30 | # The problem with the above is that the names are repeated. | ||
31 | # This creates the possibility that the names not match, | ||
32 | # creating a bug. | ||
33 | # This code allows the names to be specified only one time. | ||
34 | # It allows the bug where the names do not match to be fixed only one | ||
35 | # time by one person. | ||
36 | # IF ... the fix can be distributed back to the extant copies. | ||
37 | # How to get the beneficial mutations | ||
38 | # back into the living organisms? | ||
39 | # Life tries not to answer this question | ||
40 | # until very late in its development | ||
41 | # since after all | ||
42 | # the extant copies are the past | ||
43 | # and life looks into the future | ||
44 | # but because consciousness | ||
45 | # is the mirror of time | ||
46 | # the question is asked | ||
47 | # eventually | ||
48 | |||
49 | export_JSON() | ||
50 | { | ||
51 | declare -a exported_vars=() | ||
52 | declare -A displayname=() | ||
53 | while [ $# -gt 0 ] | ||
54 | do | ||
55 | case "$1" in | ||
56 | v | exported_vars | displayname | jqargs | jqjson | n ) | ||
57 | continue | ||
58 | ;; | ||
59 | *[^a-zA-Z0-9_=]* | *=*=* ) | ||
60 | continue | ||
61 | ;; | ||
62 | [a-zA-Z_]*=* ) | ||
63 | exported_vars+=("${1%=*}") | ||
64 | displayname["${1%=*}"]=${1#*=} | ||
65 | ;; | ||
66 | [a-zA-Z_]* ) | ||
67 | exported_vars+=("$1") | ||
68 | ;; | ||
69 | esac | ||
70 | shift | ||
71 | done | ||
72 | export_with_jq | ||
73 | } | ||
74 | |||
75 | export_with_jq() | ||
76 | { | ||
77 | n=$'\n' | ||
78 | jqargs=("-n") | ||
79 | jqjson= | ||
80 | for v in "${exported_vars[@]}" | ||
81 | do | ||
82 | if ! [ -v "$v" ] | ||
83 | then | ||
84 | echo "Warning: not defined: $v" >&2 | ||
85 | continue | ||
86 | fi | ||
87 | jqargs+=(--arg "$v" "${!v}") | ||
88 | if [ "$jqjson" ] | ||
89 | then | ||
90 | p=',' | ||
91 | else | ||
92 | p='{' | ||
93 | fi | ||
94 | dv=${displayname[$v]} | ||
95 | jqjson="$jqjson$p ${dv:-$v}: \$$v$n" | ||
96 | done | ||
97 | if [ "$jqjson" ] | ||
98 | then | ||
99 | jqjson="$jqjson}$n" | ||
100 | fi | ||
101 | jq -n "${jqargs[@]}" "$jqjson" | ||
102 | } | ||
103 | |||