github.com/hashicorp/hcl/v2@v2.20.0/cmd/hcldec/examples/sh-config-file/example.sh (about) 1 #!/bin/bash 2 # Copyright (c) HashiCorp, Inc. 3 # SPDX-License-Identifier: MPL-2.0 4 5 6 set -euo pipefail 7 8 # All paths from this point on are relative to the directory containing this 9 # script, for simplicity's sake. 10 cd "$( dirname "${BASH_SOURCE[0]}" )" 11 12 # Read the config file using hcldec and then use jq to extract values in a 13 # shell-friendly form. jq will ensure that the values are properly quoted and 14 # escaped for consumption by the shell. 15 CONFIG_VARS="$(hcldec --spec=spec.hcldec example.conf | jq -r '@sh "NAME=\(.name) GREETING=\(.greeting) FRIENDS=(\(.friends))"')" 16 if [ $? != 0 ]; then 17 # If hcldec or jq failed then it has already printed out some error messages 18 # and so we can bail out. 19 exit $? 20 fi 21 22 # Import our settings into our environment 23 eval "$CONFIG_VARS" 24 25 # ...and now, some contrived usage of the settings we loaded: 26 echo "$GREETING $NAME!" 27 for name in ${FRIENDS[@]}; do 28 echo "$GREETING $name, too!" 29 done