@@ -4,27 +4,55 @@ import (
44 "go/ast"
55 "go/types"
66
7+ "github.com/golangci/plugin-module-register/register"
78 "golang.org/x/tools/go/analysis"
89 "golang.org/x/tools/go/analysis/passes/inspect"
910 "golang.org/x/tools/go/ast/inspector"
1011)
1112
12- var checkPrivateReturnValues bool
13+ func init () {
14+ register .Plugin ("goworkflows" , New )
15+ }
1316
14- func New () * analysis. Analyzer {
15- a := & analysis. Analyzer {
16- Name : "goworkflows" ,
17- Doc : "Checks for common errors when writing workflows" ,
18- Run : run ,
19- Requires : [] * analysis. Analyzer { inspect . Analyzer },
17+ func New (settings any ) (register. LinterPlugin , error ) {
18+ // The configuration type will be map[string]any or []interface, it depends on your configuration.
19+ // You can use https://github.com/go-viper/mapstructure to convert map to struct.
20+ s , err := register. DecodeSettings [ Settings ]( settings )
21+ if err != nil {
22+ return nil , err
2023 }
2124
22- a .Flags .BoolVar (& checkPrivateReturnValues , "checkprivatereturnvalues" , false , "Check return values of workflows which aren't exported" )
25+ return & GoWorkflowsPlugin {Settings : s }, nil
26+ }
27+
28+ type GoWorkflowsPlugin struct {
29+ Settings Settings
30+ }
31+
32+ type Settings struct {
33+ CheckPrivateReturnValues bool `json:"checkprivatereturnvalues"`
34+ }
35+
36+ func (w * GoWorkflowsPlugin ) BuildAnalyzers () ([]* analysis.Analyzer , error ) {
37+ return []* analysis.Analyzer {
38+ {
39+ Name : "goworkflows" ,
40+ Doc : "Checks for common errors when writing workflows" ,
41+ Run : w .run ,
42+ Requires : []* analysis.Analyzer {inspect .Analyzer },
43+ },
44+ }, nil
45+ }
46+
47+ func (w * GoWorkflowsPlugin ) GetLoadMode () string {
48+ // NOTE: the mode can be `register.LoadModeSyntax` or `register.LoadModeTypesInfo`.
49+ // - `register.LoadModeSyntax`: if the linter doesn't use types information.
50+ // - `register.LoadModeTypesInfo`: if the linter uses types information.
2351
24- return a
52+ return register . LoadModeSyntax
2553}
2654
27- func run (pass * analysis.Pass ) (interface {}, error ) {
55+ func ( w * GoWorkflowsPlugin ) run (pass * analysis.Pass ) (interface {}, error ) {
2856 inspector := pass .ResultOf [inspect .Analyzer ].(* inspector.Inspector )
2957
3058 // Expect workflows to be top level functions in a file. Therefore it should be enough to just keep track if the current
@@ -84,7 +112,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
84112 inWorkflow = true
85113
86114 // Check return types
87- if n .Name .IsExported () || checkPrivateReturnValues {
115+ if n .Name .IsExported () || w . Settings . CheckPrivateReturnValues {
88116 if n .Type .Results == nil || len (n .Type .Results .List ) == 0 {
89117 pass .Reportf (n .Pos (), "workflow `%v` doesn't return anything. needs to return at least `error`" , n .Name .Name )
90118 } else {
0 commit comments