Skip to content

Commit

Permalink
Expose consts to make them easily reusable by linters runners
Browse files Browse the repository at this point in the history
  • Loading branch information
aziule committed Aug 1, 2020
1 parent 2ed125c commit a66cb42
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions filebuildtag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ import (
"golang.org/x/tools/go/ast/inspector"
)

// Doc of the linter.
const Doc = `check that Go files have the expected build tags in the "// +build" instruction
const (
// Doc of the linter.
Doc = `check that Go files have the expected build tags in the "// +build" instruction
Bind file patterns to build tags, for instance:
File named "bar.go" must have the "baz" build tag
Files matching "*_integration_test.go" must have the "integration" build tag`
// FlagFiletagsName is the name of the default filetags flag. It is exported to be reused from linters runners.
FlagFiletagsName = "filetags"
// FlagFiletagsDoc is the usage doc of the default filetags flag. It is exported to be reused from linters runners.
FlagFiletagsDoc = `Comma separated list of file names and build tags using the form "pattern:tag". For example:
- Single file: "*foo.go:tag1"
- Multiple files: "*foo.go:tag1,*foo2.go:tag2"`
)

var Analyzer = &analysis.Analyzer{
Name: "filebuildtag",
Expand All @@ -29,15 +37,9 @@ var Analyzer = &analysis.Analyzer{
Requires: []*analysis.Analyzer{inspect.Analyzer},
}

const flagFiletagsDoc = `Comma separated list of file names and build tags using the form "pattern:tag". For example:
- Single file: "*foo.go:tag1"
- Multiple files: "*foo.go:tag1,*foo2.go:tag2"`

const flagFiletagsName = "filetags"

func flags() flag.FlagSet {
fs := flag.NewFlagSet("", flag.ExitOnError)
fs.String(flagFiletagsName, "", flagFiletagsDoc)
fs.String(FlagFiletagsName, "", FlagFiletagsDoc)
return *fs
}

Expand Down Expand Up @@ -79,7 +81,7 @@ func run(pass *analysis.Pass) (interface{}, error) {

func parseFlags(flags flag.FlagSet) (map[string]string, error) {
filetags := make(map[string]string)
f := flags.Lookup(flagFiletagsName)
f := flags.Lookup(FlagFiletagsName)
if f == nil {
return filetags, nil
}
Expand Down
2 changes: 1 addition & 1 deletion filebuildtag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func Test_parseFlags(t *testing.T) {

func newFlagSet(t *testing.T, args string) flag.FlagSet {
fs := flags()
err := fs.Set(flagFiletagsName, args)
err := fs.Set(FlagFiletagsName, args)
require.NoError(t, err)
return fs
}

0 comments on commit a66cb42

Please sign in to comment.