-
Notifications
You must be signed in to change notification settings - Fork 1
/
oa.go
executable file
·72 lines (63 loc) · 1.7 KB
/
oa.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
package gomvc
import (
"log"
"path/filepath"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/spf13/cobra"
)
var oa = &cobra.Command{
Use: "oa",
Short: "Generate controllers from an OpenAPI yml file",
Run: func(cmd *cobra.Command, args []string) {
flags := cmd.LocalFlags()
configDir, err := flags.GetString("config")
if err != nil {
log.Println(err.Error())
return
}
// TODO: read spec location from config
specPath, err := flags.GetString("spec")
if err != nil {
log.Println(err.Error())
return
}
// read intended destination for generation output
destDir, err := flags.GetString("dest")
if err != nil {
log.Println(err.Error())
return
}
templateDir, err := flags.GetString("templates")
if err != nil {
log.Println(err.Error())
return
}
oa3 := LoadWithKin(specPath)
GenerateFromOA(oa3, destDir, templateDir, configDir)
},
}
// GenerateFromOA is the primary logic for the oa command, creating controllers
func GenerateFromOA(oa3 *openapi3.T, dest, templateDir, configDir string) {
config := NewGoMVCConfig(configDir)
createDirIfNotExists(dest)
ctrlDest := filepath.Join(dest, "controllers")
createDirIfNotExists(ctrlDest)
CreateRouter(RouteData{}, "gin/router.tpl", ctrlDest)
g := NewGenerator(oa3)
for path, pathItem := range oa3.Paths {
path = strings.Trim(path, " ")
log.Printf("examining path, %s\n", path)
if config.IsDenylisted(path) {
continue
}
if err := g.CreateControllerFiles(path, pathItem, dest, templateDir); err != nil {
log.Fatalf("%s: %s", path, err.Error())
}
}
}
// OA is the cli command that creates a router and controller functions from an
// OpenAPI file
func OA() *cobra.Command {
return oa
}