Skip to content

Commit 3e11a11

Browse files
refactor: move config path logic to separate func
1 parent 44c703e commit 3e11a11

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

config/config.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,48 @@ var allRules = []lint.Rule{
4949
&rule.DescriptionMinLenRule{},
5050
}
5151

52-
// GetConfig returns conf and follows the belwo precedence
52+
// GetConfig returns conf
53+
func GetConfig(flagConfPath string) (*lint.Config, error) {
54+
confFilePath, useDefault, err := GetConfigPath(flagConfPath)
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
if useDefault {
60+
return defConf, nil
61+
}
62+
63+
conf, err := Parse(confFilePath)
64+
if err != nil {
65+
return nil, err
66+
}
67+
return conf, nil
68+
}
69+
70+
// GetConfigPath returns config file path, follwing below
5371
// 1. check for conf in current directory
5472
// 2. check for conf flag
5573
// 3. load default conf
56-
func GetConfig(confFilePath string) (*lint.Config, error) {
74+
func GetConfigPath(confFilePath string) (string, bool, error) {
5775
// get current directory
5876
currentDir, err := os.Getwd()
5977
if err != nil {
60-
return nil, err
78+
return "", false, err
6179
}
6280

6381
// check if conf file exists in current directory
6482
currentDirConf := filepath.Join(currentDir, ConfFileName)
6583
if _, err1 := os.Stat(currentDirConf); !os.IsNotExist(err1) {
66-
confFilePath = currentDirConf
84+
return currentDirConf, false, nil
6785
}
6886

69-
// if confFilePath empty, means no config in current directory or config flag is empty
87+
// if confFilePath empty,
88+
// means no config in current directory or config flag is empty
89+
// use default config
7090
if confFilePath == "" {
71-
return defConf, nil
91+
return "", true, nil
7292
}
73-
74-
confFilePath = filepath.Clean(confFilePath)
75-
conf, err := Parse(confFilePath)
76-
if err != nil {
77-
return nil, err
78-
}
79-
return conf, nil
93+
return filepath.Clean(confFilePath), false, nil
8094
}
8195

8296
// GetFormatter returns the formatter as defined in conf

0 commit comments

Comments
 (0)