| 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 | |
| 38 | // New returns default arguments for the generator. Returning the arguments instead |
| 39 | // of using default flag parsing allows registering custom arguments afterwards |
| 40 | func New() *Args { |
| 41 | args := &Args{} |
| 42 | |
| 43 | // Default value for report filename is "-", which stands for stdout |
| 44 | args.ReportFilename = "-" |
| 45 | |
| 46 | return args |
| 47 | } |
| 48 | |
| 49 | // AddFlags add the generator flags to the flag set. |
| 50 | func (args *Args) AddFlags(fs *pflag.FlagSet) { |
| 51 | fs.StringVar(&args.OutputDir, "output-dir", "", |
| 52 | "the base directory under which to generate results") |
| 53 | fs.StringVar(&args.OutputPkg, "output-pkg", "", |
| 54 | "the base Go import-path under which to generate results") |
| 55 | fs.StringVar(&args.OutputFile, "output-file", "generated.openapi.go", |
| 56 | "the name of the file to be generated") |
| 57 | fs.StringVar(&args.GoHeaderFile, "go-header-file", "", |
| 58 | "the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year") |
| 59 | fs.StringVarP(&args.ReportFilename, "report-filename", "r", args.ReportFilename, |
| 60 | "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.") |
| 61 | } |
| 62 | |
| 63 | // Validate checks the given arguments. |
| 64 | func (args *Args) Validate() error { |
| 65 | if len(args.OutputDir) == 0 { |
| 66 | return fmt.Errorf("--output-dir must be specified") |
| 67 | } |
| 68 | if len(args.OutputPkg) == 0 { |
| 69 | return fmt.Errorf("--output-pkg must be specified") |
| 70 | } |
| 71 | if len(args.OutputFile) == 0 { |
| 72 | return fmt.Errorf("--output-file must be specified") |
| 73 | } |
| 74 | if len(args.ReportFilename) == 0 { |
| 75 | return fmt.Errorf("--report-filename must be specified (use \"-\" for stdout)") |
| 76 | } |
| 77 | return nil |
| 78 | } |