From 88830425ccfbbc6cf3d29ce7f968480c398fd3a2 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Mon, 30 May 2022 15:54:21 +0200 Subject: [PATCH] allow overwriting a default value by setting an empty envirionment variable --- .../unreleased/fix-allow-empty-environment-variables | 8 ++++++++ ocis-pkg/config/envdecode/envdecode.go | 11 ++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 changelog/unreleased/fix-allow-empty-environment-variables diff --git a/changelog/unreleased/fix-allow-empty-environment-variables b/changelog/unreleased/fix-allow-empty-environment-variables new file mode 100644 index 0000000000..57a1101e5b --- /dev/null +++ b/changelog/unreleased/fix-allow-empty-environment-variables @@ -0,0 +1,8 @@ +Bugfix: Allow empty environment variables + +We've fixed the behavior for empty environment variables, that previously would +not have overwritten default values. Therefore it had the same effect like not +setting the environment variable. We now check if the environment variable is +set at all and if so, we also allow to override a default value with an empty value. + +https://github.com/owncloud/ocis/pull/3892 diff --git a/ocis-pkg/config/envdecode/envdecode.go b/ocis-pkg/config/envdecode/envdecode.go index 0105c1764f..8a26b84dff 100644 --- a/ocis-pkg/config/envdecode/envdecode.go +++ b/ocis-pkg/config/envdecode/envdecode.go @@ -149,10 +149,11 @@ func decode(target interface{}, strict bool) (int, error) { overrides := strings.Split(parts[0], `;`) var env string + var envSet bool for _, override := range overrides { - v := os.Getenv(override) - if v != "" { + if v, set := os.LookupEnv(override); set { env = v + envSet = true } } @@ -176,13 +177,13 @@ func decode(target interface{}, strict bool) (int, error) { if required && hasDefault { panic(`envdecode: "default" and "required" may not be specified in the same annotation`) } - if env == "" && required { + if !envSet && required { return 0, fmt.Errorf("the environment variable \"%s\" is missing", parts[0]) } - if env == "" { + if !envSet { env = defaultValue } - if env == "" { + if !envSet && env == "" { continue }