Open
Description
When using the EnvSubst provider, if an environment variable doesn't exist, it will just pass a blank string to the evaluated file and not error.
To replicate the issue:
echo 'foo: ref+envsubst://$VARIABLE' | vals eval -f -
Result: foo: ""
export VARIABLE=bar
echo 'foo: ref+envsubst://$VARIABLE' | vals eval -f -
unset VARIABLE
Result: foo: bar
2 potential ideas for a backwards compatible way to address this:
- Add a new provider called "mustenvsubst" eg:
ref+mustenvsubst://$VARIABLE
which would fail if the variable is not set - Add a query string like other providers, eg:
ref+envsubst://$VARIABLE?must_exist=true
Happy to take a stab at the PR once given guidance on the preferred direction to address this :)
I am performing a vals eval
on a JSON source file, so using this script as a workaround:
#!/bin/bash
set -eo pipefail
IFS=$'\n\t'
# This script will parse the JSON file and fail if the requested vals environment variables don't exist.
if [ $# -ne 1 ]; then
cat <<< "Usage: check_vals_envsubst_exist.sh <json-file-to-be-evaled-by-vals>" 1>&2;
exit 1
fi
missing_vars=false
while read -r var; do
if [ -z "${!var}" ]; then
echo "[MISSING] Environment Variable '$var'"
missing_vars=true
else
echo "[FOUND] Environment Variable '$var'"
fi
done < <(jq -r '.. | select(type == "string" and test("^ref\\+envsubst://\\$\\w+")) | sub("^ref\\+envsubst://\\$"; "")' $1)
if [ "$missing_vars" = true ]; then
echo "The [MISSING] environment variables are missing. Make sure they are set so vals can use them. Exiting."
exit 1
fi