forked from aarondl/oa3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (135 loc) · 3.8 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
package main
import (
"embed"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/aarondl/oa3/elm"
"github.com/aarondl/oa3/generator"
"github.com/aarondl/oa3/goclient"
"github.com/aarondl/oa3/goserver"
"github.com/aarondl/oa3/openapi3spec"
"github.com/aarondl/oa3/typescript"
"github.com/spf13/cobra"
)
//go:embed templates
var templates embed.FS
var (
wd string
version string = "unknown"
)
var (
flagParams []string
flagDebug bool
flagWipe bool
flagOutputDir string
flagTemplateDir string
)
var rootCmd = &cobra.Command{
Use: "oa3 [flags] <generator> <openapifile>",
Short: "Generate a language library for an openapi3 spec file",
Long: `Generate a language library for an openapi3 spec file
Generators available:
- go
- go-client
- typescript`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
openapi3spec.DebugOutput = flagDebug
if len(flagOutputDir) == 0 {
flagOutputDir = filepath.Join(wd, "out", args[0])
}
},
RunE: rootCmdRun,
Args: cobra.ExactArgs(2),
SilenceErrors: true,
SilenceUsage: true,
}
func main() {
for _, a := range os.Args {
if a == "--version" {
fmt.Println("oa3 version " + version)
return
}
}
cwd, err := os.Getwd()
if err != nil {
fmt.Println("failed to determine current working directory")
os.Exit(1)
}
wd = cwd
rootCmd.PersistentFlags().StringSliceVarP(&flagParams, "param", "p", nil, "key=value params to the generator")
rootCmd.PersistentFlags().BoolVarP(&flagDebug, "debug", "", false, "debug output")
rootCmd.PersistentFlags().BoolVarP(&flagWipe, "wipe", "w", false, "rm output directory before generation")
rootCmd.PersistentFlags().StringVarP(&flagOutputDir, "output", "o", "", "output directory (default: "+filepath.Join(wd, "out", "<generator>")+")")
rootCmd.PersistentFlags().StringVarP(&flagTemplateDir, "templates", "t", "", "template directory (default: embedded templates)")
// ignored, only for docs
rootCmd.PersistentFlags().BoolP("version", "", false, "output version and exit")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func rootCmdRun(cmd *cobra.Command, args []string) error {
params := make(map[string]string, len(flagParams))
for i, p := range flagParams {
splits := strings.SplitN(p, "=", 2)
if len(splits) != 2 || len(splits[0]) == 0 || len(splits[1]) == 0 {
return fmt.Errorf("--param[%d] invalid: must be key=value pair, got: %s", i, p)
}
params[splits[0]] = splits[1]
}
files, err := generate(args[0], args[1], params)
if err != nil {
return err
}
if flagWipe {
_ = os.RemoveAll(flagOutputDir)
}
if err = os.MkdirAll(flagOutputDir, 0775); err != nil {
return err
}
for _, f := range files {
if err := ioutil.WriteFile(filepath.Join(flagOutputDir, f.Name), f.Contents, 0640); err != nil {
return err
}
}
return nil
}
func generate(generatorID string, openapiFile string, params map[string]string) ([]generator.File, error) {
var gen generator.Interface
switch generatorID {
case "go":
gen = goserver.New()
case "go-client":
gen = goclient.New()
// masquerade as the original go server template folder to avoid
// having to split all the templates up
generatorID = "go"
case "elm":
gen = elm.New()
case "typescript":
gen = typescript.New()
default:
return nil, fmt.Errorf("unknown generator: %s", generatorID)
}
oa, err := openapi3spec.LoadYAML(openapiFile, true)
if err != nil {
return nil, err
}
var templateFS fs.FS
if len(flagTemplateDir) != 0 {
templateFS = os.DirFS(flagTemplateDir)
} else {
templateFS, err = fs.Sub(templates, filepath.Join("templates", generatorID))
if err != nil {
return nil, fmt.Errorf("failed to root fs for generator: %w", err)
}
}
if err := gen.Load(templateFS); err != nil {
return nil, err
}
return gen.Do(oa, params)
}