github.com/galaxyobe/gen@v0.0.0-20220910125335-392fa8f0990f/cmd/deepcopy-gen/main.go (about)

     1  /*
     2  Copyright 2015 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  // deepcopy-gen is a tool for auto-generating DeepCopy functions.
    18  //
    19  // Given a list of input directories, it will generate DeepCopy and DeepCopyInto
    20  // methods that efficiently perform a full deep-copy of each type. If these
    21  // already exist (are predefined by the developer), they are used instead of
    22  // generating new ones.
    23  //
    24  // If interfaces are referenced in types, it is expected that corresponding
    25  // DeepCopyInterfaceName methods exist, e.g. DeepCopyObject for runtime.Object.
    26  // These can be predefined by the developer or generated through tags, see below.
    27  // They must be added to the interfaces themselves manually, e.g.
    28  //
    29  //	type Object interface {
    30  //	  ...
    31  //	  DeepCopyObject() Object
    32  //	}
    33  //
    34  // All generation is governed by comment tags in the source.  Any package may
    35  // request DeepCopy generation by including a comment in the file-comments of
    36  // a doc.go file, of the form:
    37  //
    38  //	// +gen:deepcopy=package
    39  //
    40  // DeepCopy functions can be generated for individual types, rather than the
    41  // entire package by specifying a comment on the type definition of the form:
    42  //
    43  //	// +gen:deepcopy=true
    44  //
    45  // When generating for a whole package, individual types may opt out of
    46  // DeepCopy generation by specifying a comment on the type definition of the form:
    47  //
    48  //	// +gen:deepcopy=false
    49  //
    50  // Additional DeepCopyInterfaceName methods can be generated by specifying a
    51  // comment on the type definition of the form:
    52  //
    53  //	// +gen:deepcopy:interfaces=k8s.io/kubernetes/runtime.Object,k8s.io/kubernetes/runtime.List
    54  //
    55  // This leads to the generation of DeepCopyObject and DeepCopyList with the given
    56  // interfaces as return types. We say that the tagged type implements deepcopy for the
    57  // interfaces.
    58  //
    59  // The deepcopy funcs for interfaces using "+gen:deepcopy:interfaces" use the pointer
    60  // of the type as receiver. For those special cases where the non-pointer object should
    61  // implement the interface, this can be done with:
    62  //
    63  //	// +gen:deepcopy:nonpointer-interfaces=true
    64  package main
    65  
    66  import (
    67  	"k8s.io/gengo/args"
    68  
    69  	"github.com/spf13/pflag"
    70  	"k8s.io/klog/v2"
    71  
    72  	"github.com/galaxyobe/gen/cmd/deepcopy-gen/generators"
    73  	"github.com/galaxyobe/gen/pkg/util"
    74  )
    75  
    76  // -v 7 -i github.com/galaxyobe/gen/cmd/deepcopy-gen/output_tests/... --trim-path-prefix github.com/galaxyobe/gen/cmd/deepcopy-gen -o .
    77  func main() {
    78  	klog.InitFlags(nil)
    79  	arguments := args.Default()
    80  
    81  	// Override defaults.
    82  	arguments.GoHeaderFilePath = util.BoilerplatePath()
    83  	arguments.OutputFileBaseName = "deepcopy_generated"
    84  
    85  	// Custom custom_args.
    86  	customArgs := &generators.CustomArgs{}
    87  	customArgs.AddFlags(pflag.CommandLine)
    88  
    89  	arguments.CustomArgs = customArgs
    90  
    91  	// Validate checks the given arguments.
    92  	if len(arguments.OutputFileBaseName) == 0 {
    93  		klog.Fatalf("output file base name cannot be empty")
    94  	}
    95  
    96  	// Run it.
    97  	if err := arguments.Execute(
    98  		generators.NameSystems(),
    99  		generators.DefaultNameSystem(),
   100  		generators.Packages,
   101  	); err != nil {
   102  		klog.Fatalf("Error: %v", err)
   103  	}
   104  	klog.V(2).Info("Completed successfully.")
   105  }