args.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package args
  14. import (
  15. "fmt"
  16. "github.com/spf13/pflag"
  17. "k8s.io/gengo/args"
  18. )
  19. // CustomArgs is used by the gengo framework to pass args specific to this generator.
  20. type CustomArgs struct {
  21. // ReportFilename is added to CustomArgs for specifying name of report file used
  22. // by API linter. If specified, API rule violations will be printed to report file.
  23. // Otherwise default value "-" will be used which indicates stdout.
  24. ReportFilename string
  25. }
  26. // NewDefaults returns default arguments for the generator. Returning the arguments instead
  27. // of using default flag parsing allows registering custom arguments afterwards
  28. func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
  29. // Default() sets a couple of flag default values for example the boilerplate.
  30. // WithoutDefaultFlagParsing() disables implicit addition of command line flags and parsing,
  31. // which allows registering custom arguments afterwards
  32. genericArgs := args.Default().WithoutDefaultFlagParsing()
  33. customArgs := &CustomArgs{}
  34. genericArgs.CustomArgs = customArgs
  35. // Default value for report filename is "-", which stands for stdout
  36. customArgs.ReportFilename = "-"
  37. // Default value for output file base name
  38. genericArgs.OutputFileBaseName = "openapi_generated"
  39. return genericArgs, customArgs
  40. }
  41. // AddFlags add the generator flags to the flag set.
  42. func (c *CustomArgs) AddFlags(fs *pflag.FlagSet) {
  43. 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.")
  44. }
  45. // Validate checks the given arguments.
  46. func Validate(genericArgs *args.GeneratorArgs) error {
  47. c, ok := genericArgs.CustomArgs.(*CustomArgs)
  48. if !ok {
  49. return fmt.Errorf("input arguments don't contain valid custom arguments")
  50. }
  51. if len(c.ReportFilename) == 0 {
  52. return fmt.Errorf("report filename cannot be empty. specify a valid filename or use \"-\" for stdout")
  53. }
  54. if len(genericArgs.OutputFileBaseName) == 0 {
  55. return fmt.Errorf("output file base name cannot be empty")
  56. }
  57. if len(genericArgs.OutputPackagePath) == 0 {
  58. return fmt.Errorf("output package cannot be empty")
  59. }
  60. return nil
  61. }