Debian Codegraph @

cmd/openapi-gen/args/args.go

raw
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package args
18
19 import (
20 "fmt"
21
22 "github.com/spf13/pflag"
23 )
24
25 type Args struct {
26 OutputDir string // must be a directory path
27 OutputPkg string // must be a Go import-path
28 OutputFile string
29
30 GoHeaderFile string
31
32 // ReportFilename is added to Args for specifying name of report file used
33 // by API linter. If specified, API rule violations will be printed to report file.
34 // Otherwise default value "-" will be used which indicates stdout.
35 ReportFilename string
36
37 // OutputModelNameFile is the name of the file to be generated for OpenAPI schema name
38 // accessor functions. If empty, no model name accessor functions are generated.
39 // When this is specified, the OpenAPI spec generator will use the function names
40 // instead of Go type names for schema names.
41 OutputModelNameFile string
42
43 // ReadOnlyPkgs is a list of package paths that should be treated as
44 // read-only. Model name files will not be generated for these packages.
45 // This prevents the generator from attempting to write into read-only
46 // dependency packages (e.g. in the Go module cache).
47 ReadOnlyPkgs []string
48 }
49
50 // New returns default arguments for the generator. Returning the arguments instead
51 // of using default flag parsing allows registering custom arguments afterwards
52 func New() *Args {
53 args := &Args{}
54
55 // Default value for report filename is "-", which stands for stdout
56 args.ReportFilename = "-"
57
58 return args
59 }
60
61 // AddFlags add the generator flags to the flag set.
62 func (args *Args) AddFlags(fs *pflag.FlagSet) {
63 fs.StringVar(&args.OutputDir, "output-dir", "",
64 "the base directory under which to generate results")
65 fs.StringVar(&args.OutputPkg, "output-pkg", "",
66 "the base Go import-path under which to generate results")
67 fs.StringVar(&args.OutputFile, "output-file", "generated.openapi.go",
68 "the name of the file to be generated")
69 fs.StringVar(&args.OutputModelNameFile, "output-model-name-file", "",
70 `The filename for generated model name accessor functions.
71 If specified, a file with this name will be created in each package containing
72 a "+k8s:openapi-model-package" tag. The generated functions return fully qualified
73 model names, which are used in the OpenAPI spec as schema references instead of
74 Go type names. If empty, no model name accessor functions are generated and names
75 are inferred from Go type names.`)
76 fs.StringSliceVar(&args.ReadOnlyPkgs, "readonly-pkg", nil,
77 "Packages that are read-only dependencies. Model name files will not be generated "+
78 "for these packages. May be specified multiple times.")
79 fs.StringVar(&args.GoHeaderFile, "go-header-file", "",
80 "the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year")
81 fs.StringVarP(&args.ReportFilename, "report-filename", "r", args.ReportFilename,
82 "Name of report file used by API linter to print API violations. Default \"-\" stands for standard output. NOTE that if valid filename other than \"-\" is specified, API linter won't return error on detected API violations. This allows further check of existing API violations without stopping the OpenAPI generation toolchain.")
83 }
84
85 // Validate checks the given arguments.
86 func (args *Args) Validate() error {
87 if len(args.OutputDir) == 0 {
88 return fmt.Errorf("--output-dir must be specified")
89 }
90 if len(args.OutputPkg) == 0 {
91 return fmt.Errorf("--output-pkg must be specified")
92 }
93 if len(args.OutputFile) == 0 {
94 return fmt.Errorf("--output-file must be specified")
95 }
96 if len(args.ReportFilename) == 0 {
97 return fmt.Errorf("--report-filename must be specified (use \"-\" for stdout)")
98 }
99 return nil
100 }