filebuildtag

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2020 License: MIT Imports: 9 Imported by: 0

README ยถ

filebuildtag

Linter that matches Go file naming patterns to their expected build tags.


GoDoc Go Report Card

Jump to Installation and usage

Benefits

Match file naming patterns to build tags and make sure these files always have the expected build tags in the // +build instruction.

Features

One-To-One match

Example: files named foo.go must include the bar build tag.

File: foo.go

// +build bar

package foo
Many-To-One match

Example: files ending with _suffix.go must include the bar build tag.

File: a_suffix.go

// +build bar

package foo

File: b_suffix.go

// +build bar

package foo
Go's buildtag linter support

Built on top of the buildtag linter, it supports its Go files features.

And also
  • Run it as a standalone command
  • Integrate it as a part of a runner using the provided analysis.Analyzer

Real world use case

Let's say you name your integration tests *_integration_test.go and you run them as part of your CI pipeline.

You might forget to add the // +build integration instruction on a newly created file, or you might remove it inadvertently, and it can have some consequences, such as never running during your pipeline.

As a consequence, you think your code works when it doesn't, because it is not tested (but you believe it is).

This linter can help with such issues and let you know when you forgot to add the expected build tags.

Installation and usage

Install with Go install

GO111MODULE=on go get github.com/aziule/filebuildtag/cmd/filebuildtag

Install and build from source

  1. Clone the repo
  2. Build the executable
make build

Usage

// All files named "foo.go" must have the "bar" tag
filebuildtag --filetags foo.go:bar ./...

// All files ending with "_integration_test.go" must have the "integration" tag
filebuildtag --filetags *_integration_test.go:integration ./...

// Both of the above
filebuildtag --filetags foo.go:bar,*_integration_test.go:integration ./...

// Only check that the `// +build` instructions are correct (no args to pass) 
filebuildtag ./...

Note: files naming patterns are matched using Go's filepath.Match method. Therefore, you can use any of its supported patterns. See File patterns for more information and examples.

Using with linters runners

This linter exposes an Analyzer (accessible via filebuildtag.Analyzer), which is defined as a golang.org/x/tools/go/analysis/analysis.Analyzer struct.

Most of the linters runners expect linters to be defined like so, therefore you should not have much trouble integrating it following the linters runner's doc.

File patterns

Syntax

From the official filepath.Match doc, file patterns syntax is the following:

pattern:
	{ term }
term:
	'*'         matches any sequence of non-Separator characters
	'?'         matches any single non-Separator character
	'[' [ '^' ] { character-range } ']'
	            character class (must be non-empty)
	c           matches character c (c != '*', '?', '\\', '[')
	'\\' c      matches character c

character-range:
	c           matches character c (c != '\\', '-', ']')
	'\\' c      matches character c
	lo '-' hi   matches character c for lo <= c <= hi
Examples
file ๐Ÿ‘‡ pattern ๐Ÿ‘‰ foo.go *.go ba?.go *_test.go
foo.go โœ… โœ… ๐Ÿšซ ๐Ÿšซ
bar.go ๐Ÿšซ โœ… โœ… ๐Ÿšซ
baz.go ๐Ÿšซ โœ… โœ… ๐Ÿšซ
a_test.go ๐Ÿšซ โœ… ๐Ÿšซ โœ…
something ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ ๐Ÿšซ

Roadmap

  • Support for folder name matching (/pkg/**/foo.go, /pkg/foo/*.go, etc.).

Contributing

A bug to report? A feature to add? Please feel free to open an issue or to propose pull requests!

License

Some of the code was copied from Go's buildtag linter and adapted to match the needs of the filebuildtag linter. Those files have the mandatory copyright header and their license can be found in LICENSE.google.

You can also find the original code here.

Documentation ยถ

Overview ยถ

Package filebuildtag exposes the necessary code to use the filebuildtag linter.

Index ยถ

Constants ยถ

View Source
const (
	// Doc of the linter.
	Doc = `` /* 257-byte string literal not displayed */

	// 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 = `` /* 169-byte string literal not displayed */

)

Variables ยถ

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "filebuildtag",
	Doc:      Doc,
	Flags:    flags(),
	Run:      run,
	Requires: []*analysis.Analyzer{inspect.Analyzer},
}

Functions ยถ

This section is empty.

Types ยถ

This section is empty.

Directories ยถ

Path Synopsis
cmd
Package internal contains code from the Go tool's linter, "buildtag".
Package internal contains code from the Go tool's linter, "buildtag".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL