Skip to content

Commit

Permalink
Merge pull request #187 from kisielk/build-tags
Browse files Browse the repository at this point in the history
Support comma-separated build tags
  • Loading branch information
kisielk committed Aug 4, 2020
2 parents cea6859 + 2bd2f2e commit 3b58ac7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/errcheck/errcheck.go
Expand Up @@ -177,7 +177,7 @@ func (c *Checker) load(paths ...string) ([]*packages.Package, error) {
cfg := &packages.Config{
Mode: packages.LoadAllSyntax,
Tests: !c.WithoutTests,
BuildFlags: []string{fmt.Sprintf("-tags=%s", strings.Join(c.Tags, " "))},
BuildFlags: []string{fmtTags(c.Tags)},
}
return loadPackages(cfg, paths...)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/errcheck/tags.go
@@ -0,0 +1,12 @@
// +build go1.13

package errcheck

import (
"fmt"
"strings"
)

func fmtTags(tags []string) string {
return fmt.Sprintf("-tags=%s", strings.Join(tags, ","))
}
13 changes: 13 additions & 0 deletions internal/errcheck/tags_compat.go
@@ -0,0 +1,13 @@
// +build go1.11
// +build !go1.13

package errcheck

import (
"fmt"
"strings"
)

func fmtTags(tags []string) string {
return fmt.Sprintf("-tags=%s", strings.Join(tags, " "))
}
11 changes: 5 additions & 6 deletions main.go
Expand Up @@ -62,17 +62,16 @@ func (f ignoreFlag) Set(s string) error {
type tagsFlag []string

func (f *tagsFlag) String() string {
return fmt.Sprintf("%q", strings.Join(*f, " "))
return fmt.Sprintf("%q", strings.Join(*f, ","))
}

func (f *tagsFlag) Set(s string) error {
if s == "" {
return nil
}
tags := strings.Split(s, " ")
if tags == nil {
return nil
}
tags := strings.FieldsFunc(s, func(c rune) bool {
return c == ' ' || c == ','
})
for _, tag := range tags {
if tag != "" {
*f = append(*f, tag)
Expand Down Expand Up @@ -137,7 +136,7 @@ func parseFlags(checker *errcheck.Checker, args []string) ([]string, int) {
flags.BoolVar(&abspath, "abspath", false, "print absolute paths to files")

tags := tagsFlag{}
flags.Var(&tags, "tags", "space-separated list of build tags to include")
flags.Var(&tags, "tags", "comma or space-separated list of build tags to include")
ignorePkg := flags.String("ignorepkg", "", "comma-separated list of package paths to ignore")
ignore := ignoreFlag(map[string]*regexp.Regexp{})
flags.Var(ignore, "ignore", "[deprecated] comma-separated list of pairs of the form pkg:regex\n"+
Expand Down
9 changes: 9 additions & 0 deletions main_test.go
Expand Up @@ -163,6 +163,15 @@ func TestParseFlags(t *testing.T) {
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-tags", "foo,bar,!baz"},
paths: []string{"."},
ignore: map[string]string{},
tags: []string{"foo", "bar", "!baz"},
blank: false,
asserts: false,
error: exitCodeOk,
},
parseTestCase{
args: []string{"errcheck", "-tags", "foo bar !baz"},
paths: []string{"."},
Expand Down

0 comments on commit 3b58ac7

Please sign in to comment.