DSR-Pipeline-ReachableCEO/vendor/git.knownelement.com/ExternalVendorCode/mo/demo/function-for-building-json
Charles N Wyble 19260c7e5a Add "mo" from "https://git.knownelement.com/ExternalVendorCode/mo.git@master"
git-vendor-name: mo
git-vendor-dir: vendor/git.knownelement.com/ExternalVendorCode/mo
git-vendor-repository: https://git.knownelement.com/ExternalVendorCode/mo.git
git-vendor-ref: master
2024-12-15 10:27:29 -06:00

40 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
cd "$(dirname "$0")" # Go to the script's directory
# Detect if this is the first item and write a comma if it is.
# Normally, I would track this using a variable, like so:
#
# COMMA_IF_NOT_FIRST_FLAG=false
# COMMA_IF_NOT_FIRST() {
# $COMMA_IF_NOT_FIRST || echo ","
# COMMA_IF_NOT_FIRST_FLAG=true
# }
#
# Since this function executes in a subshell, that approach will not work.
# Instead, we peek inside mo and see what is being processed. If the variable
# name in moParse() changes, this will need to get updated as well. An
# alternate variable that is usable is context, but that is in moLoop() and is
# two levels levels deep instead of just one.
COMMA_IF_NOT_FIRST() {
[[ "${moCurrent#*.}" != "0" ]] && echo ","
}
# Create an array that will be embedded into the JSON. If you are manipulating
# JSON, might I suggest you look at using jq? It's really good at processing
# JSON.
items=(
'{"position":"one","url":"1"}'
'{"position":"two","url":"2"}'
'{"position":"three","url":"3"}'
)
. ../mo
cat <<EOF | mo
{
{{#items}}
{{COMMA_IF_NOT_FIRST}}
{{.}}
{{/items}}
}
EOF