/
main.go
224 lines (212 loc) · 6.3 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/urfave/cli/v2"
"github.com/swaggo/swag"
"github.com/swaggo/swag/format"
"github.com/swaggo/swag/gen"
)
const (
searchDirFlag = "dir"
excludeFlag = "exclude"
generalInfoFlag = "generalInfo"
propertyStrategyFlag = "propertyStrategy"
outputFlag = "output"
outputTypesFlag = "outputTypes"
parseVendorFlag = "parseVendor"
parseDependencyFlag = "parseDependency"
markdownFilesFlag = "markdownFiles"
codeExampleFilesFlag = "codeExampleFiles"
parseInternalFlag = "parseInternal"
generatedTimeFlag = "generatedTime"
requiredByDefaultFlag = "requiredByDefault"
parseDepthFlag = "parseDepth"
instanceNameFlag = "instanceName"
overridesFileFlag = "overridesFile"
parseGoListFlag = "parseGoList"
quietFlag = "quiet"
)
var initFlags = []cli.Flag{
&cli.BoolFlag{
Name: quietFlag,
Aliases: []string{"q"},
Usage: "Make the logger quiet.",
},
&cli.StringFlag{
Name: generalInfoFlag,
Aliases: []string{"g"},
Value: "main.go",
Usage: "Go file path in which 'swagger general API Info' is written",
},
&cli.StringFlag{
Name: searchDirFlag,
Aliases: []string{"d"},
Value: "./",
Usage: "Directories you want to parse,comma separated and general-info file must be in the first one",
},
&cli.StringFlag{
Name: excludeFlag,
Usage: "Exclude directories and files when searching, comma separated",
},
&cli.StringFlag{
Name: propertyStrategyFlag,
Aliases: []string{"p"},
Value: swag.CamelCase,
Usage: "Property Naming Strategy like " + swag.SnakeCase + "," + swag.CamelCase + "," + swag.PascalCase,
},
&cli.StringFlag{
Name: outputFlag,
Aliases: []string{"o"},
Value: "./docs",
Usage: "Output directory for all the generated files(swagger.json, swagger.yaml and docs.go)",
},
&cli.StringFlag{
Name: outputTypesFlag,
Aliases: []string{"ot"},
Value: "go,json,yaml",
Usage: "Output types of generated files (docs.go, swagger.json, swagger.yaml) like go,json,yaml",
},
&cli.BoolFlag{
Name: parseVendorFlag,
Usage: "Parse go files in 'vendor' folder, disabled by default",
},
&cli.BoolFlag{
Name: parseDependencyFlag,
Aliases: []string{"pd"},
Usage: "Parse go files inside dependency folder, disabled by default",
},
&cli.StringFlag{
Name: markdownFilesFlag,
Aliases: []string{"md"},
Value: "",
Usage: "Parse folder containing markdown files to use as description, disabled by default",
},
&cli.StringFlag{
Name: codeExampleFilesFlag,
Aliases: []string{"cef"},
Value: "",
Usage: "Parse folder containing code example files to use for the x-codeSamples extension, disabled by default",
},
&cli.BoolFlag{
Name: parseInternalFlag,
Usage: "Parse go files in internal packages, disabled by default",
},
&cli.BoolFlag{
Name: generatedTimeFlag,
Usage: "Generate timestamp at the top of docs.go, disabled by default",
},
&cli.IntFlag{
Name: parseDepthFlag,
Value: 100,
Usage: "Dependency parse depth",
},
&cli.BoolFlag{
Name: requiredByDefaultFlag,
Usage: "Set validation required for all fields by default",
},
&cli.StringFlag{
Name: instanceNameFlag,
Value: "",
Usage: "This parameter can be used to name different swagger document instances. It is optional.",
},
&cli.StringFlag{
Name: overridesFileFlag,
Value: gen.DefaultOverridesFile,
Usage: "File to read global type overrides from.",
},
&cli.BoolFlag{
Name: parseGoListFlag,
Value: true,
Usage: "Parse dependency via 'go list'",
},
}
func initAction(ctx *cli.Context) error {
strategy := ctx.String(propertyStrategyFlag)
switch strategy {
case swag.CamelCase, swag.SnakeCase, swag.PascalCase:
default:
return fmt.Errorf("not supported %s propertyStrategy", strategy)
}
outputTypes := strings.Split(ctx.String(outputTypesFlag), ",")
if len(outputTypes) == 0 {
return fmt.Errorf("no output types specified")
}
logger := log.New(os.Stdout, "", log.LstdFlags)
if ctx.Bool(quietFlag) {
logger = log.New(ioutil.Discard, "", log.LstdFlags)
}
return gen.New().Build(&gen.Config{
SearchDir: ctx.String(searchDirFlag),
Excludes: ctx.String(excludeFlag),
MainAPIFile: ctx.String(generalInfoFlag),
PropNamingStrategy: strategy,
OutputDir: ctx.String(outputFlag),
OutputTypes: outputTypes,
ParseVendor: ctx.Bool(parseVendorFlag),
ParseDependency: ctx.Bool(parseDependencyFlag),
MarkdownFilesDir: ctx.String(markdownFilesFlag),
ParseInternal: ctx.Bool(parseInternalFlag),
GeneratedTime: ctx.Bool(generatedTimeFlag),
RequiredByDefault: ctx.Bool(requiredByDefaultFlag),
CodeExampleFilesDir: ctx.String(codeExampleFilesFlag),
ParseDepth: ctx.Int(parseDepthFlag),
InstanceName: ctx.String(instanceNameFlag),
OverridesFile: ctx.String(overridesFileFlag),
ParseGoList: ctx.Bool(parseGoListFlag),
Debugger: logger,
})
}
func main() {
app := cli.NewApp()
app.Version = swag.Version
app.Usage = "Automatically generate RESTful API documentation with Swagger 2.0 for Go."
app.Commands = []*cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "Create docs.go",
Action: initAction,
Flags: initFlags,
},
{
Name: "fmt",
Aliases: []string{"f"},
Usage: "format swag comments",
Action: func(c *cli.Context) error {
searchDir := c.String(searchDirFlag)
excludeDir := c.String(excludeFlag)
mainFile := c.String(generalInfoFlag)
return format.New().Build(&format.Config{
SearchDir: searchDir,
Excludes: excludeDir,
MainFile: mainFile,
})
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: searchDirFlag,
Aliases: []string{"d"},
Value: "./",
Usage: "Directories you want to parse,comma separated and general-info file must be in the first one",
},
&cli.StringFlag{
Name: excludeFlag,
Usage: "Exclude directories and files when searching, comma separated",
},
&cli.StringFlag{
Name: generalInfoFlag,
Aliases: []string{"g"},
Value: "main.go",
Usage: "Go file path in which 'swagger general API Info' is written",
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}