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)