add support for HELMFILE_FILE_PATH envvar to set -f (#1255)

* fix imports

Signed-off-by: Shane Starcher <shanestarcher@gmail.com>

* unset env var

Signed-off-by: Shane Starcher <shanestarcher@gmail.com>

---------

Signed-off-by: Shane Starcher <shanestarcher@gmail.com>
This commit is contained in:
Shane Starcher 2024-01-03 07:37:49 -06:00 committed by GitHub
parent 4871a92b8c
commit d681c62ff8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -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.

47
pkg/config/global_test.go Normal file
View File

@ -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)
}

View File

@ -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"