-
Notifications
You must be signed in to change notification settings - Fork 3
/
entrypoint.go
267 lines (236 loc) · 5.52 KB
/
entrypoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package eapi
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime/debug"
"github.com/gotomicro/eapi/spec"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/urfave/cli/v2"
)
type Config struct {
Plugin string
Dir string
Output string
Depends []string
OpenAPI OpenAPIConfig
Generators []*GeneratorConfig
}
type OpenAPIConfig struct {
OpenAPI string `yaml:"openapi"` // OpenAPI version 3.0.0|3.0.3|3.1.0
Info *spec.Info `yaml:"info"` // Required
SecuritySchemes *SecuritySchemes `yaml:"securitySchemes"`
}
type SecuritySchemes map[string]*spec.SecurityScheme
func (c OpenAPIConfig) ApplyToDoc(doc *spec.T) {
if c.OpenAPI != "" {
doc.OpenAPI = c.OpenAPI
}
if c.Info != nil {
if c.Info.Version != "" {
doc.Info.Version = c.Info.Version
}
if c.Info.Title != "" {
doc.Info.Title = c.Info.Title
}
if c.Info.Description != "" {
doc.Info.Description = c.Info.Description
}
if c.Info.TermsOfService != "" {
doc.Info.TermsOfService = c.Info.TermsOfService
}
}
if c.SecuritySchemes != nil {
doc.Components.SecuritySchemes = make(map[string]*spec.SecuritySchemeRef)
for name, scheme := range *c.SecuritySchemes {
doc.Components.SecuritySchemes[name] = &spec.SecuritySchemeRef{Value: scheme}
}
}
}
type GeneratorConfig struct {
Name string
File string
Output string
}
type Entrypoint struct {
k *koanf.Koanf
plugins []Plugin
cfg Config
}
func NewEntrypoint(plugins ...Plugin) *Entrypoint {
return &Entrypoint{
k: koanf.New("."),
plugins: plugins,
cfg: Config{
Plugin: "gin",
Dir: ".",
Output: "docs",
},
}
}
const usageText = `Generate Doc:
eapi --config config.yaml
or
eapi --plugin gin --dir src/ --output docs/
Generate Frontend Code:
eapi --config config.yaml gencode
or
eapi --plugin gin --dir src/ --output docs/ gencode`
func (e *Entrypoint) Run(args []string) {
app := cli.NewApp()
app.Name = "egen"
app.Usage = `Tool for generating OpenAPI documentation and Frontend Code by static-analysis`
app.UsageText = usageText
app.Description = `Tool for generating OpenAPI documentation and Frontend Code by static-analysis`
app.Flags = append(app.Flags, &cli.StringFlag{
Name: "plugin",
Aliases: []string{"p", "plug"},
Usage: "specify plugin name",
Destination: &e.cfg.Plugin,
})
app.Flags = append(app.Flags, &cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Usage: "directory of your project which contains go.mod file",
Destination: &e.cfg.Dir,
})
app.Flags = append(app.Flags, &cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "output directory of openapi.json",
Destination: &e.cfg.Output,
})
app.Flags = append(app.Flags, &cli.StringSliceFlag{
Name: "depends",
Aliases: []string{"dep"},
Usage: "depended module name",
Action: func(context *cli.Context, depends []string) error {
e.cfg.Depends = depends
return nil
},
})
app.Flags = append(app.Flags, &cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "configuration file",
Required: false,
})
app.Commands = append(app.Commands, showVersion())
app.Action = e.run
err := app.Run(args)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func (e *Entrypoint) before(c *cli.Context) error {
cfg := c.String("config")
if cfg == "" {
fileInfo, err := os.Stat("eapi.yaml")
if err == nil && !fileInfo.IsDir() {
cfg = "eapi.yaml"
}
}
if cfg != "" {
err := e.loadConfig(cfg)
if err != nil {
return err
}
err = e.k.Unmarshal("", &e.cfg)
if err != nil {
return err
}
}
if e.cfg.Plugin == "" {
return fmt.Errorf("'plugin' is not set")
}
if e.cfg.Dir == "" {
e.cfg.Dir = "."
}
if e.cfg.Output == "" {
e.cfg.Output = "docs"
}
return nil
}
func (e *Entrypoint) loadConfig(cfg string) error {
return e.k.Load(file.Provider(cfg), yaml.Parser())
}
func (e *Entrypoint) run(c *cli.Context) error {
var plugin Plugin
err := e.before(c)
if err != nil {
return err
}
for _, p := range e.plugins {
if p.Name() == e.cfg.Plugin {
plugin = p
break
}
}
if plugin == nil {
return fmt.Errorf("plugin %s not exists", e.cfg.Plugin)
}
stat, err := os.Stat(e.cfg.Dir)
if err != nil {
return err
}
if !stat.IsDir() {
return fmt.Errorf("%s is not a directory", e.cfg.Dir)
}
err = os.MkdirAll(e.cfg.Output, os.ModePerm)
if err != nil {
return err
}
a := NewAnalyzer(e.k).Plugin(plugin).Depends(e.cfg.Depends...)
doc := a.Process(e.cfg.Dir).Doc().Specialize()
e.cfg.OpenAPI.ApplyToDoc(doc)
// write documentation
{
docContent, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(e.cfg.Output, "openapi.json"), docContent, fs.ModePerm)
if err != nil {
return err
}
}
// execute generators
for idx, item := range e.cfg.Generators {
err = newGeneratorExecutor(
item,
doc,
func(key string) interface{} {
confMap := e.k.Get("generators").([]interface{})[idx].(map[string]interface{})
val, ok := confMap[key]
if !ok {
return nil
}
return val
},
).execute()
if err != nil {
return err
}
}
return nil
}
func showVersion() *cli.Command {
return &cli.Command{
Name: "version",
Action: func(c *cli.Context) error {
info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Printf("unknown version\n")
os.Exit(1)
return nil
}
fmt.Printf("%s\n", info.Main.Version)
return nil
},
}
}