From 569af0fef0168e60cb7b56697fb1f000bef8f34c Mon Sep 17 00:00:00 2001 From: James Elliott Date: Tue, 11 Apr 2023 20:52:04 +1000 Subject: [PATCH] fix(commands): storage cmd fail when implicit config absent (#5213) This fixes an issue where if the implicit config location of configuration.yml does not exist that an error is returned. This does not affect the behavior when the method was either implicit or environment. Signed-off-by: James Elliott --- internal/commands/util.go | 18 ++++++++++++------ internal/commands/util_test.go | 25 ++++++++++++++++++++----- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/internal/commands/util.go b/internal/commands/util.go index 4950c271b..bab9c70c9 100644 --- a/internal/commands/util.go +++ b/internal/commands/util.go @@ -215,13 +215,14 @@ const ( func loadXEnvCLIConfigValues(cmd *cobra.Command) (configs []string, filters []configuration.FileFilter, err error) { var ( filterNames []string + result XEnvCLIResult ) - if configs, _, err = loadXEnvCLIStringSliceValue(cmd, cmdFlagEnvNameConfig, cmdFlagNameConfig); err != nil { + if configs, result, err = loadXEnvCLIStringSliceValue(cmd, cmdFlagEnvNameConfig, cmdFlagNameConfig); err != nil { return nil, nil, err } - if configs, err = loadXNormalizedPaths(configs); err != nil { + if configs, err = loadXNormalizedPaths(configs, result); err != nil { return nil, nil, err } @@ -236,7 +237,7 @@ func loadXEnvCLIConfigValues(cmd *cobra.Command) (configs []string, filters []co return } -func loadXNormalizedPaths(paths []string) ([]string, error) { +func loadXNormalizedPaths(paths []string, result XEnvCLIResult) ([]string, error) { var ( configs, files, dirs []string err error @@ -258,10 +259,15 @@ func loadXNormalizedPaths(paths []string) ([]string, error) { files = append(files, path) default: if os.IsNotExist(err) { - configs = append(configs, path) - files = append(files, path) + switch result { + case XEnvCLIResultCLIImplicit: + continue + default: + configs = append(configs, path) + files = append(files, path) - continue + continue + } } return nil, fmt.Errorf("error occurred stating file at path '%s': %w", path, err) diff --git a/internal/commands/util_test.go b/internal/commands/util_test.go index 1aa6aaf83..f2306558b 100644 --- a/internal/commands/util_test.go +++ b/internal/commands/util_test.go @@ -96,6 +96,7 @@ func TestLoadXNormalizedPaths(t *testing.T) { ayml := filepath.Join(configdir, "a.yml") byml := filepath.Join(configdir, "b.yml") cyml := filepath.Join(otherdir, "c.yml") + dyml := filepath.Join(otherdir, "d.yml") file, err = os.Create(ayml) @@ -142,30 +143,44 @@ func TestLoadXNormalizedPaths(t *testing.T) { testCases := []struct { name string + haveX XEnvCLIResult have, expected []string expectedErr string }{ {"ShouldAllowFiles", + XEnvCLIResultCLIImplicit, []string{ayml}, []string{ayml}, - []string{ayml}, "", + "", + }, + {"ShouldSkipFilesNotExistImplicit", + XEnvCLIResultCLIImplicit, []string{dyml}, + []string(nil), + "", + }, + {"ShouldNotErrFilesNotExistExplicit", + XEnvCLIResultCLIExplicit, []string{dyml}, + []string{dyml}, + "", }, {"ShouldAllowDirectories", + XEnvCLIResultCLIImplicit, []string{configdir}, []string{configdir}, - []string{configdir}, "", + "", }, {"ShouldAllowFilesDirectories", + XEnvCLIResultCLIImplicit, []string{ayml, otherdir}, []string{ayml, otherdir}, - []string{ayml, otherdir}, "", + "", }, {"ShouldRaiseErrOnOverlappingFilesDirectories", - []string{ayml, configdir}, + XEnvCLIResultCLIImplicit, []string{ayml, configdir}, nil, fmt.Sprintf("failed to load config directory '%s': the config file '%s' is in that directory which is not supported", configdir, ayml), }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - actual, actualErr := loadXNormalizedPaths(tc.have) + actual, actualErr := loadXNormalizedPaths(tc.have, tc.haveX) assert.Equal(t, tc.expected, actual)