diff options
author | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-01-24 12:05:00 -0500 |
---|---|---|
committer | root <root@vps-18a7a2b7.vps.ovh.ca> | 2024-01-24 12:05:00 -0500 |
commit | 9434a1f76f17471cd849ec8ccfd09a5e5ef51f71 (patch) | |
tree | 452318b7aba0baacabe4833a8837eeb0e0e365d6 /wordpress | |
parent | a4d09aa6aeb1961f7ee8f239fec5b5c5f9297857 (diff) |
export-json fixes
Diffstat (limited to 'wordpress')
-rw-r--r-- | wordpress/export-json.bash | 181 |
1 files changed, 121 insertions, 60 deletions
diff --git a/wordpress/export-json.bash b/wordpress/export-json.bash index 343e66d..735a649 100644 --- a/wordpress/export-json.bash +++ b/wordpress/export-json.bash | |||
@@ -1,15 +1,18 @@ | |||
1 | #!/bin/bash | 1 | #!/bin/bash |
2 | 2 | ||
3 | # This script allows bash to export environment | 3 | # This script allows bash to export environment |
4 | # variables as JSON. It uses the external tool | 4 | # variables as JSON. A list of environment |
5 | # "jq" to parse string values # placed in jq's | 5 | # variables and their values is exported as a |
6 | # argument list by bash and then encode them | 6 | # single JSON object containing key-value mappings |
7 | # as JSON string values. This is no accidental | 7 | # of JSON strings. It uses the external tool "jq" |
8 | # dependency. The jq program is the foundation of | 8 | # to parse string values # placed in jq's argument |
9 | # the trustworthiness of this code. If we were | 9 | # list by bash and then encode them as JSON string |
10 | # encoding JSON strings in bash (and not just JSON | 10 | # values. This is no accidental dependency. The jq |
11 | # objects containing strings) we would have to be | 11 | # program is the foundation of the trustworthiness |
12 | # a lot more careful. | 12 | # of this code. If we were encoding JSON strings |
13 | # in bash (and not just encoding the JSON objects | ||
14 | # containing the strings) we would have to be a | ||
15 | # lot more careful. | ||
13 | 16 | ||
14 | # This is the simple code I wanted to replace: | 17 | # This is the simple code I wanted to replace: |
15 | # | 18 | # |
@@ -27,13 +30,16 @@ | |||
27 | # , table_prefix: $t | 30 | # , table_prefix: $t |
28 | # }' | 31 | # }' |
29 | # | 32 | # |
30 | # The problem with the above is that the names are repeated. | 33 | # The problem with the above is that the names are |
31 | # This creates the possibility that the names not match, | 34 | # repeated. |
32 | # creating a bug. | 35 | # This creates the possibility that the names not |
33 | # This code allows the names to be specified only one time. | 36 | # match, creating a bug. |
34 | # It allows the bug where the names do not match to be fixed only one | 37 | # This code allows the names to be specified only |
35 | # time by one person. | 38 | # one time. |
36 | # IF ... the fix can be distributed back to the extant copies. | 39 | # It allows the bug where the names do not match |
40 | # to be fixed only one time by one person. | ||
41 | # IF ... the fix can be distributed back to the | ||
42 | # extant copies. | ||
37 | # How to get the beneficial mutations | 43 | # How to get the beneficial mutations |
38 | # back into the living organisms? | 44 | # back into the living organisms? |
39 | # Life tries not to answer this question | 45 | # Life tries not to answer this question |
@@ -41,63 +47,118 @@ | |||
41 | # since after all | 47 | # since after all |
42 | # the extant copies are the past | 48 | # the extant copies are the past |
43 | # and life looks into the future | 49 | # and life looks into the future |
44 | # but because consciousness | 50 | # But because consciousness |
45 | # is the mirror of time | 51 | # is the mirror of time |
46 | # the question is asked | 52 | # the question is asked |
47 | # eventually | 53 | # eventually |
54 | # when that level | ||
55 | # of life | ||
56 | # consciousness is reached. | ||
48 | 57 | ||
49 | export_JSON() | 58 | var_to_env0() |
59 | { | ||
60 | case "$1" in | ||
61 | *[^a-zA-Z0-9_=]* ) | ||
62 | false | ||
63 | ;; | ||
64 | [a-zA-Z_]*=* ) | ||
65 | set -- "${1#*=}" "${1%%=*}" | ||
66 | ;; | ||
67 | [a-zA-Z_]* ) | ||
68 | set -- "$1" "$1" | ||
69 | ;; | ||
70 | * ) | ||
71 | false | ||
72 | ;; | ||
73 | esac && | ||
74 | if [ -v "$2" ] | ||
75 | then | ||
76 | printf '%s=%s\0' "$1" "${!2}" | ||
77 | else | ||
78 | warn "Warning: ignoring unset variable: '$2'" | ||
79 | fi | ||
80 | } | ||
81 | |||
82 | vars_to_env0() | ||
50 | { | 83 | { |
51 | declare -a exported_vars=() | ||
52 | declare -A displayname=() | ||
53 | while [ $# -gt 0 ] | 84 | while [ $# -gt 0 ] |
54 | do | 85 | do |
55 | case "$1" in | 86 | var_to_env0 "$1" || return |
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 | 87 | shift |
71 | done | 88 | done |
72 | export_with_jq | ||
73 | } | 89 | } |
74 | 90 | ||
75 | export_with_jq() | 91 | env0_to_JSON() |
76 | { | 92 | { |
77 | n=$'\n' | 93 | local REPLY |
78 | jqargs=("-n") | 94 | set -- |
79 | jqjson= | 95 | while read -d '' |
80 | for v in "${exported_vars[@]}" | ||
81 | do | 96 | do |
82 | if ! [ -v "$v" ] | 97 | set -- "$@" --arg "${REPLY%%=*}" "${REPLY#*=}" |
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 | 98 | done |
97 | if [ "$jqjson" ] | 99 | jq -n -r '$ARGS.named' "$@" |
98 | then | 100 | } |
99 | jqjson="$jqjson}$n" | 101 | |
100 | fi | 102 | warn() |
101 | jq -n "${jqargs[@]}" "$jqjson" | 103 | { |
104 | printf '%s\n' "$*" >&2 | ||
102 | } | 105 | } |
103 | 106 | ||
107 | export_JSON() | ||
108 | { | ||
109 | ( | ||
110 | set -e | ||
111 | set -o pipefail | ||
112 | vars_to_env0 "$@" | env0_to_JSON | ||
113 | ) | ||
114 | } | ||
115 | |||
116 | try() | ||
117 | { | ||
118 | "$@" | ||
119 | warn "${*@Q} -> $?" | ||
120 | } | ||
121 | |||
122 | runtest() | ||
123 | { | ||
124 | set -- SSH_CLIENT SSH_TTY SSH_AUTH_SOCK SSH_CONNECTION | ||
125 | try export_JSON "$@" | ||
126 | unset unsetvar | ||
127 | try export_JSON SSH_TTY unsetvar | ||
128 | try export_JSON | ||
129 | try export_JSON '' | ||
130 | try export_JSON '' SSH_TTY | ||
131 | } | ||
132 | |||
133 | # Cryptography and routing needs to and does work like the benefits | ||
134 | # office where there is a fixed supply to hand out so there is a line | ||
135 | # of recipients waiting and in order to save resources there is a | ||
136 | # limit to its size and a residue of turnaways. So it is with tcp | ||
137 | # connections and in order for a service to minimize disruption to other | ||
138 | # tcp connections it need only limit the number of open connections it | ||
139 | # allows; understanding that the residue of turnaways will increase but | ||
140 | # the fixed supply will be delivered and the turnaways will re-enter the | ||
141 | # queue to receive belatedly. | ||
142 | # | ||
143 | # The connections in which we are interested are not TCP connections, | ||
144 | # they are VPN connections over UDP, but not really, they are social | ||
145 | # connections sustained through a software<->computer interface | ||
146 | # intermediary. | ||
147 | # | ||
148 | # The social program of Samizdat involves tending to the computers by | ||
149 | # interfacing with their USB ports and network ports, not their screens! | ||
150 | # The screen is used to seek user confirmation WITH DEfAULT TIMEOUT | ||
151 | # that makes Samizdat human-interaction-optional. Furthermore, Samizdat | ||
152 | # attempts to use the network to make human-interaction require no | ||
153 | # physical presence, but rather the human can interact from any one of | ||
154 | # their nodes that is live at the time! These identity-holding nodes | ||
155 | # hold SSH servers open on the internet AND thereby hold the personal | ||
156 | # cryptographic identity that YOU can most easily export and use on YOUR | ||
157 | # OWN servers. | ||
158 | # | ||
159 | # Anyway, we allow connectivity onto these machines in various ways, | ||
160 | # for example, the local network can take over the display if the user | ||
161 | # has never logged in, allowing to authorize the first install to disk | ||
162 | # on the machine without needing the machine to have a working display | ||
163 | # or input device. | ||
164 | |||