/
shared.go
213 lines (186 loc) · 5.9 KB
/
shared.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
package generate
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/go-openapi/analysis"
"github.com/go-openapi/swag"
"github.com/go-swagger/go-swagger/generator"
flags "github.com/jessevdk/go-flags"
"github.com/spf13/viper"
)
// FlattenCmdOptions determines options to the flatten spec preprocessing
type FlattenCmdOptions struct {
WithExpand bool `long:"with-expand" description:"expands all $ref's in spec prior to generation (shorthand to --with-flatten=expand)"`
WithFlatten []string `long:"with-flatten" description:"flattens all $ref's in spec prior to generation" choice:"minimal" choice:"full" choice:"expand" choice:"verbose" choice:"noverbose" choice:"remove-unused" default:"minimal" default:"verbose"`
}
// SetFlattenOptions builds flatten options from command line args
func (f *FlattenCmdOptions) SetFlattenOptions(dflt *analysis.FlattenOpts) (res *analysis.FlattenOpts) {
res = &analysis.FlattenOpts{}
if dflt != nil {
*res = *dflt
}
if f == nil {
return
}
verboseIsSet := false
minimalIsSet := false
//removeUnusedIsSet := false
expandIsSet := false
if f.WithExpand {
res.Expand = true
expandIsSet = true
}
for _, opt := range f.WithFlatten {
if opt == "verbose" {
res.Verbose = true
verboseIsSet = true
}
if opt == "noverbose" && !verboseIsSet {
// verbose flag takes precedence
res.Verbose = false
verboseIsSet = true
}
if opt == "remove-unused" {
res.RemoveUnused = true
//removeUnusedIsSet = true
}
if opt == "expand" {
res.Expand = true
expandIsSet = true
}
if opt == "full" && !minimalIsSet && !expandIsSet {
// minimal flag takes precedence
res.Minimal = false
minimalIsSet = true
}
if opt == "minimal" && !expandIsSet {
// expand flag takes precedence
res.Minimal = true
minimalIsSet = true
}
}
return
}
type shared struct {
Spec flags.Filename `long:"spec" short:"f" description:"the spec file to use (default swagger.{json,yml,yaml})"`
APIPackage string `long:"api-package" short:"a" description:"the package to save the operations" default:"operations"`
ModelPackage string `long:"model-package" short:"m" description:"the package to save the models" default:"models"`
ServerPackage string `long:"server-package" short:"s" description:"the package to save the server specific code" default:"restapi"`
ClientPackage string `long:"client-package" short:"c" description:"the package to save the client specific code" default:"client"`
Target flags.Filename `long:"target" short:"t" default:"./" description:"the base directory for generating the files"`
Template string `long:"template" description:"Load contributed templates" choice:"stratoscale"`
TemplateDir flags.Filename `long:"template-dir" short:"T" description:"alternative template override directory"`
ConfigFile flags.Filename `long:"config-file" short:"C" description:"configuration file to use for overriding template options"`
CopyrightFile flags.Filename `long:"copyright-file" short:"r" description:"copyright file used to add copyright header"`
ExistingModels string `long:"existing-models" description:"use pre-generated models e.g. github.com/foobar/model"`
AdditionalInitialisms []string `long:"additional-initialism" description:"consecutive capitals that should be considered intialisms"`
FlattenCmdOptions
}
type sharedCommand interface {
getOpts() (*generator.GenOpts, error)
getShared() *shared
getConfigFile() flags.Filename
getAdditionalInitialisms() []string
generate(*generator.GenOpts) error
log(string)
}
func (s *shared) getConfigFile() flags.Filename {
return s.ConfigFile
}
func (s *shared) getAdditionalInitialisms() []string {
return s.AdditionalInitialisms
}
func (s *shared) setCopyright() (string, error) {
var copyrightstr string
copyrightfile := string(s.CopyrightFile)
if copyrightfile != "" {
//Read the Copyright from file path in opts
bytebuffer, err := ioutil.ReadFile(copyrightfile)
if err != nil {
return "", err
}
copyrightstr = string(bytebuffer)
} else {
copyrightstr = ""
}
return copyrightstr, nil
}
func createSwagger(s sharedCommand) error {
cfg, erc := readConfig(string(s.getConfigFile()))
if erc != nil {
return erc
}
setDebug(cfg)
opts, ero := s.getOpts()
if ero != nil {
return ero
}
if opts.Template != "" {
contribOptionsOverride(opts)
}
if err := opts.EnsureDefaults(); err != nil {
return err
}
if err := configureOptsFromConfig(cfg, opts); err != nil {
return err
}
swag.AddInitialisms(s.getAdditionalInitialisms()...)
if sharedOpts := s.getShared(); sharedOpts != nil {
// process shared options
opts.FlattenOpts = sharedOpts.FlattenCmdOptions.SetFlattenOptions(opts.FlattenOpts)
copyrightStr, erc := sharedOpts.setCopyright()
if erc != nil {
return erc
}
opts.Copyright = copyrightStr
}
if err := s.generate(opts); err != nil {
return err
}
basepath, era := filepath.Abs(".")
if era != nil {
return era
}
targetAbs, err := filepath.Abs(opts.Target)
if err != nil {
return err
}
rp, err := filepath.Rel(basepath, targetAbs)
if err != nil {
return err
}
s.log(rp)
return nil
}
func readConfig(filename string) (*viper.Viper, error) {
if filename == "" {
return nil, nil
}
abspath, err := filepath.Abs(filename)
if err != nil {
return nil, err
}
log.Println("trying to read config from", abspath)
return generator.ReadConfig(abspath)
}
func configureOptsFromConfig(cfg *viper.Viper, opts *generator.GenOpts) error {
if cfg == nil {
return nil
}
var def generator.LanguageDefinition
if err := cfg.Unmarshal(&def); err != nil {
return err
}
return def.ConfigureOpts(opts)
}
func setDebug(cfg *viper.Viper) {
if os.Getenv("DEBUG") != "" || os.Getenv("SWAGGER_DEBUG") != "" {
if cfg != nil {
cfg.Debug()
} else {
log.Println("NO config read")
}
}
}