forked from swaggo/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (126 loc) · 3.56 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
package main
import (
"fmt"
"log"
"os"
"github.com/swaggo/swag"
"github.com/swaggo/swag/gen"
"github.com/urfave/cli/v2"
)
const (
searchDirFlag = "dir"
excludeFlag = "exclude"
generalInfoFlag = "generalInfo"
propertyStrategyFlag = "propertyStrategy"
outputFlag = "output"
parseVendorFlag = "parseVendor"
parseDependencyFlag = "parseDependency"
markdownFilesFlag = "markdownFiles"
codeExampleFilesFlag = "codeExampleFiles"
parseInternalFlag = "parseInternal"
generatedTimeFlag = "generatedTime"
parseDepthFlag = "parseDepth"
)
var initFlags = []cli.Flag{
&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: "Directory you want to parse",
},
&cli.StringFlag{
Name: excludeFlag,
Usage: "Exclude directories and files when searching, comma separated",
},
&cli.StringFlag{
Name: propertyStrategyFlag,
Aliases: []string{"p"},
Value: "camelcase",
Usage: "Property Naming Strategy like snakecase,camelcase,pascalcase",
},
&cli.StringFlag{
Name: outputFlag,
Aliases: []string{"o"},
Value: "./docs",
Usage: "Output directory for all the generated files(swagger.json, swagger.yaml and doc.go)",
},
&cli.BoolFlag{
Name: parseVendorFlag,
Usage: "Parse go files in 'vendor' folder, disabled by default",
},
&cli.BoolFlag{
Name: parseDependencyFlag,
Usage: "Parse go files in outside 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",
},
}
func initAction(c *cli.Context) error {
strategy := c.String(propertyStrategyFlag)
switch strategy {
case swag.CamelCase, swag.SnakeCase, swag.PascalCase:
default:
return fmt.Errorf("not supported %s propertyStrategy", strategy)
}
return gen.New().Build(&gen.Config{
SearchDir: c.String(searchDirFlag),
Excludes: c.String(excludeFlag),
MainAPIFile: c.String(generalInfoFlag),
PropNamingStrategy: strategy,
OutputDir: c.String(outputFlag),
ParseVendor: c.Bool(parseVendorFlag),
ParseDependency: c.Bool(parseDependencyFlag),
MarkdownFilesDir: c.String(markdownFilesFlag),
ParseInternal: c.Bool(parseInternalFlag),
GeneratedTime: c.Bool(generatedTimeFlag),
CodeExampleFilesDir: c.String(codeExampleFilesFlag),
ParseDepth: c.Int(parseDepthFlag),
})
}
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,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}