Skip to content

Commit 47440bc

Browse files
committed
don't print config parsing info logs twice
1 parent a24cc87 commit 47440bc

File tree

10 files changed

+110
-79
lines changed

10 files changed

+110
-79
lines changed

pkg/commands/executor.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import (
66
"github.com/golangci/golangci-lint/pkg/logutils"
77
"github.com/golangci/golangci-lint/pkg/report"
88
"github.com/spf13/cobra"
9+
"github.com/spf13/pflag"
910
)
1011

1112
type Executor struct {
1213
rootCmd *cobra.Command
14+
runCmd *cobra.Command
1315

1416
exitCode int
1517
version, commit, date string
@@ -23,22 +25,47 @@ type Executor struct {
2325

2426
func NewExecutor(version, commit, date string) *Executor {
2527
e := &Executor{
26-
cfg: config.NewDefault(),
27-
version: version,
28-
commit: commit,
29-
date: date,
28+
cfg: config.NewDefault(),
29+
version: version,
30+
commit: commit,
31+
date: date,
32+
DBManager: lintersdb.NewManager(),
3033
}
3134

3235
e.log = report.NewLogWrapper(logutils.NewStderrLog(""), &e.reportData)
33-
e.DBManager = lintersdb.NewManager()
34-
e.EnabledLintersSet = lintersdb.NewEnabledSet(e.DBManager, &lintersdb.Validator{},
35-
e.log.Child("lintersdb"), e.cfg)
3636

37+
// to setup log level early we need to parse config from command line extra time to
38+
// find `-v` option
39+
commandLineCfg, err := e.getConfigForCommandLine()
40+
if err != nil && err != pflag.ErrHelp {
41+
e.log.Fatalf("Can't get config for command line: %s", err)
42+
}
43+
if commandLineCfg != nil {
44+
logutils.SetupVerboseLog(e.log, commandLineCfg.Run.IsVerbose)
45+
}
46+
47+
// init of commands must be done before config file reading because
48+
// init sets config with the default values of flags
3749
e.initRoot()
3850
e.initRun()
3951
e.initHelp()
4052
e.initLinters()
4153

54+
// init e.cfg by values from config: flags parse will see these values
55+
// like the default ones. It will overwrite them only if the same option
56+
// is found in command-line: it's ok, command-line has higher priority.
57+
58+
r := config.NewFileReader(e.cfg, commandLineCfg, e.log.Child("config_reader"))
59+
if err := r.Read(); err != nil {
60+
e.log.Fatalf("Can't read config: %s", err)
61+
}
62+
63+
// Slice options must be explicitly set for proper merging of config and command-line options.
64+
fixSlicesFlags(e.runCmd.Flags())
65+
66+
e.EnabledLintersSet = lintersdb.NewEnabledSet(e.DBManager,
67+
lintersdb.NewValidator(e.DBManager), e.log.Child("lintersdb"), e.cfg)
68+
4269
return e
4370
}
4471

pkg/commands/linters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func IsLinterInConfigsList(name string, linters []linter.Config) bool {
2929
return false
3030
}
3131

32-
func (e Executor) executeLinters(cmd *cobra.Command, args []string) {
32+
func (e *Executor) executeLinters(cmd *cobra.Command, args []string) {
3333
enabledLCs, err := e.EnabledLintersSet.Get()
3434
if err != nil {
3535
log.Fatalf("Can't get enabled linters: %s", err)

pkg/commands/root.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ func (e *Executor) persistentPreRun(cmd *cobra.Command, args []string) {
2020

2121
runtime.GOMAXPROCS(e.cfg.Run.Concurrency)
2222

23-
logutils.SetupVerboseLog(e.log, e.cfg.Run.IsVerbose)
24-
2523
if e.cfg.Run.CPUProfilePath != "" {
2624
f, err := os.Create(e.cfg.Run.CPUProfilePath)
2725
if err != nil {

pkg/commands/run.go

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,42 +168,48 @@ func (e *Executor) initRunConfiguration(cmd *cobra.Command) {
168168
fs := cmd.Flags()
169169
fs.SortFlags = false // sort them as they are defined here
170170
initFlagSet(fs, e.cfg, e.DBManager)
171+
}
171172

172-
// init e.cfg by values from config: flags parse will see these values
173-
// like the default ones. It will overwrite them only if the same option
174-
// is found in command-line: it's ok, command-line has higher priority.
175-
176-
r := config.NewFileReader(e.cfg, e.log.Child("config_reader"), func(fs *pflag.FlagSet, cfg *config.Config) {
177-
// Don't do `fs.AddFlagSet(cmd.Flags())` because it shares flags representations:
178-
// `changed` variable inside string slice vars will be shared.
179-
// Use another config variable here, not e.cfg, to not
180-
// affect main parsing by this parsing of only config option.
181-
initFlagSet(fs, cfg, e.DBManager)
182-
183-
// Parse max options, even force version option: don't want
184-
// to get access to Executor here: it's error-prone to use
185-
// cfg vs e.cfg.
186-
initRootFlagSet(fs, cfg, true)
187-
})
188-
if err := r.Read(); err != nil {
189-
e.log.Fatalf("Can't read config: %s", err)
173+
func (e Executor) getConfigForCommandLine() (*config.Config, error) {
174+
// We use another pflag.FlagSet here to not set `changed` flag
175+
// on cmd.Flags() options. Otherwise string slice options will be duplicated.
176+
fs := pflag.NewFlagSet("config flag set", pflag.ContinueOnError)
177+
178+
var cfg config.Config
179+
// Don't do `fs.AddFlagSet(cmd.Flags())` because it shares flags representations:
180+
// `changed` variable inside string slice vars will be shared.
181+
// Use another config variable here, not e.cfg, to not
182+
// affect main parsing by this parsing of only config option.
183+
initFlagSet(fs, &cfg, e.DBManager)
184+
185+
// Parse max options, even force version option: don't want
186+
// to get access to Executor here: it's error-prone to use
187+
// cfg vs e.cfg.
188+
initRootFlagSet(fs, &cfg, true)
189+
190+
fs.Usage = func() {} // otherwise help text will be printed twice
191+
if err := fs.Parse(os.Args); err != nil {
192+
if err == pflag.ErrHelp {
193+
return nil, err
194+
}
195+
196+
return nil, fmt.Errorf("can't parse args: %s", err)
190197
}
191198

192-
// Slice options must be explicitly set for proper merging of config and command-line options.
193-
fixSlicesFlags(fs)
199+
return &cfg, nil
194200
}
195201

196202
func (e *Executor) initRun() {
197-
var runCmd = &cobra.Command{
203+
e.runCmd = &cobra.Command{
198204
Use: "run",
199205
Short: welcomeMessage,
200206
Run: e.executeRun,
201207
}
202-
e.rootCmd.AddCommand(runCmd)
208+
e.rootCmd.AddCommand(e.runCmd)
203209

204-
runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals
210+
e.runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals
205211

206-
e.initRunConfiguration(runCmd)
212+
e.initRunConfiguration(e.runCmd)
207213
}
208214

209215
func fixSlicesFlags(fs *pflag.FlagSet) {

pkg/config/reader.go

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"strings"
89

910
"github.com/golangci/golangci-lint/pkg/fsutils"
1011
"github.com/golangci/golangci-lint/pkg/logutils"
11-
"github.com/spf13/pflag"
1212
"github.com/spf13/viper"
1313
)
1414

15-
type FlagSetInit func(fs *pflag.FlagSet, cfg *Config)
16-
1715
type FileReader struct {
18-
log logutils.Log
19-
cfg *Config
20-
flagSetInit FlagSetInit
16+
log logutils.Log
17+
cfg *Config
18+
commandLineCfg *Config
2119
}
2220

23-
func NewFileReader(toCfg *Config, log logutils.Log, flagSetInit FlagSetInit) *FileReader {
21+
func NewFileReader(toCfg, commandLineCfg *Config, log logutils.Log) *FileReader {
2422
return &FileReader{
25-
log: log,
26-
cfg: toCfg,
27-
flagSetInit: flagSetInit,
23+
log: log,
24+
cfg: toCfg,
25+
commandLineCfg: commandLineCfg,
2826
}
2927
}
3028

@@ -33,9 +31,9 @@ func (r *FileReader) Read() error {
3331
// 1. to access "config" option here.
3432
// 2. to give config less priority than command line.
3533

36-
configFile, restArgs, err := r.parseConfigOption()
34+
configFile, err := r.parseConfigOption()
3735
if err != nil {
38-
if err == errConfigDisabled || err == pflag.ErrHelp {
36+
if err == errConfigDisabled {
3937
return nil
4038
}
4139

@@ -45,7 +43,7 @@ func (r *FileReader) Read() error {
4543
if configFile != "" {
4644
viper.SetConfigFile(configFile)
4745
} else {
48-
r.setupConfigFileSearch(restArgs)
46+
r.setupConfigFileSearch()
4947
}
5048

5149
return r.parseConfig()
@@ -108,7 +106,9 @@ func (r *FileReader) validateConfig() error {
108106
return nil
109107
}
110108

111-
func (r *FileReader) setupConfigFileSearch(args []string) {
109+
func getFirstPathArg() string {
110+
args := os.Args
111+
112112
// skip all args ([golangci-lint, run/linters]) before files/dirs list
113113
for len(args) != 0 {
114114
if args[0] == "run" {
@@ -121,10 +121,18 @@ func (r *FileReader) setupConfigFileSearch(args []string) {
121121

122122
// find first file/dir arg
123123
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+
}
126129
}
127130

131+
return firstArg
132+
}
133+
134+
func (r *FileReader) setupConfigFileSearch() {
135+
firstArg := getFirstPathArg()
128136
absStartPath, err := filepath.Abs(firstArg)
129137
if err != nil {
130138
r.log.Warnf("Can't make abs path for %q: %s", firstArg, err)
@@ -159,34 +167,20 @@ func (r *FileReader) setupConfigFileSearch(args []string) {
159167

160168
var errConfigDisabled = errors.New("config is disabled by --no-config")
161169

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
177174
}
178175

179-
// for `-v` to work until running of preRun function
180-
logutils.SetupVerboseLog(r.log, cfg.Run.IsVerbose)
181-
182176
configFile := cfg.Run.Config
183177
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")
185179
}
186180

187181
if cfg.Run.NoConfig {
188-
return "", nil, errConfigDisabled
182+
return "", errConfigDisabled
189183
}
190184

191-
return configFile, fs.Args(), nil
185+
return configFile, nil
192186
}

pkg/lint/lintersdb/enabled_set.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ func (es EnabledSet) Get() ([]linter.Config, error) {
129129
}
130130

131131
es.verbosePrintLintersStatus(resultLinters)
132-
133132
return resultLinters, nil
134133
}
135134

pkg/lint/lintersdb/enabled_set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestGetEnabledLintersSet(t *testing.T) {
4545
}
4646

4747
m := NewManager()
48-
es := NewEnabledSet(m, &Validator{}, nil, nil)
48+
es := NewEnabledSet(m, NewValidator(m), nil, nil)
4949
for _, c := range cases {
5050
t.Run(c.name, func(t *testing.T) {
5151
defaultLinters := []linter.Config{}

pkg/lint/lintersdb/validator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type Validator struct {
1111
m *Manager
1212
}
1313

14+
func NewValidator(m *Manager) *Validator {
15+
return &Validator{
16+
m: m,
17+
}
18+
}
19+
1420
func (v Validator) validateLintersNames(cfg *config.Linters) error {
1521
allNames := append([]string{}, cfg.Enable...)
1622
allNames = append(allNames, cfg.Disable...)

scripts/gen_readme/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func buildTemplateContext() (map[string]interface{}, error) {
8989

9090
func getLintersListMarkdown(enabled bool) string {
9191
var neededLcs []linter.Config
92-
lcs := lintersdb.GetAllSupportedLinterConfigs()
92+
lcs := lintersdb.NewManager().GetAllSupportedLinterConfigs()
9393
for _, lc := range lcs {
9494
if lc.EnabledByDefault == enabled {
9595
neededLcs = append(neededLcs, lc)
@@ -114,7 +114,7 @@ func getLintersListMarkdown(enabled bool) string {
114114
func getThanksList() string {
115115
var lines []string
116116
addedAuthors := map[string]bool{}
117-
for _, lc := range lintersdb.GetAllSupportedLinterConfigs() {
117+
for _, lc := range lintersdb.NewManager().GetAllSupportedLinterConfigs() {
118118
if lc.OriginalURL == "" {
119119
continue
120120
}

test/run_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ func inSlice(s []string, v string) bool {
170170
}
171171

172172
func getEnabledByDefaultFastLintersExcept(except ...string) []string {
173-
ebdl := lintersdb.GetAllEnabledByDefaultLinters()
173+
m := lintersdb.NewManager()
174+
ebdl := m.GetAllEnabledByDefaultLinters()
174175
ret := []string{}
175176
for _, linter := range ebdl {
176177
if linter.DoesFullImport {
@@ -186,7 +187,7 @@ func getEnabledByDefaultFastLintersExcept(except ...string) []string {
186187
}
187188

188189
func getAllFastLintersWith(with ...string) []string {
189-
linters := lintersdb.GetAllSupportedLinterConfigs()
190+
linters := lintersdb.NewManager().GetAllSupportedLinterConfigs()
190191
ret := append([]string{}, with...)
191192
for _, linter := range linters {
192193
if linter.DoesFullImport {
@@ -199,7 +200,7 @@ func getAllFastLintersWith(with ...string) []string {
199200
}
200201

201202
func getEnabledByDefaultLinters() []string {
202-
ebdl := lintersdb.GetAllEnabledByDefaultLinters()
203+
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
203204
ret := []string{}
204205
for _, linter := range ebdl {
205206
ret = append(ret, linter.Linter.Name())
@@ -209,7 +210,7 @@ func getEnabledByDefaultLinters() []string {
209210
}
210211

211212
func getEnabledByDefaultFastLintersWith(with ...string) []string {
212-
ebdl := lintersdb.GetAllEnabledByDefaultLinters()
213+
ebdl := lintersdb.NewManager().GetAllEnabledByDefaultLinters()
213214
ret := append([]string{}, with...)
214215
for _, linter := range ebdl {
215216
if linter.DoesFullImport {

0 commit comments

Comments
 (0)