Skip to content

Commit 6fa025a

Browse files
committed
feat: example of the new plugin signature
1 parent 0042ea6 commit 6fa025a

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

.golangci.example.yml

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ linters-settings:
77
description: The description of the linter. This is optional, but shows up when running `golangci-lint linters`.
88
# Original-url is optional, and is only used for documentation purposes.
99
original-url: github.com/golangci/example-linter
10+
settings:
11+
one: Foo
12+
two:
13+
- name: Bar
14+
three:
15+
name: Bar
1016

1117
linters:
1218
disable-all: true

README.md

+28-19
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,57 @@ If you're looking for instructions on how to configure your own custom linter, t
2828

2929
1. If the project you want to lint does not have one already, copy the [.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml) to the root directory.
3030
2. Adjust the YAML to appropriate `linters-settings.custom` entries as so:
31-
```yml
31+
```yaml
3232
linters-settings:
33-
custom:
34-
example:
35-
path: /example.so
36-
description: The description of the linter
37-
original-url: github.com/golangci/example-linter
33+
custom:
34+
example:
35+
path: /example.so
36+
description: The description of the linter
37+
original-url: github.com/golangci/example-linter
38+
settings: # Settings are optional.
39+
one: Foo
40+
two:
41+
- name: Bar
42+
three:
43+
name: Bar
3844
```
3945
4046
That is all the configuration that is required to run a custom linter in your project.
4147
4248
Custom linters are enabled by default, but abide by the same rules as other linters.
4349
4450
If the disable all option is specified either on command line or in `.golang.yml` files `linters.disable-all: true`, custom linters will be disabled;
45-
they can be re-enabled by adding them to the `linters:enable` list,
51+
they can be re-enabled by adding them to the `linters.enable` list,
4652
or providing the enabled option on the command line, `golangci-lint run -Eexample`.
4753

48-
### To Create Your Own Custom Linter
54+
The configuration inside the `settings` field of linter have some limitations (there are NOT related to the plugin system itself):
55+
we use Viper to handle the configuration but Viper put all the keys in lowercase, and `.` cannot be used inside a key.
4956

50-
Your linter must implement one or more `golang.org/x/tools/go/analysis.Analyzer` structs.
57+
### To Create Your Own Plugin
58+
59+
Your linter must provide one or more `golang.org/x/tools/go/analysis.Analyzer` structs.
5160

5261
Your project should also use `go.mod`.
5362

5463
All versions of libraries that overlap `golangci-lint` (including replaced libraries) MUST be set to the same version as `golangci-lint`.
5564
You can see the versions by running `go version -m golangci-lint`.
5665

57-
You'll also need to create a go file like `plugin/example.go`.
58-
59-
This MUST be in the package `main`, and define a variable of name `AnalyzerPlugin`.
60-
The `AnalyzerPlugin` instance MUST implement the following interface:
66+
You'll also need to create a Go file like `plugin/example.go`.
6167

68+
This file MUST be in the package `main`, and MUST define an exposed function called `New` with the following signature:
6269
```go
63-
type AnalyzerPlugin interface {
64-
GetAnalyzers() []*analysis.Analyzer
70+
func New(conf any) ([]*analysis.Analyzer, error) {
71+
// ...
6572
}
6673
```
6774

68-
The type of `AnalyzerPlugin` is not important, but is by convention `type analyzerPlugin struct {}`.
69-
See [plugin/example.go](https://github.com/golangci/example-plugin-linter/plugin/example.go) for more info.
75+
See [plugin/example.go](https://github.com/golangci/example-plugin-linter/blob/master/plugin/example.go) for more info.
7076

71-
To build the plugin, from the root project directory, run `go build -buildmode=plugin plugin/example.go`.
77+
To build the plugin, from the root project directory, run:
78+
```bash
79+
go build -buildmode=plugin plugin/example.go
80+
```
7281

73-
This will create a plugin `*.so` file that can be copied into your project or another well known location for usage in golangci-lint.
82+
This will create a plugin `*.so` file that can be copied into your project or another well known location for usage in `golangci-lint`.
7483

7584
[^1]: Alternately, you can use the `-o /path/to/location/example.so` output flag to have it put it there for you.

plugin/example.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
package main
33

44
import (
5+
"fmt"
6+
57
linters "github.com/golangci/example-linter"
68
"golang.org/x/tools/go/analysis"
79
)
810

9-
// This must be defined and named 'AnalyzerPlugin'
10-
var AnalyzerPlugin analyzerPlugin
11+
func New(conf any) ([]*analysis.Analyzer, error) {
12+
// TODO: This must be implemented
13+
14+
fmt.Printf("My configuration (%[1]T): %#[1]v\n", conf)
1115

12-
type analyzerPlugin struct{}
16+
// The configuration type will be map[string]any or []interface, it depends on your configuration.
17+
// You can use https://github.com/mitchellh/mapstructure to convert map to struct.
1318

14-
// This must be implemented
15-
func (*analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
16-
return []*analysis.Analyzer{linters.TodoAnalyzer}
19+
return []*analysis.Analyzer{linters.TodoAnalyzer}, nil
1720
}

0 commit comments

Comments
 (0)