| 1 |
/* |
| 2 |
Copyright 2016 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 |
// defaulter-gen is a tool for auto-generating Defaulter functions. |
| 18 |
// |
| 19 |
// Given a list of input directories, it will scan for top level types |
| 20 |
// and generate efficient defaulters for an entire object from the sum |
| 21 |
// of the SetDefault_* methods contained in the object tree. |
| 22 |
// |
| 23 |
// Generation is governed by comment tags in the source. Any package may |
| 24 |
// request defaulter generation by including one or more comment tags at |
| 25 |
// the package comment level: |
| 26 |
// |
| 27 |
// // +k8s:defaulter-gen=<field-name-to-flag> |
| 28 |
// |
| 29 |
// which will create defaulters for any type that contains the provided |
| 30 |
// field name (if the type has defaulters). Any type may request explicit |
| 31 |
// defaulting by providing the comment tag: |
| 32 |
// |
| 33 |
// // +k8s:defaulter-gen=true|false |
| 34 |
// |
| 35 |
// An existing defaulter method (`SetDefaults_TYPE`) can provide the |
| 36 |
// comment tag: |
| 37 |
// |
| 38 |
// // +k8s:defaulter-gen=covers |
| 39 |
// |
| 40 |
// to indicate that the defaulter does not or should not call any nested |
| 41 |
// defaulters. |
| 42 |
package main |
| 43 |
|
| 44 |
import ( |
| 45 |
"k8s.io/gengo/args" |
| 46 |
"k8s.io/gengo/examples/defaulter-gen/generators" |
| 47 |
|
| 48 |
"github.com/spf13/pflag" |
| 49 |
"k8s.io/klog/v2" |
| 50 |
) |
| 51 |
|
| 52 |
func main() { |
| 53 |
klog.InitFlags(nil) |
| 54 |
arguments := args.Default() |
| 55 |
|
| 56 |
// Override defaults. |
| 57 |
arguments.OutputFileBaseName = "defaulter_generated" |
| 58 |
|
| 59 |
// Custom args. |
| 60 |
customArgs := &generators.CustomArgs{ |
| 61 |
ExtraPeerDirs: []string{}, |
| 62 |
} |
| 63 |
pflag.CommandLine.StringSliceVar(&customArgs.ExtraPeerDirs, "extra-peer-dirs", customArgs.ExtraPeerDirs, |
| 64 |
"Comma-separated list of import paths which are considered, after tag-specified peers, for conversions.") |
| 65 |
arguments.CustomArgs = customArgs |
| 66 |
|
| 67 |
// Run it. |
| 68 |
if err := arguments.Execute( |
| 69 |
generators.NameSystems(), |
| 70 |
generators.DefaultNameSystem(), |
| 71 |
generators.Packages, |
| 72 |
); err != nil { |
| 73 |
klog.Fatalf("Error: %v", err) |
| 74 |
} |
| 75 |
klog.V(2).Info("Completed successfully.") |
| 76 |
} |