@@ -5,26 +5,24 @@ import (
5
5
"fmt"
6
6
"os"
7
7
"path/filepath"
8
+ "strings"
8
9
9
10
"github.com/golangci/golangci-lint/pkg/fsutils"
10
11
"github.com/golangci/golangci-lint/pkg/logutils"
11
- "github.com/spf13/pflag"
12
12
"github.com/spf13/viper"
13
13
)
14
14
15
- type FlagSetInit func (fs * pflag.FlagSet , cfg * Config )
16
-
17
15
type FileReader struct {
18
- log logutils.Log
19
- cfg * Config
20
- flagSetInit FlagSetInit
16
+ log logutils.Log
17
+ cfg * Config
18
+ commandLineCfg * Config
21
19
}
22
20
23
- func NewFileReader (toCfg * Config , log logutils.Log , flagSetInit FlagSetInit ) * FileReader {
21
+ func NewFileReader (toCfg , commandLineCfg * Config , log logutils.Log ) * FileReader {
24
22
return & FileReader {
25
- log : log ,
26
- cfg : toCfg ,
27
- flagSetInit : flagSetInit ,
23
+ log : log ,
24
+ cfg : toCfg ,
25
+ commandLineCfg : commandLineCfg ,
28
26
}
29
27
}
30
28
@@ -33,9 +31,9 @@ func (r *FileReader) Read() error {
33
31
// 1. to access "config" option here.
34
32
// 2. to give config less priority than command line.
35
33
36
- configFile , restArgs , err := r .parseConfigOption ()
34
+ configFile , err := r .parseConfigOption ()
37
35
if err != nil {
38
- if err == errConfigDisabled || err == pflag . ErrHelp {
36
+ if err == errConfigDisabled {
39
37
return nil
40
38
}
41
39
@@ -45,7 +43,7 @@ func (r *FileReader) Read() error {
45
43
if configFile != "" {
46
44
viper .SetConfigFile (configFile )
47
45
} else {
48
- r .setupConfigFileSearch (restArgs )
46
+ r .setupConfigFileSearch ()
49
47
}
50
48
51
49
return r .parseConfig ()
@@ -108,7 +106,9 @@ func (r *FileReader) validateConfig() error {
108
106
return nil
109
107
}
110
108
111
- func (r * FileReader ) setupConfigFileSearch (args []string ) {
109
+ func getFirstPathArg () string {
110
+ args := os .Args
111
+
112
112
// skip all args ([golangci-lint, run/linters]) before files/dirs list
113
113
for len (args ) != 0 {
114
114
if args [0 ] == "run" {
@@ -121,10 +121,18 @@ func (r *FileReader) setupConfigFileSearch(args []string) {
121
121
122
122
// find first file/dir arg
123
123
firstArg := "./..."
124
- if len (args ) != 0 {
125
- firstArg = args [0 ]
124
+ for _ , arg := range args {
125
+ if ! strings .HasPrefix (arg , "-" ) {
126
+ firstArg = arg
127
+ break
128
+ }
126
129
}
127
130
131
+ return firstArg
132
+ }
133
+
134
+ func (r * FileReader ) setupConfigFileSearch () {
135
+ firstArg := getFirstPathArg ()
128
136
absStartPath , err := filepath .Abs (firstArg )
129
137
if err != nil {
130
138
r .log .Warnf ("Can't make abs path for %q: %s" , firstArg , err )
@@ -159,34 +167,20 @@ func (r *FileReader) setupConfigFileSearch(args []string) {
159
167
160
168
var errConfigDisabled = errors .New ("config is disabled by --no-config" )
161
169
162
- func (r * FileReader ) parseConfigOption () (string , []string , error ) {
163
- // We use another pflag.FlagSet here to not set `changed` flag
164
- // on cmd.Flags() options. Otherwise string slice options will be duplicated.
165
- fs := pflag .NewFlagSet ("config flag set" , pflag .ContinueOnError )
166
-
167
- var cfg Config
168
- r .flagSetInit (fs , & cfg )
169
-
170
- fs .Usage = func () {} // otherwise help text will be printed twice
171
- if err := fs .Parse (os .Args ); err != nil {
172
- if err == pflag .ErrHelp {
173
- return "" , nil , err
174
- }
175
-
176
- return "" , nil , fmt .Errorf ("can't parse args: %s" , err )
170
+ func (r * FileReader ) parseConfigOption () (string , error ) {
171
+ cfg := r .commandLineCfg
172
+ if cfg == nil {
173
+ return "" , nil
177
174
}
178
175
179
- // for `-v` to work until running of preRun function
180
- logutils .SetupVerboseLog (r .log , cfg .Run .IsVerbose )
181
-
182
176
configFile := cfg .Run .Config
183
177
if cfg .Run .NoConfig && configFile != "" {
184
- return "" , nil , fmt .Errorf ("can't combine option --config and --no-config" )
178
+ return "" , fmt .Errorf ("can't combine option --config and --no-config" )
185
179
}
186
180
187
181
if cfg .Run .NoConfig {
188
- return "" , nil , errConfigDisabled
182
+ return "" , errConfigDisabled
189
183
}
190
184
191
- return configFile , fs . Args (), nil
185
+ return configFile , nil
192
186
}
0 commit comments