Skip to content

Commit 2210cc2

Browse files
committed
fix(oapi-codegen): command line parsing compatibility (oapi-codegen#852)
Improve command line compatibility between old and new style configs so existing user setups work as expected where possible. This restores -generate and -templates flags from deprecated states as the new configuration format doesn't provide the needed functionality with out them. It makes all but -alias-types work with new config files. The -alias-types doesn't do anything in either config mode any more. For -generate the use case is one configuration and multiple output files for different generation types, which we use extensively. For -templates having to have go .tmpl inside a .yaml config file works for simple snippets but for large templates its not practical, hence the direction option is required.
1 parent 99b7af5 commit 2210cc2

1 file changed

Lines changed: 66 additions & 56 deletions

File tree

cmd/oapi-codegen/oapi-codegen.go

Lines changed: 66 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"path"
2222
"path/filepath"
2323
"runtime/debug"
24-
"strings"
2524

2625
"gopkg.in/yaml.v2"
2726

@@ -42,13 +41,13 @@ var (
4241
flagPrintVersion bool
4342
flagPackageName string
4443
flagPrintUsage bool
44+
flagGenerate string
45+
flagTemplatesDir string
4546

46-
// The options below are deprecated, and they will be removed in a future
47+
// Deprecated: The options below will be removed in a future
4748
// release. Please use the new config file format.
48-
flagGenerate string
4949
flagIncludeTags string
5050
flagExcludeTags string
51-
flagTemplatesDir string
5251
flagImportMapping string
5352
flagExcludeSchemas string
5453
flagResponseTypeSuffix string
@@ -164,10 +163,8 @@ func main() {
164163
// config style. It should work correctly if we go down the old path,
165164
// even if we have a simple config file readable as both types.
166165
deprecatedFlagNames := map[string]bool{
167-
"generate": true,
168166
"include-tags": true,
169167
"exclude-tags": true,
170-
"templates": true,
171168
"import-mapping": true,
172169
"exclude-schemas": true,
173170
"response-type-suffix": true,
@@ -216,9 +213,8 @@ func main() {
216213
OutputFile: flagOutputFile,
217214
}
218215
}
219-
var err error
220-
opts, err = updateConfigFromFlags(opts)
221-
if err != nil {
216+
217+
if err := updateConfigFromFlags(&opts); err != nil {
222218
errExit("error processing flags: %v\n", err)
223219
}
224220
} else {
@@ -352,51 +348,54 @@ func detectPackageName(cfg *configuration) error {
352348
}
353349

354350
// updateConfigFromFlags updates a loaded configuration from flags. Flags
355-
// override anything in the file. We generate errors for command line options
356-
// associated with the old style configuration
357-
func updateConfigFromFlags(cfg configuration) (configuration, error) {
351+
// override anything in the file. We generate errors for any unsupported
352+
// command line flags.
353+
func updateConfigFromFlags(cfg *configuration) error {
358354
if flagPackageName != "" {
359355
cfg.PackageName = flagPackageName
360356
}
361357

362-
var unsupportedFlags []string
363-
364358
if flagGenerate != "types,client,server,spec" {
365-
unsupportedFlags = append(unsupportedFlags, "--generate")
359+
// Override generation and output options from generate command line flag.
360+
if err := generationTargets(&cfg.Configuration, util.ParseCommandLineList(flagGenerate)); err != nil {
361+
return err
362+
}
366363
}
367364
if flagIncludeTags != "" {
368-
unsupportedFlags = append(unsupportedFlags, "--include-tags")
365+
cfg.OutputOptions.IncludeTags = util.ParseCommandLineList(flagIncludeTags)
369366
}
370367
if flagExcludeTags != "" {
371-
unsupportedFlags = append(unsupportedFlags, "--exclude-tags")
368+
cfg.OutputOptions.ExcludeTags = util.ParseCommandLineList(flagExcludeTags)
372369
}
373370
if flagTemplatesDir != "" {
374-
unsupportedFlags = append(unsupportedFlags, "--templates")
371+
templates, err := loadTemplateOverrides(flagTemplatesDir)
372+
if err != nil {
373+
return fmt.Errorf("load templates from %q: %w", flagTemplatesDir, err)
374+
}
375+
cfg.OutputOptions.UserTemplates = templates
375376
}
376377
if flagImportMapping != "" {
377-
unsupportedFlags = append(unsupportedFlags, "--import-mapping")
378+
var err error
379+
cfg.ImportMapping, err = util.ParseCommandlineMap(flagImportMapping)
380+
if err != nil {
381+
return err
382+
}
378383
}
379384
if flagExcludeSchemas != "" {
380-
unsupportedFlags = append(unsupportedFlags, "--exclude-schemas")
385+
cfg.OutputOptions.ExcludeSchemas = util.ParseCommandLineList(flagExcludeSchemas)
381386
}
382387
if flagResponseTypeSuffix != "" {
383-
unsupportedFlags = append(unsupportedFlags, "--response-type-suffix")
388+
cfg.OutputOptions.ResponseTypeSuffix = flagResponseTypeSuffix
384389
}
385390
if flagAliasTypes {
386-
unsupportedFlags = append(unsupportedFlags, "--alias-types")
387-
}
388-
389-
if len(unsupportedFlags) > 0 {
390-
return configuration{}, fmt.Errorf("flags %s aren't supported in "+
391-
"new config style, please use --old-config-style or update your configuration ",
392-
strings.Join(unsupportedFlags, ", "))
391+
return fmt.Errorf("--alias-types isn't supported any more")
393392
}
394393

395394
if cfg.OutputFile == "" {
396395
cfg.OutputFile = flagOutputFile
397396
}
398397

399-
return cfg, nil
398+
return nil
400399
}
401400

402401
// updateOldConfigFromFlags parses the flags and the config file. Anything which is
@@ -434,6 +433,40 @@ func updateOldConfigFromFlags(cfg oldConfiguration) oldConfiguration {
434433
return cfg
435434
}
436435

436+
// generationTargets sets cfg options based on the generation targets.
437+
func generationTargets(cfg *codegen.Configuration, targets []string) error {
438+
opts := codegen.GenerateOptions{} // Blank to start with.
439+
for _, opt := range targets {
440+
switch opt {
441+
case "chi-server", "chi":
442+
opts.ChiServer = true
443+
case "server", "echo-server", "echo":
444+
opts.EchoServer = true
445+
case "gin", "gin-server":
446+
opts.GinServer = true
447+
case "gorilla", "gorilla-server":
448+
opts.GorillaServer = true
449+
case "strict-server":
450+
opts.Strict = true
451+
case "client":
452+
opts.Client = true
453+
case "types", "models":
454+
opts.Models = true
455+
case "spec", "embedded-spec":
456+
opts.EmbeddedSpec = true
457+
case "skip-fmt":
458+
cfg.OutputOptions.SkipFmt = true
459+
case "skip-prune":
460+
cfg.OutputOptions.SkipPrune = true
461+
default:
462+
return fmt.Errorf("unknown generate option %q", opt)
463+
}
464+
}
465+
cfg.Generate = opts
466+
467+
return nil
468+
}
469+
437470
func newConfigFromOldConfig(c oldConfiguration) configuration {
438471
// Take flags into account.
439472
cfg := updateOldConfigFromFlags(c)
@@ -445,33 +478,10 @@ func newConfigFromOldConfig(c oldConfiguration) configuration {
445478
}
446479
opts.OutputOptions.ResponseTypeSuffix = flagResponseTypeSuffix
447480

448-
for _, g := range cfg.GenerateTargets {
449-
switch g {
450-
case "client":
451-
opts.Generate.Client = true
452-
case "chi-server":
453-
opts.Generate.ChiServer = true
454-
case "server":
455-
opts.Generate.EchoServer = true
456-
case "gin":
457-
opts.Generate.GinServer = true
458-
case "gorilla":
459-
opts.Generate.GorillaServer = true
460-
case "strict-server":
461-
opts.Generate.Strict = true
462-
case "types":
463-
opts.Generate.Models = true
464-
case "spec":
465-
opts.Generate.EmbeddedSpec = true
466-
case "skip-fmt":
467-
opts.OutputOptions.SkipFmt = true
468-
case "skip-prune":
469-
opts.OutputOptions.SkipPrune = true
470-
default:
471-
fmt.Printf("unknown generate option %s\n", g)
472-
flag.PrintDefaults()
473-
os.Exit(1)
474-
}
481+
if err := generationTargets(&opts, cfg.GenerateTargets); err != nil {
482+
fmt.Println(err)
483+
flag.PrintDefaults()
484+
os.Exit(1)
475485
}
476486

477487
opts.OutputOptions.IncludeTags = cfg.IncludeTags

0 commit comments

Comments
 (0)