From 1fdcbddf13070afd8d7204948b03a7b87ffeff5f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 19 Apr 2021 03:19:13 +0200 Subject: [PATCH 1/2] fix: comma in exclude pattern leads to unexpected results --- pkg/commands/run.go | 18 +++++++++++++++++- pkg/config/reader.go | 2 +- pkg/lint/runner.go | 6 +++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index c255e24642a4..fc2bdb3a389d 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -313,8 +313,24 @@ func fixSlicesFlags(fs *pflag.FlagSet) { return } + // custom join to handle string with comma. + var g string + for i, v := range s { + if strings.Contains(v, ",") { + // add quotes to escape comma because spf13/pflag use a CSV parser: + // https://github.com/spf13/pflag/blob/85dd5c8bc61cfa382fecd072378089d4e856579d/string_slice.go#L43 + g += `"` + v + `"` + } else { + g += v + } + + if i < len(s)-1 { + g += "," + } + } + // calling Set sets Changed to true: next Set calls will append, not overwrite - _ = f.Value.Set(strings.Join(s, ",")) + _ = f.Value.Set(g) }) } diff --git a/pkg/config/reader.go b/pkg/config/reader.go index 00722ba6366d..6e97277daa2a 100644 --- a/pkg/config/reader.go +++ b/pkg/config/reader.go @@ -7,7 +7,7 @@ import ( "path/filepath" "strings" - homedir "github.com/mitchellh/go-homedir" + "github.com/mitchellh/go-homedir" "github.com/spf13/viper" "github.com/golangci/golangci-lint/pkg/fsutils" diff --git a/pkg/lint/runner.go b/pkg/lint/runner.go index 26611c05ae63..c492f558020f 100644 --- a/pkg/lint/runner.go +++ b/pkg/lint/runner.go @@ -237,9 +237,9 @@ func (r *Runner) processIssues(issues []result.Issue, sw *timeutils.Stopwatch, s func getExcludeProcessor(cfg *config.Issues) processors.Processor { var excludeTotalPattern string - excludeGlobalPatterns := cfg.ExcludePatterns - if len(excludeGlobalPatterns) != 0 { - excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludeGlobalPatterns, "|")) + + if len(cfg.ExcludePatterns) != 0 { + excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(cfg.ExcludePatterns, "|")) } var excludeProcessor processors.Processor From d9a5fa3d54c2c2dbcfa60533a2ba75dafe0c3530 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 19 Apr 2021 11:14:51 +0200 Subject: [PATCH 2/2] review --- pkg/commands/run.go | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/pkg/commands/run.go b/pkg/commands/run.go index fc2bdb3a389d..f5421e702eee 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -313,24 +313,15 @@ func fixSlicesFlags(fs *pflag.FlagSet) { return } - // custom join to handle string with comma. - var g string - for i, v := range s { - if strings.Contains(v, ",") { - // add quotes to escape comma because spf13/pflag use a CSV parser: - // https://github.com/spf13/pflag/blob/85dd5c8bc61cfa382fecd072378089d4e856579d/string_slice.go#L43 - g += `"` + v + `"` - } else { - g += v - } - - if i < len(s)-1 { - g += "," - } + var safe []string + for _, v := range s { + // add quotes to escape comma because spf13/pflag use a CSV parser: + // https://github.com/spf13/pflag/blob/85dd5c8bc61cfa382fecd072378089d4e856579d/string_slice.go#L43 + safe = append(safe, `"`+v+`"`) } // calling Set sets Changed to true: next Set calls will append, not overwrite - _ = f.Value.Set(g) + _ = f.Value.Set(strings.Join(safe, ",")) }) }