main.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2015 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. // deepcopy-gen is a tool for auto-generating DeepCopy functions.
  14. //
  15. // Given a list of input directories, it will generate functions that
  16. // efficiently perform a full deep-copy of each type. For any type that
  17. // offers a `.DeepCopy()` method, it will simply call that. Otherwise it will
  18. // use standard value assignment whenever possible. If that is not possible it
  19. // will try to call its own generated copy function for the type, if the type is
  20. // within the allowed root packages. Failing that, it will fall back on
  21. // `conversion.Cloner.DeepCopy(val)` to make the copy. The resulting file will
  22. // be stored in the same directory as the processed source package.
  23. //
  24. // Generation is governed by comment tags in the source. Any package may
  25. // request DeepCopy generation by including a comment in the file-comments of
  26. // one file, of the form:
  27. // // +k8s:deepcopy-gen=package
  28. //
  29. // DeepCopy functions can be generated for individual types, rather than the
  30. // entire package by specifying a comment on the type definion of the form:
  31. // // +k8s:deepcopy-gen=true
  32. //
  33. // When generating for a whole package, individual types may opt out of
  34. // DeepCopy generation by specifying a comment on the of the form:
  35. // // +k8s:deepcopy-gen=false
  36. //
  37. // Note that registration is a whole-package option, and is not available for
  38. // individual types.
  39. package main
  40. import (
  41. "flag"
  42. "path/filepath"
  43. "github.com/spf13/pflag"
  44. "k8s.io/gengo/args"
  45. "k8s.io/gengo/examples/deepcopy-gen/generators"
  46. "k8s.io/klog/v2"
  47. generatorargs "k8s.io/code-generator/cmd/deepcopy-gen/args"
  48. "k8s.io/code-generator/pkg/util"
  49. )
  50. func main() {
  51. klog.InitFlags(nil)
  52. genericArgs, customArgs := generatorargs.NewDefaults()
  53. // Override defaults.
  54. // TODO: move this out of deepcopy-gen
  55. genericArgs.GoHeaderFilePath = filepath.Join(args.DefaultSourceTree(), util.BoilerplatePath())
  56. genericArgs.AddFlags(pflag.CommandLine)
  57. customArgs.AddFlags(pflag.CommandLine)
  58. flag.Set("logtostderr", "true")
  59. pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
  60. pflag.Parse()
  61. if err := generatorargs.Validate(genericArgs); err != nil {
  62. klog.Fatalf("Error: %v", err)
  63. }
  64. // Run it.
  65. if err := genericArgs.Execute(
  66. generators.NameSystems(),
  67. generators.DefaultNameSystem(),
  68. generators.Packages,
  69. ); err != nil {
  70. klog.Fatalf("Error: %v", err)
  71. }
  72. klog.V(2).Info("Completed successfully.")
  73. }