Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 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 | "k8s.io/gengo/args" |
| 24 | ) |
| 25 | |
| 26 | // CustomArgs is used by the gengo framework to pass args specific to this generator. |
| 27 | type CustomArgs struct { |
| 28 | // ReportFilename is added to CustomArgs for specifying name of report file used |
| 29 | // by API linter. If specified, API rule violations will be printed to report file. |
| 30 | // Otherwise default value "-" will be used which indicates stdout. |
| 31 | ReportFilename string |
| 32 | } |
| 33 | |
| 34 | // NewDefaults returns default arguments for the generator. Returning the arguments instead |
| 35 | // of using default flag parsing allows registering custom arguments afterwards |
| 36 | func NewDefaults() (*args.GeneratorArgs, *CustomArgs) { |
| 37 | // Default() sets a couple of flag default values for example the boilerplate. |
| 38 | // WithoutDefaultFlagParsing() disables implicit addition of command line flags and parsing, |
| 39 | // which allows registering custom arguments afterwards |
| 40 | genericArgs := args.Default().WithoutDefaultFlagParsing() |
| 41 | customArgs := &CustomArgs{} |
| 42 | genericArgs.CustomArgs = customArgs |
| 43 | |
| 44 | // Default value for report filename is "-", which stands for stdout |
| 45 | customArgs.ReportFilename = "-" |
| 46 | // Default value for output file base name |
| 47 | genericArgs.OutputFileBaseName = "openapi_generated" |
| 48 | |
| 49 | return genericArgs, customArgs |
| 50 | } |
| 51 | |
| 52 | // AddFlags add the generator flags to the flag set. |
| 53 | func (c *CustomArgs) AddFlags(fs *pflag.FlagSet) { |
| 54 | fs.StringVarP(&c.ReportFilename, "report-filename", "r", c.ReportFilename, "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.") |
| 55 | } |
| 56 | |
| 57 | // Validate checks the given arguments. |
| 58 | func Validate(genericArgs *args.GeneratorArgs) error { |
| 59 | c, ok := genericArgs.CustomArgs.(*CustomArgs) |
| 60 | if !ok { |
| 61 | return fmt.Errorf("input arguments don't contain valid custom arguments") |
| 62 | } |
| 63 | if len(c.ReportFilename) == 0 { |
| 64 | return fmt.Errorf("report filename cannot be empty. specify a valid filename or use \"-\" for stdout") |
| 65 | } |
| 66 | if len(genericArgs.OutputFileBaseName) == 0 { |
| 67 | return fmt.Errorf("output file base name cannot be empty") |
| 68 | } |
| 69 | if len(genericArgs.OutputPackagePath) == 0 { |
| 70 | return fmt.Errorf("output package cannot be empty") |
| 71 | } |
| 72 | return nil |
| 73 | } |