From 632a9ca792c51218e7ea4382cc4d8bf7387a3b3b Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Jan 2024 14:52:54 -0500 Subject: improve error reporting --- wordpress/export-json.bash | 53 ++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/wordpress/export-json.bash b/wordpress/export-json.bash index f146c19..714946e 100644 --- a/wordpress/export-json.bash +++ b/wordpress/export-json.bash @@ -65,34 +65,33 @@ arg1_to_env0() { case "$1" in - *[^a-zA-Z0-9_=]* | *=*=* ) - echo "Error: invalid variable: '$1'" >&2 + *=*=* ) + set -- "${1#*=}" + echo "Error: invalid variable: ${1@Q}" >&2 + return 30 + ;; + *[^a-zA-Z0-9_=]* ) + echo "Error: invalid variable: ${1@Q}" >&2 return 10 ;; [a-zA-Z_]* ) - set -- "${1#*=}" "${1%%=*}" ;; * ) - echo "Error: invalid variable: '$1'" >&2 + echo "Error: invalid variable: ${1@Q}" >&2 return 20 ;; esac + set -- "${1##*=}" "${1%%=*}" if [ -v "$2" ] then printf '%s=%s\0' "$1" "${!2}" else - echo "Warning: ignoring unset variable: '$2'" >&2 + echo "Warning: ignoring unset variable: ${2@Q}" >&2 fi } -# for_each - environmentally-hygienic loop -# -# The first argument is de-quoted as a command -# evaluated once against each of the other -# supplied arguments, in order, aborting if the -# command returns an error. -# -# Doesn't touch the bash environment. +# Hygienic loop. Doesn't touch the shell +# environment. for_each() { while [ $# -ge 2 ] @@ -102,11 +101,6 @@ for_each() done } -export_to_env0() -{ - for_each arg1_to_env0 "$@" -} - to_JSON_all() { jq -s 'add' < \ @@ -190,11 +184,20 @@ json_encode_string() to_JSON() { - case $# in - 0) - to_JSON_all -a + case "$#$1" in + 0 ) + printf \ + '%s\n' \ + 'usage: to_JSON -a' \ + ' to_JSON -A' \ + ' to_JSON [...]' \ + >&2 + return -1 ;; - *) + 1-a | 1-A ) + to_JSON_all $1 + ;; + * ) for_each to_JSON1 "$@" ;; esac @@ -229,7 +232,7 @@ export_JSON_unsafe() { ( set -o pipefail - export_to_env0 "$@" | env0_to_JSON + for_each arg1_to_env0 "$@" | env0_to_JSON ) } @@ -237,7 +240,7 @@ safety_pipe() { [ $# -ge 2 ] || return set -- "$(mktemp)" "$@" || return - if (shift 2; "$@") > "$1" + if "${@:3}" > "$1" then "$2" < "$1" else @@ -247,7 +250,7 @@ safety_pipe() export_JSON() { - safety_pipe env0_to_JSON export_to_env0 "$@" + safety_pipe env0_to_JSON for_each arg1_to_env0 "$@" } filter_vars() -- cgit v1.2.3