diff --git a/pkg/app/app.go b/pkg/app/app.go index 7d05c74e..0778fe15 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -15,6 +15,7 @@ import ( "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/argparser" + "github.com/helmfile/helmfile/pkg/envvar" "github.com/helmfile/helmfile/pkg/filesystem" "github.com/helmfile/helmfile/pkg/helmexec" "github.com/helmfile/helmfile/pkg/plugins" @@ -1216,7 +1217,7 @@ func (a *App) findDesiredStateFiles(specifiedPath string, opts LoadOpts) ([]stri case defaultFile != "": return []string{defaultFile}, nil default: - return []string{}, fmt.Errorf("no state file found. It must be named %s/*.{yaml,yml,yaml.gotmpl,yml.gotmpl}, %s, or %s, otherwise specified with the --file flag", DefaultHelmfileDirectory, DefaultHelmfile, DefaultGotmplHelmfile) + return []string{}, fmt.Errorf("no state file found. It must be named %s/*.{yaml,yml,yaml.gotmpl,yml.gotmpl}, %s, or %s, otherwise specified with the --file flag or %s environment variable", DefaultHelmfileDirectory, DefaultHelmfile, DefaultGotmplHelmfile, envvar.FilePath) } } diff --git a/pkg/config/global.go b/pkg/config/global.go index 0580343b..b34711cc 100644 --- a/pkg/config/global.go +++ b/pkg/config/global.go @@ -8,6 +8,7 @@ import ( "go.uber.org/zap" "golang.org/x/term" + "github.com/helmfile/helmfile/pkg/envvar" "github.com/helmfile/helmfile/pkg/maputil" "github.com/helmfile/helmfile/pkg/state" ) @@ -120,7 +121,12 @@ func (g *GlobalImpl) Chart() string { // FileOrDir returns the path to the Helmfile. func (g *GlobalImpl) FileOrDir() string { - return g.GlobalOptions.File + file := g.GlobalOptions.File + if file == "" { + file = os.Getenv(envvar.FilePath) + } + + return file } // Selectors returns the selectors to use. diff --git a/pkg/config/global_test.go b/pkg/config/global_test.go new file mode 100644 index 00000000..d59e6ca8 --- /dev/null +++ b/pkg/config/global_test.go @@ -0,0 +1,47 @@ +package config + +import ( + "os" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/helmfile/helmfile/pkg/envvar" +) + +// TestFileOrDir tests if statement +func TestFileOrDir(t *testing.T) { + tests := []struct { + opts GlobalOptions + env string + expected string + }{ + { + opts: GlobalOptions{}, + env: "", + expected: "", + }, + { + opts: GlobalOptions{}, + env: "envset", + expected: "envset", + }, + { + opts: GlobalOptions{File: "folderset"}, + env: "", + expected: "folderset", + }, + { + opts: GlobalOptions{File: "folderset"}, + env: "envset", + expected: "folderset", + }, + } + + for _, test := range tests { + os.Setenv(envvar.FilePath, test.env) + received := NewGlobalImpl(&test.opts).FileOrDir() + require.Equalf(t, test.expected, received, "FileOrDir expected %t, received %t", test.expected, received) + } + os.Unsetenv(envvar.FilePath) +} diff --git a/pkg/envvar/const.go b/pkg/envvar/const.go index 1a8bcd97..7fa9ee82 100644 --- a/pkg/envvar/const.go +++ b/pkg/envvar/const.go @@ -6,6 +6,7 @@ const ( SkipInsecureTemplateFunctions = "HELMFILE_SKIP_INSECURE_TEMPLATE_FUNCTIONS" Experimental = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case Environment = "HELMFILE_ENVIRONMENT" + FilePath = "HELMFILE_FILE_PATH" TempDir = "HELMFILE_TEMPDIR" UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED" V1Mode = "HELMFILE_V1MODE"