Private linter for Telegraf (#9168)

* Custom linter

* Telegraf private linter

Co-authored-by: Bas <3441183+BattleBas@users.noreply.github.com>
This commit is contained in:
Sebastian Spaink 2021-04-26 16:12:58 -05:00 committed by GitHub
parent e058f3641c
commit 598990b469
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 3 deletions

View File

@ -1,6 +1,7 @@
linters:
disable-all: true
enable:
# - telegraflinter
- bodyclose
- dogsled
- errcheck
@ -20,6 +21,10 @@ linters:
- varcheck
linters-settings:
# custom:
# telegraflinter:
# path: telegraflinter.so
# description: "Find Telegraf specific review criteria, more info: https://github.com/influxdata/telegraf/wiki/Review"
revive:
rules:
- name: argument-limit

3
go.mod
View File

@ -130,8 +130,9 @@ require (
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4
golang.org/x/text v0.3.4
golang.org/x/tools v0.1.0
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200205215550-e35592f146e4
google.golang.org/api v0.29.0
google.golang.org/genproto v0.0.0-20200815001618-f69a88009b70

6
go.sum
View File

@ -1360,8 +1360,9 @@ golang.org/x/sys v0.0.0-20200821140526-fda516888d29/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
@ -1438,8 +1439,9 @@ golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjs
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/tools v0.0.0-20200513201620-d5fe73897c97/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200822203824-307de81be3f4/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9 h1:sEvmEcJVKBNUvgCUClbUQeHOAa9U0I2Ce1BooMvVCY4=
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

31
telegraflinter/README.md Normal file
View File

@ -0,0 +1,31 @@
# Private linter for Telegraf
The purpose of this linter is to enforce the review criteria for the Telegraf project, outlined here: https://github.com/influxdata/telegraf/wiki/Review. This is currently not compatible with the linter running in the CI and can only be ran locally.
## Running it locally
To use the Telegraf linter, you need a binary of golangci-lint that was compiled with CGO enabled. Currently no release is provided with it enabled, therefore you will need to clone the source code and compile it yourself. You can run the following commands to achieve this:
1. `git clone https://github.com/sspaink/golangci-lint.git`
2. `cd golangci-lint`
3. `git checkout tags/v1.39.0 -b 1390`
4. `CGO_ENABLED=true go build -o golangci-lint-cgo ./cmd/golangci-lint`
You will now have the binary you need to run the Telegraf linter. The Telegraf linter will now need to be compiled as a plugin to get a *.so file. [Currently plugins are only supported on Linux, FreeBSD, and macOS](https://golang.org/pkg/plugin/). From the root of the Telegraf project, you can run the following commands to compile the linter and run it:
1. `CGO_ENABLED=true go build -buildmode=plugin telegraflinter/telegraflinter.go`
2. In the .golanci-lint file:
* uncomment the `custom` section under the `linters-settings` section
* uncomment `telegraflinter` under the `enable` section
3. `golanci-lint-cgo run`
*Note:* If you made a change to the telegraf linter and want to run it again, be sure to clear the [cache directory](https://golang.org/pkg/os/#UserCacheDir). On unix systems you can run `rm -rf ~/.cache/golangci-lint` otherwise it will seem like nothing changed.
## Requirement
This linter lives in the Telegraf repository and is compiled to become a Go plugin, any packages used in the linter *MUST* match the version in the golanci-lint otherwise there will be issues. For example the import `golang.org/x/tools v0.1.0` needs to match what golangci-lint is using.
## Useful references
* https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint
* https://github.com/golangci/example-plugin-linter

View File

@ -0,0 +1,54 @@
// This must be package main
package main
import (
"go/ast"
"strings"
"golang.org/x/tools/go/analysis"
)
type analyzerPlugin struct{}
// This must be implemented
func (*analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
return []*analysis.Analyzer{
TelegrafAnalyzer,
}
}
// This must be defined and named 'AnalyzerPlugin'
var AnalyzerPlugin analyzerPlugin
var TelegrafAnalyzer = &analysis.Analyzer{
Name: "telegraflinter",
Doc: "Find Telegraf specific review criteria, more info: https://github.com/influxdata/telegraf/wiki/Review",
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
for _, file := range pass.Files {
ast.Inspect(file, func(n ast.Node) bool {
checkLogImport(n, pass)
return true
})
}
return nil, nil
}
func checkLogImport(n ast.Node, pass *analysis.Pass) {
if !strings.HasPrefix(pass.Pkg.Path(), "github.com/influxdata/telegraf/plugins/") {
return
}
if importSpec, ok := n.(*ast.ImportSpec); ok {
if importSpec.Path != nil && strings.HasPrefix(importSpec.Path.Value, "\"log\"") {
pass.Report(analysis.Diagnostic{
Pos: importSpec.Pos(),
End: 0,
Category: "log",
Message: "Don't use log package in plugin, use the Telegraf logger.",
SuggestedFixes: nil,
})
}
}
}