6
6
"os"
7
7
"path/filepath"
8
8
"slices"
9
- "strings"
10
9
11
10
"github.com/go-viper/mapstructure/v2"
12
11
"github.com/mitchellh/go-homedir"
@@ -33,16 +32,18 @@ type Loader struct {
33
32
34
33
log logutils.Log
35
34
36
- cfg * Config
35
+ cfg * Config
36
+ args []string
37
37
}
38
38
39
- func NewLoader (log logutils.Log , v * viper.Viper , fs * pflag.FlagSet , opts LoaderOptions , cfg * Config ) * Loader {
39
+ func NewLoader (log logutils.Log , v * viper.Viper , fs * pflag.FlagSet , opts LoaderOptions , cfg * Config , args [] string ) * Loader {
40
40
return & Loader {
41
41
opts : opts ,
42
42
viper : v ,
43
43
fs : fs ,
44
44
log : log ,
45
45
cfg : cfg ,
46
+ args : args ,
46
47
}
47
48
}
48
49
@@ -116,50 +117,59 @@ func (l *Loader) evaluateOptions() (string, error) {
116
117
}
117
118
118
119
func (l * Loader ) setupConfigFileSearch () {
119
- firstArg := extractFirstPathArg ()
120
+ l .viper .SetConfigName (".golangci" )
121
+
122
+ configSearchPaths := l .getConfigSearchPaths ()
123
+
124
+ l .log .Infof ("Config search paths: %s" , configSearchPaths )
125
+
126
+ for _ , p := range configSearchPaths {
127
+ l .viper .AddConfigPath (p )
128
+ }
129
+ }
130
+
131
+ func (l * Loader ) getConfigSearchPaths () []string {
132
+ firstArg := "./..."
133
+ if len (l .args ) > 0 {
134
+ firstArg = l .args [0 ]
135
+ }
120
136
121
- absStartPath , err := filepath .Abs (firstArg )
137
+ absPath , err := filepath .Abs (firstArg )
122
138
if err != nil {
123
139
l .log .Warnf ("Can't make abs path for %q: %s" , firstArg , err )
124
- absStartPath = filepath .Clean (firstArg )
140
+ absPath = filepath .Clean (firstArg )
125
141
}
126
142
127
143
// start from it
128
- var curDir string
129
- if fsutils .IsDir (absStartPath ) {
130
- curDir = absStartPath
144
+ var currentDir string
145
+ if fsutils .IsDir (absPath ) {
146
+ currentDir = absPath
131
147
} else {
132
- curDir = filepath .Dir (absStartPath )
148
+ currentDir = filepath .Dir (absPath )
133
149
}
134
150
135
151
// find all dirs from it up to the root
136
- configSearchPaths := []string {"./" }
152
+ searchPaths := []string {"./" }
137
153
138
154
for {
139
- configSearchPaths = append (configSearchPaths , curDir )
155
+ searchPaths = append (searchPaths , currentDir )
140
156
141
- newCurDir := filepath .Dir (curDir )
142
- if curDir == newCurDir || newCurDir == "" {
157
+ parent := filepath .Dir (currentDir )
158
+ if currentDir == parent || parent == "" {
143
159
break
144
160
}
145
161
146
- curDir = newCurDir
162
+ currentDir = parent
147
163
}
148
164
149
165
// find home directory for global config
150
166
if home , err := homedir .Dir (); err != nil {
151
- l .log .Warnf ("Can't get user's home directory: %s " , err . Error () )
152
- } else if ! slices .Contains (configSearchPaths , home ) {
153
- configSearchPaths = append (configSearchPaths , home )
167
+ l .log .Warnf ("Can't get user's home directory: %v " , err )
168
+ } else if ! slices .Contains (searchPaths , home ) {
169
+ searchPaths = append (searchPaths , home )
154
170
}
155
171
156
- l .log .Infof ("Config search paths: %s" , configSearchPaths )
157
-
158
- l .viper .SetConfigName (".golangci" )
159
-
160
- for _ , p := range configSearchPaths {
161
- l .viper .AddConfigPath (p )
162
- }
172
+ return searchPaths
163
173
}
164
174
165
175
func (l * Loader ) parseConfig () error {
@@ -416,28 +426,3 @@ func customDecoderHook() viper.DecoderConfigOption {
416
426
mapstructure .TextUnmarshallerHookFunc (),
417
427
))
418
428
}
419
-
420
- func extractFirstPathArg () string {
421
- args := os .Args
422
-
423
- // skip all args ([golangci-lint, run/linters]) before files/dirs list
424
- for len (args ) != 0 {
425
- if args [0 ] == "run" {
426
- args = args [1 :]
427
- break
428
- }
429
-
430
- args = args [1 :]
431
- }
432
-
433
- // find first file/dir arg
434
- firstArg := "./..."
435
- for _ , arg := range args {
436
- if ! strings .HasPrefix (arg , "-" ) {
437
- firstArg = arg
438
- break
439
- }
440
- }
441
-
442
- return firstArg
443
- }
0 commit comments