Skip to content

Commit 6d6e72c

Browse files
committed
Apply suggestion by Hidde.
Signed-off-by: Felix Fontein <felix@fontein.de>
1 parent cbce6b4 commit 6d6e72c

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

cmd/sops/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,14 +2189,14 @@ func keyservices(c *cli.Context) (svcs []keyservice.KeyServiceClient) {
21892189
return
21902190
}
21912191

2192-
// Wrapper of config.FindConfigFileEx that takes care of handling the returned wraning.
2192+
// Wrapper of config.LookupConfigFile that takes care of handling the returned warning.
21932193
func findConfigFile() (string, error) {
2194-
configPath, err, warn := config.FindConfigFileEx(".")
2195-
if len(warn) > 0 && !showedConfigFileWarning {
2194+
result, err := config.LookupConfigFile(".")
2195+
if len(result.Warning) > 0 && !showedConfigFileWarning {
21962196
showedConfigFileWarning = true
2197-
log.Warn(warn)
2197+
log.Warn(result.Warning)
21982198
}
2199-
return configPath, err
2199+
return result.Path, err
22002200
}
22012201

22022202
func loadStoresConfig(context *cli.Context, path string) (*config.StoresConfig, error) {

config/config.go

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,65 @@ func (fs osFS) Stat(name string) (os.FileInfo, error) {
3737
var fs fileSystem = osFS{stat: os.Stat}
3838

3939
const (
40-
maxDepth = 100
41-
configFileName = ".sops.yaml"
42-
misspelledConfigFilename = ".sops.yml"
40+
maxDepth = 100
41+
configFileName = ".sops.yaml"
42+
alternateConfigName = ".sops.yml"
4343
)
4444

45-
// FindConfigFileEx looks for a sops config file in the current working directory and on parent directories, up to the limit defined by the maxDepth constant.
46-
// The third return value is a warning in case misspelled config files were found.
47-
func FindConfigFileEx(start string) (string, error, string) {
45+
// ConfigFileResult contains the path to a config file and any warnings
46+
type ConfigFileResult struct {
47+
Path string
48+
Warning string
49+
}
50+
51+
// LookupConfigFile looks for a sops config file in the current working directory
52+
// and on parent directories, up to the maxDepth limit.
53+
// It returns a result containing the file path and any warnings.
54+
func LookupConfigFile(start string) (ConfigFileResult, error) {
4855
filepath := path.Dir(start)
49-
var foundMisspelledPath string
50-
var warning string
56+
var foundAlternatePath string
57+
5158
for i := 0; i < maxDepth; i++ {
5259
configPath := path.Join(filepath, configFileName)
5360
_, err := fs.Stat(configPath)
54-
if err != nil {
55-
misspelledPath := path.Join(filepath, misspelledConfigFilename)
56-
_, err = fs.Stat(misspelledPath)
57-
if err == nil && len(foundMisspelledPath) == 0 {
58-
foundMisspelledPath = misspelledPath
61+
if err == nil {
62+
result := ConfigFileResult{Path: configPath}
63+
64+
if foundAlternatePath != "" {
65+
result.Warning = fmt.Sprintf(
66+
"ignoring %q when searching for config file; the config file must be called %q; using %q instead",
67+
foundAlternatePath, configFileName, configPath)
5968
}
60-
filepath = path.Join(filepath, "..")
61-
} else {
62-
if len(foundMisspelledPath) > 0 {
63-
warning = fmt.Sprintf(
64-
"Ignoring %q when searching for config file. The config file must be called %q."+
65-
" Found and using %q further up the directory tree instead.",
66-
foundMisspelledPath, configFileName, configPath)
69+
return result, nil
70+
}
71+
72+
// Check for alternate filename if we haven't found one yet
73+
if foundAlternatePath == "" {
74+
alternatePath := path.Join(filepath, alternateConfigName)
75+
_, altErr := fs.Stat(alternatePath)
76+
if altErr == nil {
77+
foundAlternatePath = alternatePath
6778
}
68-
return configPath, nil, warning
6979
}
80+
81+
filepath = path.Join(filepath, "..")
7082
}
71-
err := fmt.Errorf("Config file not found")
72-
if len(foundMisspelledPath) > 0 {
73-
warning = fmt.Sprintf("Ignoring %q when searching for config file. The config file must be called %q.", foundMisspelledPath, configFileName)
83+
84+
// No config file found
85+
result := ConfigFileResult{}
86+
if foundAlternatePath != "" {
87+
result.Warning = fmt.Sprintf(
88+
"ignoring %q when searching for config file; the config file must be called %q",
89+
foundAlternatePath, configFileName)
7490
}
75-
return "", err, warning
91+
92+
return result, fmt.Errorf("config file not found")
7693
}
7794

7895
// FindConfigFile looks for a sops config file in the current working directory and on parent directories, up to the limit defined by the maxDepth constant.
7996
func FindConfigFile(start string) (string, error) {
80-
config, err, _ := FindConfigFileEx(start)
81-
return config, err
97+
result, err := LookupConfigFile(start)
98+
return result.Path, err
8299
}
83100

84101
type DotenvStoreConfig struct{}

0 commit comments

Comments
 (0)