Skip to content

Commit

Permalink
fix(commands): storage cmd fail when implicit config absent (#5213)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
james-d-elliott committed Apr 11, 2023
1 parent 0312def commit 569af0f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
20 changes: 13 additions & 7 deletions internal/commands/util.go
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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)

continue
switch result {
case XEnvCLIResultCLIImplicit:
continue
default:
configs = append(configs, path)
files = append(files, path)

continue
}
}

return nil, fmt.Errorf("error occurred stating file at path '%s': %w", path, err)
Expand Down
25 changes: 20 additions & 5 deletions internal/commands/util_test.go
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 569af0f

Please sign in to comment.