Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnvSubst references don't fail when the environment variable doesn't exist #338

Open
MattHodge opened this issue Apr 24, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@MattHodge
Copy link

MattHodge commented Apr 24, 2024

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
@yxxhero
Copy link
Member

yxxhero commented May 22, 2024

@mumoshu WDYT?

Add a query string like other providers, eg: ref+envsubst://$VARIABLE?must_exist=true

let must_exist defautl be true will be better?

@MattHodge

@yxxhero yxxhero added the enhancement New feature or request label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants