Adds user-templates-dir to OutputOptions#707
Closed
chanced wants to merge 3 commits intooapi-codegen:masterfrom
Closed
Adds user-templates-dir to OutputOptions#707chanced wants to merge 3 commits intooapi-codegen:masterfrom
user-templates-dir to OutputOptions#707chanced wants to merge 3 commits intooapi-codegen:masterfrom
Conversation
Author
|
Setting up a local command that calls |
|
@chanced Could you elaborate on this? What do you mean by local command? |
Author
|
The way I ended up addressing this was to create a wrapper application which loaded templates & template functions and then called This is rough but it'll give you an idea: package main
import (
"context"
"embed"
"flag"
"io/fs"
"log"
"os"
"path"
"strings"
"text/template"
"github.com/deepmap/oapi-codegen/pkg/codegen"
"github.com/deepmap/oapi-codegen/pkg/util"
"github.com/getkin/kin-openapi/openapi3"
"gopkg.in/yaml.v3"
)
type configuration struct {
codegen.Configuration `yaml:",inline"`
// OutputFile is the filename to output.
OutputFile string `yaml:"output,omitempty"`
}
//go:embed templates
var templates embed.FS
// add template functions here
var templateFunctions template.FuncMap = template.FuncMap{}
func main() {
log.SetFlags(0)
var cfgpath string
flag.StringVar(&cfgpath, "config", "", "path to config file")
flag.Parse()
if cfgpath == "" {
log.Fatal("--config is required")
}
if flag.NArg() < 1 {
log.Fatal("Please specify a path to an OpenAPI 3.0 spec file")
}
// loading specification
input := flag.Arg(0)
spec, err := util.LoadSwagger(input)
if err != nil {
log.Fatalf("error loading openapi specification: %v", err)
}
err = spec.Validate(context.Background())
if err != nil {
log.Fatalf("error validating openapi specification: %v", err)
}
// loading configuration
cfgdata, err := os.ReadFile(cfgpath)
if err != nil {
log.Fatalf("error reading config file: %s", err)
}
var cfg configuration
err = yaml.Unmarshal(cfgdata, &cfg)
if err != nil {
log.Fatalf("error unmarshaling config %v", err)
}
// generating output
output, err := generate(spec, cfg.Configuration, templates)
if err != nil {
log.Fatalf("error generating code: %v", err)
}
// writing output to file
outFile, err := os.Create(cfg.OutputFile)
if err != nil {
log.Fatalf("error creating output file: %v", err)
}
_, err = outFile.Write([]byte(output))
if err != nil {
log.Fatalf("error writing output file: %v", err)
}
outFile.Close()
}
func generate(spec *openapi3.T, config codegen.Configuration, templates embed.FS) (string, error) {
var err error
config, err = addTemplateOverrides(config, templates)
if err != nil {
return "", err
}
// adding local template functions
for k, v := range templateFunctions {
codegen.TemplateFunctions[k] = v
}
return codegen.Generate(spec, config)
}
func addTemplateOverrides(config codegen.Configuration, templates embed.FS) (codegen.Configuration, error) {
overrides := config.OutputOptions.UserTemplates
if overrides == nil {
overrides = make(map[string]string)
}
err := fs.WalkDir(templates, ".", func(p string, d fs.DirEntry, err error) error {
if !d.IsDir() {
if err != nil {
return err
}
f, err := templates.ReadFile(p)
if err != nil {
return err
}
// using .gtpl for light syntax highlighting
name := strings.TrimSuffix(p, path.Ext(p)) + ".tmpl"
name = strings.Join(strings.Split(name, "/")[1:], "/")
overrides[name] = string(f)
}
return nil
})
config.OutputOptions.UserTemplates = overrides
return config, err
}To generate with it, you'd do something like (assuming you named it //go:generate go run github.com/{github-user}/{project-id}/cmd/generate-go-api --config=api.server.yaml ../../spec/api.yaml |
Author
|
@w32blaster I saw your comment on the other pull request regarding templates. You may be interested in the code above. |
Author
|
maintainers, I'm re-opening this incase you find merit in it. Please feel free to close. |
|
Awesome! Thank you @chanced ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This addresses #607 and is an alternative or companion to #653.
Adds a new configuration parameter,
user-templates-dir, onOutputOptionswhich allows for assigning a directory path to parse user-template overrides. It re-uses the existing logic found inloadTemplateOverridesbut is replicated in thecodegenpackage.user-templatestakes precedence overuser-templates-dir.