args.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "path"
  17. "github.com/spf13/pflag"
  18. codegenutil "k8s.io/code-generator/pkg/util"
  19. "k8s.io/gengo/args"
  20. )
  21. // CustomArgs is used by the gengo framework to pass args specific to this generator.
  22. type CustomArgs struct {
  23. VersionedClientSetPackage string
  24. InternalClientSetPackage string
  25. ListersPackage string
  26. SingleDirectory bool
  27. }
  28. // NewDefaults returns default arguments for the generator.
  29. func NewDefaults() (*args.GeneratorArgs, *CustomArgs) {
  30. genericArgs := args.Default().WithoutDefaultFlagParsing()
  31. customArgs := &CustomArgs{
  32. SingleDirectory: false,
  33. }
  34. genericArgs.CustomArgs = customArgs
  35. if pkg := codegenutil.CurrentPackage(); len(pkg) != 0 {
  36. genericArgs.OutputPackagePath = path.Join(pkg, "pkg/client/informers")
  37. customArgs.VersionedClientSetPackage = path.Join(pkg, "pkg/client/clientset/versioned")
  38. customArgs.InternalClientSetPackage = path.Join(pkg, "pkg/client/clientset/internalversion")
  39. customArgs.ListersPackage = path.Join(pkg, "pkg/client/listers")
  40. }
  41. return genericArgs, customArgs
  42. }
  43. // AddFlags add the generator flags to the flag set.
  44. func (ca *CustomArgs) AddFlags(fs *pflag.FlagSet) {
  45. fs.StringVar(&ca.InternalClientSetPackage, "internal-clientset-package", ca.InternalClientSetPackage, "the full package name for the internal clientset to use")
  46. fs.StringVar(&ca.VersionedClientSetPackage, "versioned-clientset-package", ca.VersionedClientSetPackage, "the full package name for the versioned clientset to use")
  47. fs.StringVar(&ca.ListersPackage, "listers-package", ca.ListersPackage, "the full package name for the listers to use")
  48. fs.BoolVar(&ca.SingleDirectory, "single-directory", ca.SingleDirectory, "if true, omit the intermediate \"internalversion\" and \"externalversions\" subdirectories")
  49. }
  50. // Validate checks the given arguments.
  51. func Validate(genericArgs *args.GeneratorArgs) error {
  52. customArgs := genericArgs.CustomArgs.(*CustomArgs)
  53. if len(genericArgs.OutputPackagePath) == 0 {
  54. return fmt.Errorf("output package cannot be empty")
  55. }
  56. if len(customArgs.VersionedClientSetPackage) == 0 {
  57. return fmt.Errorf("versioned clientset package cannot be empty")
  58. }
  59. if len(customArgs.ListersPackage) == 0 {
  60. return fmt.Errorf("listers package cannot be empty")
  61. }
  62. return nil
  63. }