Blame vendor/github.com/deepmap/oapi-codegen/cmd/oapi-codegen/oapi-codegen.go

Packit Service 3a6627
// Copyright 2019 DeepMap, Inc.
Packit Service 3a6627
//
Packit Service 3a6627
// Licensed under the Apache License, Version 2.0 (the "License");
Packit Service 3a6627
// you may not use this file except in compliance with the License.
Packit Service 3a6627
// You may obtain a copy of the License at
Packit Service 3a6627
//
Packit Service 3a6627
// http://www.apache.org/licenses/LICENSE-2.0
Packit Service 3a6627
//
Packit Service 3a6627
// Unless required by applicable law or agreed to in writing, software
Packit Service 3a6627
// distributed under the License is distributed on an "AS IS" BASIS,
Packit Service 3a6627
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit Service 3a6627
// See the License for the specific language governing permissions and
Packit Service 3a6627
// limitations under the License.
Packit Service 3a6627
package main
Packit Service 3a6627
Packit Service 3a6627
import (
Packit Service 3a6627
	"flag"
Packit Service 3a6627
	"fmt"
Packit Service 3a6627
	"io/ioutil"
Packit Service 3a6627
	"os"
Packit Service 3a6627
	"path"
Packit Service 3a6627
	"path/filepath"
Packit Service 3a6627
	"strings"
Packit Service 3a6627
Packit Service 3a6627
	"github.com/deepmap/oapi-codegen/pkg/codegen"
Packit Service 3a6627
	"github.com/deepmap/oapi-codegen/pkg/util"
Packit Service 3a6627
)
Packit Service 3a6627
Packit Service 3a6627
func errExit(format string, args ...interface{}) {
Packit Service 3a6627
	_, _ = fmt.Fprintf(os.Stderr, format, args...)
Packit Service 3a6627
	os.Exit(1)
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
func main() {
Packit Service 3a6627
	var (
Packit Service 3a6627
		packageName    string
Packit Service 3a6627
		generate       string
Packit Service 3a6627
		outputFile     string
Packit Service 3a6627
		includeTags    string
Packit Service 3a6627
		excludeTags    string
Packit Service 3a6627
		templatesDir   string
Packit Service 3a6627
		importMapping  string
Packit Service 3a6627
		excludeSchemas string
Packit Service 3a6627
	)
Packit Service 3a6627
	flag.StringVar(&packageName, "package", "", "The package name for generated code")
Packit Service 3a6627
	flag.StringVar(&generate, "generate", "types,client,server,spec",
Packit Service 3a6627
		`Comma-separated list of code to generate; valid options: "types", "client", "chi-server", "server", "spec", "skip-fmt", "skip-prune"`)
Packit Service 3a6627
	flag.StringVar(&outputFile, "o", "", "Where to output generated code, stdout is default")
Packit Service 3a6627
	flag.StringVar(&includeTags, "include-tags", "", "Only include operations with the given tags. Comma-separated list of tags.")
Packit Service 3a6627
	flag.StringVar(&excludeTags, "exclude-tags", "", "Exclude operations that are tagged with the given tags. Comma-separated list of tags.")
Packit Service 3a6627
	flag.StringVar(&templatesDir, "templates", "", "Path to directory containing user templates")
Packit Service 3a6627
	flag.StringVar(&importMapping, "import-mapping", "", "A dict from the external reference to golang package path")
Packit Service 3a6627
	flag.StringVar(&excludeSchemas, "exclude-schemas", "", "A comma separated list of schemas which must be excluded from generation")
Packit Service 3a6627
	flag.Parse()
Packit Service 3a6627
Packit Service 3a6627
	if flag.NArg() < 1 {
Packit Service 3a6627
		fmt.Println("Please specify a path to a OpenAPI 3.0 spec file")
Packit Service 3a6627
		os.Exit(1)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	// If the package name has not been specified, we will use the name of the
Packit Service 3a6627
	// swagger file.
Packit Service 3a6627
	if packageName == "" {
Packit Service 3a6627
		path := flag.Arg(0)
Packit Service 3a6627
		baseName := filepath.Base(path)
Packit Service 3a6627
		// Split the base name on '.' to get the first part of the file.
Packit Service 3a6627
		nameParts := strings.Split(baseName, ".")
Packit Service 3a6627
		packageName = codegen.ToCamelCase(nameParts[0])
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	opts := codegen.Options{}
Packit Service 3a6627
	for _, g := range splitCSVArg(generate) {
Packit Service 3a6627
		switch g {
Packit Service 3a6627
		case "client":
Packit Service 3a6627
			opts.GenerateClient = true
Packit Service 3a6627
		case "chi-server":
Packit Service 3a6627
			opts.GenerateChiServer = true
Packit Service 3a6627
		case "server":
Packit Service 3a6627
			opts.GenerateEchoServer = true
Packit Service 3a6627
		case "types":
Packit Service 3a6627
			opts.GenerateTypes = true
Packit Service 3a6627
		case "spec":
Packit Service 3a6627
			opts.EmbedSpec = true
Packit Service 3a6627
		case "skip-fmt":
Packit Service 3a6627
			opts.SkipFmt = true
Packit Service 3a6627
		case "skip-prune":
Packit Service 3a6627
			opts.SkipPrune = true
Packit Service 3a6627
		default:
Packit Service 3a6627
			fmt.Printf("unknown generate option %s\n", g)
Packit Service 3a6627
			flag.PrintDefaults()
Packit Service 3a6627
			os.Exit(1)
Packit Service 3a6627
		}
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	opts.IncludeTags = splitCSVArg(includeTags)
Packit Service 3a6627
	opts.ExcludeTags = splitCSVArg(excludeTags)
Packit Service 3a6627
	opts.ExcludeSchemas = splitCSVArg(excludeSchemas)
Packit Service 3a6627
Packit Service 3a6627
	if opts.GenerateEchoServer && opts.GenerateChiServer {
Packit Service 3a6627
		errExit("can not specify both server and chi-server targets simultaneously")
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	swagger, err := util.LoadSwagger(flag.Arg(0))
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		errExit("error loading swagger spec\n: %s", err)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	templates, err := loadTemplateOverrides(templatesDir)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		errExit("error loading template overrides: %s\n", err)
Packit Service 3a6627
	}
Packit Service 3a6627
	opts.UserTemplates = templates
Packit Service 3a6627
Packit Service 3a6627
	if len(importMapping) > 0 {
Packit Service 3a6627
		opts.ImportMapping, err = util.ParseCommandlineMap(importMapping)
Packit Service 3a6627
		if err != nil {
Packit Service 3a6627
			errExit("error parsing import-mapping: %s\n", err)
Packit Service 3a6627
		}
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	code, err := codegen.Generate(swagger, packageName, opts)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		errExit("error generating code: %s\n", err)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	if outputFile != "" {
Packit Service 3a6627
		err = ioutil.WriteFile(outputFile, []byte(code), 0644)
Packit Service 3a6627
		if err != nil {
Packit Service 3a6627
			errExit("error writing generated code to file: %s", err)
Packit Service 3a6627
		}
Packit Service 3a6627
	} else {
Packit Service 3a6627
		fmt.Println(code)
Packit Service 3a6627
	}
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
func splitCSVArg(input string) []string {
Packit Service 3a6627
	input = strings.TrimSpace(input)
Packit Service 3a6627
	if len(input) == 0 {
Packit Service 3a6627
		return nil
Packit Service 3a6627
	}
Packit Service 3a6627
	splitInput := strings.Split(input, ",")
Packit Service 3a6627
	args := make([]string, 0, len(splitInput))
Packit Service 3a6627
	for _, s := range splitInput {
Packit Service 3a6627
		s = strings.TrimSpace(s)
Packit Service 3a6627
		if len(s) > 0 {
Packit Service 3a6627
			args = append(args, s)
Packit Service 3a6627
		}
Packit Service 3a6627
	}
Packit Service 3a6627
	return args
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
func loadTemplateOverrides(templatesDir string) (map[string]string, error) {
Packit Service 3a6627
	var templates = make(map[string]string)
Packit Service 3a6627
Packit Service 3a6627
	if templatesDir == "" {
Packit Service 3a6627
		return templates, nil
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	files, err := ioutil.ReadDir(templatesDir)
Packit Service 3a6627
	if err != nil {
Packit Service 3a6627
		return nil, err
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	for _, f := range files {
Packit Service 3a6627
		data, err := ioutil.ReadFile(path.Join(templatesDir, f.Name()))
Packit Service 3a6627
		if err != nil {
Packit Service 3a6627
			return nil, err
Packit Service 3a6627
		}
Packit Service 3a6627
		templates[f.Name()] = string(data)
Packit Service 3a6627
	}
Packit Service 3a6627
Packit Service 3a6627
	return templates, nil
Packit Service 3a6627
}