go-linters

module
v0.0.0-...-224f9e4 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2021 License: Apache-2.0

README

go-linters

PlanetScale's Go linters.

Use as standalone binaries or golangci-lint plugins.

Usage

Due to the way golangci-lint plugins work it is not possible to use plugins with the pre-built binaries. Instead, a CGO-enabled version of golangci-lint must be used and the plugins must be compiled with the same version of all external dependencies.

To ease the use of these plugins this repo provides a set of Makefile tasks to include in your project. The tasks handle compiling a CGO-enabled version of golangci-lint and the plugins in the repo.

Adding to your project
  • Add to Makefile:
-include $(shell test -e .go-linters.include.mk || curl -sSL -o .go-linters.include.mk https://raw.githubusercontent.com/planetscale/go-linters/main/go-linters.include.mk; echo .go-linters.include.mk)

This will add a new set of make targets. Run make go-linters/help for a description of each. Run make go-linters/clean to reset.

  • Add to .golangci.yml:
linters-settings:
  custom:
    logkeycheck:
      path: .go-linters/logkeycheck.so
    todoauthor:
      path: .go-linters/todoauthor.so

linters:
  enable:
    - logkeycheck
    - todoauthor
  • Add to.gitignore:
.go-linters.include.mk
.go-linters
Running linters
  • The simplest way to execute the linters is with the go-linters/run task, eg:
make go-linters/run
  • Add a Makefile task lint:
lint: go-linters/run
  • Alternatively, call $(GOLANGCI_LINT_BIN) with custom flags and go-linters/install as a pre-requisite, eg:
lint: go-linters/install
  $(GOLANGCI_LINT_BIN) run -v ./...

Advanced / Alternative Usage

In addition to being provided as plugins each linter is available as a standalone binary or all together in an omnibus binary.

Install all-linters binary:

go install github.com/planetscale/go-linters/cmd/all-linters@main

Install individual linters:

go install github.com/planetscale/go-linters/cmd/logkeycheck@main
go install github.com/planetscale/go-linters/cmd/todoauthor@main

The omnibus all-linters binary will run all linters by default or specific linters with the flag -<LINTER>:

all-linters main.go              # runs all linters
all-linters -logkeycheck main.go # runs logkeycheck only
all-linters -todoauthor main.go  # runs logkeycheck only

Contributing

New linters
  1. Implement linter: ./pkg/{{LINTER_NAME}}/analyzer.go
  2. Write unit tests: ./pkg/{{LINTER_NAME}}/analyzer_test.go.
  3. Create cmd/{{LINTER_NAME}}/main.go which will be used to generate a standalone linter binary (./bin/{{LINTER_NAME}}).
  4. Add your linter to cmd/all-linters/main.go which will add it the omnibus binary (./bin/all-linters). Your linter will be run with all linters when the binary is executed, or individually when the -{{LINTER_NAME}} flag is passed.
Testing
  • make lint
  • make test

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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