Skip to content

Commit d70b518

Browse files
committed
Add template overrides from files in new config
1 parent 2cf7fcf commit d70b518

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

cmd/oapi-codegen/oapi-codegen.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ func main() {
131131
if err != nil {
132132
errExit("error processing flags: %v", err)
133133
}
134+
if opts, err = updateTemplatesFromFiles(opts); err != nil {
135+
errExit("error loading template files: %v", err)
136+
}
134137
} else {
135138
var oldConfig oldConfiguration
136139
if flagConfigFile != "" {
@@ -265,6 +268,27 @@ func updateConfigFromFlags(cfg configuration) (configuration, error) {
265268
return cfg, nil
266269
}
267270

271+
// updateTemplatesFromFiles populates the UserTemplates field in OutputOptions using the
272+
// UserTemplateFiles field. If there is a shared key between UserTemplates and UserTemplateFiles,
273+
// the contents of the file take precedence.
274+
func updateTemplatesFromFiles(cfg configuration) (configuration, error) {
275+
oo := &cfg.OutputOptions
276+
if oo.UserTemplateFiles == nil {
277+
return cfg, nil
278+
}
279+
if oo.UserTemplates == nil {
280+
oo.UserTemplates = make(map[string]string)
281+
}
282+
for key, filename := range oo.UserTemplateFiles {
283+
data, err := ioutil.ReadFile(filename)
284+
if err != nil {
285+
return configuration{}, err
286+
}
287+
oo.UserTemplates[key] = string(data)
288+
}
289+
return cfg, nil
290+
}
291+
268292
// updateOldConfigFromFlags parses the flags and the config file. Anything which is
269293
// a zerovalue in the configuration file will be replaced with the flag
270294
// value, this means that the config file overrides flag values.

pkg/codegen/configuration.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type GenerateOptions struct {
2626
EchoServer bool `yaml:"echo-server,omitempty"` // EchoServer specifies whether to generate echo server boilerplate
2727
GinServer bool `yaml:"gin-server,omitempty"` // GinServer specifies whether to generate gin server boilerplate
2828
GorillaServer bool `yaml:"gorilla-server,omitempty"` // GorillaServer specifies whether to generate Gorilla server boilerplate
29-
Strict bool `yaml:"strict-server,omitempty"` // Strict specifies whether to generate strict server wrapper
29+
Strict bool `yaml:"strict-server,omitempty"` // Strict specifies whether to generate strict server wrapper
3030
Client bool `yaml:"client,omitempty"` // Client specifies whether to generate client boilerplate
3131
Models bool `yaml:"models,omitempty"` // Models specifies whether to generate type definitions
3232
EmbeddedSpec bool `yaml:"embedded-spec,omitempty"` // Whether to embed the swagger spec in the generated code
@@ -61,11 +61,12 @@ type CompatibilityOptions struct {
6161

6262
// OutputOptions are used to modify the output code in some way.
6363
type OutputOptions struct {
64-
SkipFmt bool `yaml:"skip-fmt,omitempty"` // Whether to skip go imports on the generated code
65-
SkipPrune bool `yaml:"skip-prune,omitempty"` // Whether to skip pruning unused components on the generated code
66-
IncludeTags []string `yaml:"include-tags,omitempty"` // Only include operations that have one of these tags. Ignored when empty.
67-
ExcludeTags []string `yaml:"exclude-tags,omitempty"` // Exclude operations that have one of these tags. Ignored when empty.
68-
UserTemplates map[string]string `yaml:"user-templates,omitempty"` // Override built-in templates from user-provided files
64+
SkipFmt bool `yaml:"skip-fmt,omitempty"` // Whether to skip go imports on the generated code
65+
SkipPrune bool `yaml:"skip-prune,omitempty"` // Whether to skip pruning unused components on the generated code
66+
IncludeTags []string `yaml:"include-tags,omitempty"` // Only include operations that have one of these tags. Ignored when empty.
67+
ExcludeTags []string `yaml:"exclude-tags,omitempty"` // Exclude operations that have one of these tags. Ignored when empty.
68+
UserTemplates map[string]string `yaml:"user-templates,omitempty"` // Override built-in templates from user-provided files
69+
UserTemplateFiles map[string]string `yaml:"user-template-files,omitempty"` // Same as UserTemplates, with filenames as values instead
6970

7071
ExcludeSchemas []string `yaml:"exclude-schemas,omitempty"` // Exclude from generation schemas with given names. Ignored when empty.
7172
ResponseTypeSuffix string `yaml:"response-type-suffix,omitempty"` // The suffix used for responses types

0 commit comments

Comments
 (0)