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 <james-d-elliott@users.noreply.github.com>
pull/5204/head^2
James Elliott 2023-04-11 20:52:04 +10:00 committed by GitHub
parent 0312defcd7
commit 569af0fef0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 11 deletions

View File

@ -215,13 +215,14 @@ const (
func loadXEnvCLIConfigValues(cmd *cobra.Command) (configs []string, filters []configuration.FileFilter, err error) { func loadXEnvCLIConfigValues(cmd *cobra.Command) (configs []string, filters []configuration.FileFilter, err error) {
var ( var (
filterNames []string 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 return nil, nil, err
} }
if configs, err = loadXNormalizedPaths(configs); err != nil { if configs, err = loadXNormalizedPaths(configs, result); err != nil {
return nil, nil, err return nil, nil, err
} }
@ -236,7 +237,7 @@ func loadXEnvCLIConfigValues(cmd *cobra.Command) (configs []string, filters []co
return return
} }
func loadXNormalizedPaths(paths []string) ([]string, error) { func loadXNormalizedPaths(paths []string, result XEnvCLIResult) ([]string, error) {
var ( var (
configs, files, dirs []string configs, files, dirs []string
err error err error
@ -258,10 +259,15 @@ func loadXNormalizedPaths(paths []string) ([]string, error) {
files = append(files, path) files = append(files, path)
default: default:
if os.IsNotExist(err) { if os.IsNotExist(err) {
configs = append(configs, path) switch result {
files = append(files, path) 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) return nil, fmt.Errorf("error occurred stating file at path '%s': %w", path, err)

View File

@ -96,6 +96,7 @@ func TestLoadXNormalizedPaths(t *testing.T) {
ayml := filepath.Join(configdir, "a.yml") ayml := filepath.Join(configdir, "a.yml")
byml := filepath.Join(configdir, "b.yml") byml := filepath.Join(configdir, "b.yml")
cyml := filepath.Join(otherdir, "c.yml") cyml := filepath.Join(otherdir, "c.yml")
dyml := filepath.Join(otherdir, "d.yml")
file, err = os.Create(ayml) file, err = os.Create(ayml)
@ -142,30 +143,44 @@ func TestLoadXNormalizedPaths(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
haveX XEnvCLIResult
have, expected []string have, expected []string
expectedErr string expectedErr string
}{ }{
{"ShouldAllowFiles", {"ShouldAllowFiles",
XEnvCLIResultCLIImplicit, []string{ayml},
[]string{ayml}, []string{ayml},
[]string{ayml}, "", "",
},
{"ShouldSkipFilesNotExistImplicit",
XEnvCLIResultCLIImplicit, []string{dyml},
[]string(nil),
"",
},
{"ShouldNotErrFilesNotExistExplicit",
XEnvCLIResultCLIExplicit, []string{dyml},
[]string{dyml},
"",
}, },
{"ShouldAllowDirectories", {"ShouldAllowDirectories",
XEnvCLIResultCLIImplicit, []string{configdir},
[]string{configdir}, []string{configdir},
[]string{configdir}, "", "",
}, },
{"ShouldAllowFilesDirectories", {"ShouldAllowFilesDirectories",
XEnvCLIResultCLIImplicit, []string{ayml, otherdir},
[]string{ayml, otherdir}, []string{ayml, otherdir},
[]string{ayml, otherdir}, "", "",
}, },
{"ShouldRaiseErrOnOverlappingFilesDirectories", {"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), 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 { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { 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) assert.Equal(t, tc.expected, actual)