File tree Expand file tree Collapse file tree 3 files changed +88
-0
lines changed Expand file tree Collapse file tree 3 files changed +88
-0
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,9 @@ pkgname
25
25
├── cmd
26
26
│ └── pkgname
27
27
│ └── main.go
28
+ ├── plugin
29
+ │ └── pkgname
30
+ │ └── main.go
28
31
├── pkgname.go
29
32
├── pkgname_test.go
30
33
└── testdata
@@ -38,6 +41,12 @@ pkgname
38
41
```
39
42
$ skeleton -path="github.com/gostaticanalysis/pkgname"
40
43
pkgname
44
+ ├── cmd
45
+ │ └── pkgname
46
+ │ └── main.go
47
+ ├── plugin
48
+ │ └── pkgname
49
+ │ └── main.go
41
50
├── pkgname.go
42
51
├── pkgname_test.go
43
52
└── testdata
@@ -51,10 +60,39 @@ pkgname
51
60
```
52
61
$ skeleton -cmd=false pkgname
53
62
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
54
82
├── pkgname.go
55
83
├── pkgname_test.go
56
84
└── testdata
57
85
└── src
58
86
└── a
59
87
└── a.go
60
88
```
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
+ ```
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import (
15
15
func main () {
16
16
var s Skeleton
17
17
flag .BoolVar (& s .Cmd , "cmd" , true , "create cmd directory" )
18
+ flag .BoolVar (& s .Plugin , "plugin" , true , "create plugin directory" )
18
19
flag .StringVar (& s .ImportPath , "path" , "" , "import path" )
19
20
flag .Parse ()
20
21
s .ExeName = os .Args [0 ]
@@ -35,6 +36,7 @@ type Skeleton struct {
35
36
ExeName string
36
37
Args []string
37
38
Cmd bool
39
+ Plugin bool
38
40
ImportPath string
39
41
}
40
42
@@ -107,6 +109,12 @@ func (s *Skeleton) Run() error {
107
109
}
108
110
}
109
111
112
+ if s .Plugin {
113
+ if err := s .createPlugin (dir , & info ); err != nil {
114
+ return err
115
+ }
116
+ }
117
+
110
118
return nil
111
119
}
112
120
@@ -152,3 +160,22 @@ func (s *Skeleton) createCmd(dir string, info *PkgInfo) error {
152
160
153
161
return nil
154
162
}
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
+ }
Original file line number Diff line number Diff line change @@ -78,3 +78,26 @@ import (
78
78
79
79
func main() { unitchecker.Main({{.Pkg}}.Analyzer) }
80
80
` ))
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
+ ` ))
You can’t perform that action at this time.
0 commit comments