Skip to content

Commit 1608fcc

Browse files
authored
Merge pull request #5 from gostaticanalysis/add-plugin
Generate skeleton code of a plugin for golangci-lint
2 parents 513a933 + 7123a36 commit 1608fcc

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ pkgname
2525
├── cmd
2626
│   └── pkgname
2727
│   └── main.go
28+
├── plugin
29+
│   └── pkgname
30+
│   └── main.go
2831
├── pkgname.go
2932
├── pkgname_test.go
3033
└── testdata
@@ -38,6 +41,12 @@ pkgname
3841
```
3942
$ skeleton -path="github.com/gostaticanalysis/pkgname"
4043
pkgname
44+
├── cmd
45+
│   └── pkgname
46+
│   └── main.go
47+
├── plugin
48+
│   └── pkgname
49+
│   └── main.go
4150
├── pkgname.go
4251
├── pkgname_test.go
4352
└── testdata
@@ -51,10 +60,39 @@ pkgname
5160
```
5261
$ skeleton -cmd=false pkgname
5362
pkgname
63+
├── plugin
64+
│   └── pkgname
65+
│   └── main.go
66+
├── pkgname.go
67+
├── pkgname_test.go
68+
└── testdata
69+
└── src
70+
└── a
71+
└── a.go
72+
```
73+
74+
### Create skeleton codes without plugin directory
75+
76+
```
77+
$ skeleton -cmd=false pkgname
78+
pkgname
79+
├── cmd
80+
│   └── pkgname
81+
│   └── main.go
5482
├── pkgname.go
5583
├── pkgname_test.go
5684
└── testdata
5785
└── src
5886
└── a
5987
└── a.go
6088
```
89+
90+
## Build as a plugin for golangci-lint
91+
92+
`skeleton` generates plugin directory which has main.go.
93+
The main.go can be built as a plugin for [golangci-lint](https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint).
94+
95+
```
96+
$ skeleton pkgname
97+
$ go build -buildmode=plugin -o path_to_plugin_dir importpath
98+
```

main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
func main() {
1616
var s Skeleton
1717
flag.BoolVar(&s.Cmd, "cmd", true, "create cmd directory")
18+
flag.BoolVar(&s.Plugin, "plugin", true, "create plugin directory")
1819
flag.StringVar(&s.ImportPath, "path", "", "import path")
1920
flag.Parse()
2021
s.ExeName = os.Args[0]
@@ -35,6 +36,7 @@ type Skeleton struct {
3536
ExeName string
3637
Args []string
3738
Cmd bool
39+
Plugin bool
3840
ImportPath string
3941
}
4042

@@ -107,6 +109,12 @@ func (s *Skeleton) Run() error {
107109
}
108110
}
109111

112+
if s.Plugin {
113+
if err := s.createPlugin(dir, &info); err != nil {
114+
return err
115+
}
116+
}
117+
110118
return nil
111119
}
112120

@@ -152,3 +160,22 @@ func (s *Skeleton) createCmd(dir string, info *PkgInfo) error {
152160

153161
return nil
154162
}
163+
164+
func (s *Skeleton) createPlugin(dir string, info *PkgInfo) error {
165+
pluginDir := filepath.Join(dir, "plugin", info.Pkg)
166+
if err := os.MkdirAll(pluginDir, 0777); err != nil {
167+
return err
168+
}
169+
170+
pluginMain, err := os.Create(filepath.Join(pluginDir, "main.go"))
171+
if err != nil {
172+
return err
173+
}
174+
defer pluginMain.Close()
175+
176+
if err := pluginMainTempl.Execute(pluginMain, info); err != nil {
177+
return err
178+
}
179+
180+
return nil
181+
}

template.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,26 @@ import (
7878
7979
func main() { unitchecker.Main({{.Pkg}}.Analyzer) }
8080
`))
81+
82+
var pluginMainTempl = template.Must(template.New("main.go").Parse(`// This file can build as a plugin for golangci-lint by below command.
83+
// go build -buildmode=plugin -o path_to_plugin_dir {{.ImportPath}}/plugin/{{.Pkg}}
84+
// See: https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint
85+
86+
package main
87+
88+
import (
89+
"{{.ImportPath}}"
90+
"golang.org/x/tools/go/analysis"
91+
)
92+
93+
// AnalyzerPlugin provides analyzers as a plugin.
94+
// It follows golangci-lint style plugin.
95+
var AnalyzerPlugin analyzerPlugin
96+
97+
type analyzerPlugin struct{}
98+
func (analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
99+
return []*analysis.Analyzer{
100+
{{.Pkg}}.Analyzer,
101+
}
102+
}
103+
`))

0 commit comments

Comments
 (0)