#tools/yq
Install yq for handy work with yaml
yml2env() {
yq --unwrapScalar=false --output-format=props "${1:-.} | (.. | select((tag == \"\!\!map\" or tag == \"\!\!seq\") and length == 0)) = \"\" | ... comments=\"\"" | awk '{gsub(/\./,"_",$1)} {$1=toupper($1);print}' | sed 's/ = /=/g'
}
cat my.yaml
rpc:
proxy:
network: "unix"
unix:
dir: "/var/run/engine"
group: "${USER}"
cat my.yaml | yml2env
RPC_PROXY_NETWORK="unix"
RPC_PROXY_UNIX_DIR="/var/run/engine"
RPC_PROXY_UNIX_GROUP="${USER}"
cat my.yaml | yml2env '.rpc.proxy.unix'
DIR=/var/run/engine
GROUP=${USER}
- due to yq output format is properties, so it cannot merge lists into a string but append index, like 0, 1, .., N in a last level with single value under these index