66 "os"
77 "path/filepath"
88 "slices"
9- "strings"
109
1110 "github.com/go-viper/mapstructure/v2"
1211 "github.com/mitchellh/go-homedir"
@@ -33,16 +32,18 @@ type Loader struct {
3332
3433 log logutils.Log
3534
36- cfg * Config
35+ cfg * Config
36+ args []string
3737}
3838
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 {
4040 return & Loader {
4141 opts : opts ,
4242 viper : v ,
4343 fs : fs ,
4444 log : log ,
4545 cfg : cfg ,
46+ args : args ,
4647 }
4748}
4849
@@ -116,50 +117,59 @@ func (l *Loader) evaluateOptions() (string, error) {
116117}
117118
118119func (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+ }
120136
121- absStartPath , err := filepath .Abs (firstArg )
137+ absPath , err := filepath .Abs (firstArg )
122138 if err != nil {
123139 l .log .Warnf ("Can't make abs path for %q: %s" , firstArg , err )
124- absStartPath = filepath .Clean (firstArg )
140+ absPath = filepath .Clean (firstArg )
125141 }
126142
127143 // 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
131147 } else {
132- curDir = filepath .Dir (absStartPath )
148+ currentDir = filepath .Dir (absPath )
133149 }
134150
135151 // find all dirs from it up to the root
136- configSearchPaths := []string {"./" }
152+ searchPaths := []string {"./" }
137153
138154 for {
139- configSearchPaths = append (configSearchPaths , curDir )
155+ searchPaths = append (searchPaths , currentDir )
140156
141- newCurDir := filepath .Dir (curDir )
142- if curDir == newCurDir || newCurDir == "" {
157+ parent := filepath .Dir (currentDir )
158+ if currentDir == parent || parent == "" {
143159 break
144160 }
145161
146- curDir = newCurDir
162+ currentDir = parent
147163 }
148164
149165 // find home directory for global config
150166 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 )
154170 }
155171
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
163173}
164174
165175func (l * Loader ) parseConfig () error {
@@ -416,28 +426,3 @@ func customDecoderHook() viper.DecoderConfigOption {
416426 mapstructure .TextUnmarshallerHookFunc (),
417427 ))
418428}
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