From 64b2d8e0fdf09cd09f795e9da19970b910adc76c Mon Sep 17 00:00:00 2001 From: Anton Velichko Date: Mon, 6 Mar 2023 12:44:42 +0400 Subject: [PATCH 1/3] feat: add flag for disabling recursive environment variables expansion --- internal/app/cli.go | 7 +++++++ internal/app/utils.go | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/app/cli.go b/internal/app/cli.go index 24a2f009..f6651926 100644 --- a/internal/app/cli.go +++ b/internal/app/cli.go @@ -88,6 +88,7 @@ type cli struct { diffContext int noEnvSubst bool substEnvValues bool + noRecursiveEnvExpand bool noSSMSubst bool substSSMValues bool detailedExitCode bool @@ -147,6 +148,7 @@ func (c *cli) setup() { flag.BoolVar(&c.detailedExitCode, "detailed-exit-code", false, "returns a detailed exit code (0 - no changes, 1 - error, 2 - changes present)") flag.BoolVar(&c.noEnvSubst, "no-env-subst", false, "turn off environment substitution globally") flag.BoolVar(&c.substEnvValues, "subst-env-values", false, "turn on environment substitution in values files.") + flag.BoolVar(&c.noRecursiveEnvExpand, "no-recursive-env-expand", false, "disable recursive environment values expansion") flag.BoolVar(&c.noSSMSubst, "no-ssm-subst", false, "turn off SSM parameter substitution globally") flag.BoolVar(&c.substSSMValues, "subst-ssm-values", false, "turn on SSM parameter substitution in values files.") flag.BoolVar(&c.updateDeps, "update-deps", false, "run 'helm dep up' for local charts") @@ -240,6 +242,11 @@ func (c *cli) parse() { log.Verbose("Substitution of env variables in values enabled") } } + + if !c.noRecursiveEnvExpand { + log.Verbose("Recursive environment variables expansion is enabled") + } + if !c.noSSMSubst { log.Verbose("Substitution of SSM variables enabled") if c.substSSMValues { diff --git a/internal/app/utils.go b/internal/app/utils.go index b1cd6ccf..ec5f3e29 100644 --- a/internal/app/utils.go +++ b/internal/app/utils.go @@ -146,8 +146,10 @@ func readFile(filepath string) string { // recusively expanding the variable's value func getEnv(key string) string { value := os.Getenv(key) - for envVar.MatchString(value) { - value = os.ExpandEnv(value) + if !flags.noRecursiveEnvExpand { + for envVar.MatchString(value) { + value = os.ExpandEnv(value) + } } return value } From b8cd4c6a347539e960356e9a5f640806162f16f7 Mon Sep 17 00:00:00 2001 From: Anton Velichko Date: Mon, 6 Mar 2023 12:58:57 +0400 Subject: [PATCH 2/3] chore: fill the cli documentation about new flag --- docs/cmd_reference.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/cmd_reference.md b/docs/cmd_reference.md index af4337b8..3180df83 100644 --- a/docs/cmd_reference.md +++ b/docs/cmd_reference.md @@ -69,6 +69,9 @@ This lists available CMD options in Helmsman: `--no-env-subst` turn off environment substitution globally. + `--no-recursive-env-expand` + disable recursive environment variables expansion. + `--subst-env-values` turn on environment substitution in values files. From 3f925554d17add20ca5f725cd89c95b2a2fada12 Mon Sep 17 00:00:00 2001 From: Anton Velichko Date: Tue, 7 Mar 2023 12:22:42 +0400 Subject: [PATCH 3/3] chore: optimize flags --- internal/app/main.go | 2 +- internal/app/utils.go | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/app/main.go b/internal/app/main.go index 423a1a27..ec9a31b0 100644 --- a/internal/app/main.go +++ b/internal/app/main.go @@ -8,7 +8,7 @@ import ( const ( helmBin = "helm" kubectlBin = "kubectl" - appVersion = "v3.16.4" + appVersion = "v3.16.4-fix" tempFilesDir = ".helmsman-tmp" defaultContextName = "default" resourcePool = 10 diff --git a/internal/app/utils.go b/internal/app/utils.go index ec5f3e29..a5095ba9 100644 --- a/internal/app/utils.go +++ b/internal/app/utils.go @@ -162,13 +162,15 @@ func prepareEnv(envFiles []string) error { return fmt.Errorf("error loading env file: %w", err) } } - for _, e := range os.Environ() { - if !strings.Contains(e, "$") { - continue + if !flags.noRecursiveEnvExpand { + for _, e := range os.Environ() { + if !strings.Contains(e, "$") { + continue + } + e = os.Expand(e, getEnv) + pair := strings.SplitN(e, "=", 2) + os.Setenv(pair[0], pair[1]) } - e = os.Expand(e, getEnv) - pair := strings.SplitN(e, "=", 2) - os.Setenv(pair[0], pair[1]) } return nil } @@ -180,7 +182,7 @@ func substituteEnv(str string) string { if strings.Contains(str, "$") { // add $$ escaping for $ strings os.Setenv("HELMSMAN_DOLLAR", "$") - return os.ExpandEnv(strings.ReplaceAll(str, "$$", "${HELMSMAN_DOLLAR}")) + return os.Expand(strings.ReplaceAll(str, "$$", "${HELMSMAN_DOLLAR}"), getEnv) } return str }