Skip to content

Commit

Permalink
Merge pull request #8 from gostaticanalysis/add-flag-for-plugin
Browse files Browse the repository at this point in the history
Add flags for plugin
  • Loading branch information
tenntenn authored Jun 7, 2020
2 parents bee45b9 + 652651e commit 1ef067b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,10 @@ The main.go can be built as a plugin for [golangci-lint](https://golangci-lint.r
$ skeleton pkgname
$ go build -buildmode=plugin -o path_to_plugin_dir importpath
```

If you would like to specify flags for your plugin, you can put them via `ldflags` as below.

```
$ skeleton pkgname
$ go build -buildmode=plugin -ldflags "-X 'main.flags=-funcs log.Fatal'" -o path_to_plugin_dir github.com/gostaticanalysis/called/plugin/called
```
13 changes: 13 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,29 @@ var pluginMainTempl = template.Must(template.New("main.go").Parse(`// This file
package main
import (
"strings"
"{{.ImportPath}}"
"golang.org/x/tools/go/analysis"
)
// flags for Analyzer.Flag.
// If you would like to specify flags for your plugin, you can put them via `ldflags` as below.
// $ go build -buildmode=plugin -ldflags "-X 'main.flags=-opt val'" {{.ImportPath}}/plugin/{{.Pkg}
var flags string
// AnalyzerPlugin provides analyzers as a plugin.
// It follows golangci-lint style plugin.
var AnalyzerPlugin analyzerPlugin
type analyzerPlugin struct{}
func (analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
if flags != "" {
flagset := {{.Pkg}}.Analyzer.Flags
if err := flagset.Parse(strings.Split(flags, " ")); err != nil {
panic("cannot parse flags of {{.Pkg}}: "+err.Error())
}
}
return []*analysis.Analyzer{
{{.Pkg}}.Analyzer,
}
Expand Down

0 comments on commit 1ef067b

Please sign in to comment.