args.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2017 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. )
  18. // Args is used by the gengo framework to pass args specific to this generator.
  19. type Args struct {
  20. OutputDir string // must be a directory path
  21. OutputPkg string // must be a Go import-path
  22. GoHeaderFile string
  23. VersionedClientSetPackage string // must be a Go import-path
  24. InternalClientSetPackage string // must be a Go import-path
  25. ListersPackage string // must be a Go import-path
  26. SingleDirectory bool
  27. // PluralExceptions define a list of pluralizer exceptions in Type:PluralType format.
  28. // The default list is "Endpoints:Endpoints"
  29. PluralExceptions []string
  30. }
  31. // New returns default arguments for the generator.
  32. func New() *Args {
  33. return &Args{
  34. SingleDirectory: false,
  35. }
  36. }
  37. // AddFlags add the generator flags to the flag set.
  38. func (args *Args) AddFlags(fs *pflag.FlagSet) {
  39. fs.StringVar(&args.OutputDir, "output-dir", "",
  40. "the base directory under which to generate results")
  41. fs.StringVar(&args.OutputPkg, "output-pkg", args.OutputPkg,
  42. "the Go import-path of the generated results")
  43. fs.StringVar(&args.GoHeaderFile, "go-header-file", "",
  44. "the path to a file containing boilerplate header text; the string \"YEAR\" will be replaced with the current 4-digit year")
  45. fs.StringVar(&args.InternalClientSetPackage, "internal-clientset-package", args.InternalClientSetPackage,
  46. "the Go import-path of the internal clientset to use")
  47. fs.StringVar(&args.VersionedClientSetPackage, "versioned-clientset-package", args.VersionedClientSetPackage,
  48. "the Go import-path of the versioned clientset to use")
  49. fs.StringVar(&args.ListersPackage, "listers-package", args.ListersPackage,
  50. "the Go import-path of the listers to use")
  51. fs.BoolVar(&args.SingleDirectory, "single-directory", args.SingleDirectory,
  52. "if true, omit the intermediate \"internalversion\" and \"externalversions\" subdirectories")
  53. fs.StringSliceVar(&args.PluralExceptions, "plural-exceptions", args.PluralExceptions,
  54. "list of comma separated plural exception definitions in Type:PluralizedType format")
  55. }
  56. // Validate checks the given arguments.
  57. func (args *Args) Validate() error {
  58. if len(args.OutputDir) == 0 {
  59. return fmt.Errorf("--output-dir must be specified")
  60. }
  61. if len(args.OutputPkg) == 0 {
  62. return fmt.Errorf("--output-pkg must be specified")
  63. }
  64. if len(args.VersionedClientSetPackage) == 0 {
  65. return fmt.Errorf("--versioned-clientset-package must be specified")
  66. }
  67. if len(args.ListersPackage) == 0 {
  68. return fmt.Errorf("--listers-package must be specified")
  69. }
  70. return nil
  71. }