From 9434a1f76f17471cd849ec8ccfd09a5e5ef51f71 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 24 Jan 2024 12:05:00 -0500 Subject: export-json fixes --- wordpress/export-json.bash | 181 ++++++++++++++++++++++++++++++--------------- 1 file changed, 121 insertions(+), 60 deletions(-) (limited to 'wordpress/export-json.bash') 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 @@ #!/bin/bash # This script allows bash to export environment -# variables as JSON. 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 (and not just JSON -# objects containing strings) we would have to be -# a lot more careful. +# variables as JSON. A list of environment +# variables and their values is exported as a +# single JSON object containing key-value mappings +# of JSON strings. 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 (and not just encoding the JSON objects +# containing the strings) we would have to be a +# lot more careful. # This is the simple code I wanted to replace: # @@ -27,13 +30,16 @@ # , table_prefix: $t # }' # -# The problem with the above is that the names are repeated. -# This creates the possibility that the names not match, -# creating a bug. -# This code allows the names to be specified only one time. -# It allows the bug where the names do not match to be fixed only one -# time by one person. -# IF ... the fix can be distributed back to the extant copies. +# The problem with the above is that the names are +# repeated. +# This creates the possibility that the names not +# match, creating a bug. +# This code allows the names to be specified only +# one time. +# It allows the bug where the names do not match +# to be fixed only one time by one person. +# IF ... the fix can be distributed back to the +# extant copies. # How to get the beneficial mutations # back into the living organisms? # Life tries not to answer this question @@ -41,63 +47,118 @@ # since after all # the extant copies are the past # and life looks into the future -# but because consciousness +# But because consciousness # is the mirror of time # the question is asked # eventually +# when that level +# of life +# consciousness is reached. -export_JSON() +var_to_env0() +{ + case "$1" in + *[^a-zA-Z0-9_=]* ) + false + ;; + [a-zA-Z_]*=* ) + set -- "${1#*=}" "${1%%=*}" + ;; + [a-zA-Z_]* ) + set -- "$1" "$1" + ;; + * ) + false + ;; + esac && + if [ -v "$2" ] + then + printf '%s=%s\0' "$1" "${!2}" + else + warn "Warning: ignoring unset variable: '$2'" + fi +} + +vars_to_env0() { - declare -a exported_vars=() - declare -A displayname=() while [ $# -gt 0 ] do - case "$1" in - v | exported_vars | displayname | jqargs | jqjson | n ) - continue - ;; - *[^a-zA-Z0-9_=]* | *=*=* ) - continue - ;; - [a-zA-Z_]*=* ) - exported_vars+=("${1%=*}") - displayname["${1%=*}"]=${1#*=} - ;; - [a-zA-Z_]* ) - exported_vars+=("$1") - ;; - esac + var_to_env0 "$1" || return shift done - export_with_jq } -export_with_jq() +env0_to_JSON() { - n=$'\n' - jqargs=("-n") - jqjson= - for v in "${exported_vars[@]}" + local REPLY + set -- + while read -d '' do - if ! [ -v "$v" ] - then - echo "Warning: not defined: $v" >&2 - continue - fi - jqargs+=(--arg "$v" "${!v}") - if [ "$jqjson" ] - then - p=',' - else - p='{' - fi - dv=${displayname[$v]} - jqjson="$jqjson$p ${dv:-$v}: \$$v$n" + set -- "$@" --arg "${REPLY%%=*}" "${REPLY#*=}" done - if [ "$jqjson" ] - then - jqjson="$jqjson}$n" - fi - jq -n "${jqargs[@]}" "$jqjson" + jq -n -r '$ARGS.named' "$@" +} + +warn() +{ + printf '%s\n' "$*" >&2 } +export_JSON() +{ + ( + set -e + set -o pipefail + vars_to_env0 "$@" | env0_to_JSON + ) +} + +try() +{ + "$@" + warn "${*@Q} -> $?" +} + +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 + try export_JSON '' + try export_JSON '' SSH_TTY +} + +# Cryptography and routing needs to and does work like the benefits +# office where there is a fixed supply to hand out so there is a line +# of recipients waiting and in order to save resources there is a +# limit to its size and a residue of turnaways. So it is with tcp +# connections and in order for a service to minimize disruption to other +# tcp connections it need only limit the number of open connections it +# allows; understanding that the residue of turnaways will increase but +# the fixed supply will be delivered and the turnaways will re-enter the +# queue to receive belatedly. +# +# The connections in which we are interested are not TCP connections, +# they are VPN connections over UDP, but not really, they are social +# connections sustained through a software<->computer interface +# intermediary. +# +# The social program of Samizdat involves tending to the computers by +# interfacing with their USB ports and network ports, not their screens! +# The screen is used to seek user confirmation WITH DEfAULT TIMEOUT +# that makes Samizdat human-interaction-optional. Furthermore, Samizdat +# attempts to use the network to make human-interaction require no +# physical presence, but rather the human can interact from any one of +# their nodes that is live at the time! These identity-holding nodes +# hold SSH servers open on the internet AND thereby hold the personal +# cryptographic identity that YOU can most easily export and use on YOUR +# OWN servers. +# +# Anyway, we allow connectivity onto these machines in various ways, +# for example, the local network can take over the display if the user +# has never logged in, allowing to authorize the first install to disk +# on the machine without needing the machine to have a working display +# or input device. + -- cgit v1.2.3