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

     1  /*
     2   Copyright 2022 Galaxyobe.
     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  // setter-gen is a tool for auto-generating set struct field functions.
    18  //
    19  // Given a list of input directories, it will generate SetField methods.
    20  // If these already exist (are predefined by the developer), they are used
    21  // instead of generating new ones.
    22  //
    23  // All generation is governed by comment tags in the source.  Any package may
    24  // request Getter generation by including a comment in the file-comments of
    25  // a doc.go file, of the form:
    26  //
    27  //	// +gen:setter=package
    28  //
    29  // Getter functions can be generated for individual types, rather than the
    30  // entire package by specifying a comment on the type or filed definition of the form:
    31  //
    32  //	// +gen:setter=true|false
    33  //
    34  // You can not participate in the generation by setting the field a
    35  // comment on the type definition of the form:
    36  //
    37  //	type Struct struct {
    38  //		// +setter=false
    39  //		Field string
    40  //	}
    41  //
    42  // You can also specify the field name that needs to be generated a
    43  // comment on the type definition of the form:
    44  //
    45  //	// +gen:setter:fields=field1,field2,field3
    46  package main
    47  
    48  import (
    49  	"k8s.io/gengo/args"
    50  
    51  	"github.com/spf13/pflag"
    52  	"k8s.io/klog/v2"
    53  
    54  	"github.com/galaxyobe/gen/cmd/setter-gen/generators"
    55  	custom_args "github.com/galaxyobe/gen/pkg/custom_args"
    56  	"github.com/galaxyobe/gen/pkg/util"
    57  )
    58  
    59  // main setter-gen at project root, program flag:
    60  // -v 7 -i github.com/galaxyobe/gen/cmd/setter-gen/output_tests/... --trim-path-prefix github.com/galaxyobe/gen -o .
    61  func main() {
    62  	klog.InitFlags(nil)
    63  	arguments := args.Default()
    64  
    65  	// Override defaults.
    66  	arguments.GoHeaderFilePath = util.BoilerplatePath()
    67  	arguments.OutputFileBaseName = "setter_generated"
    68  
    69  	// Custom custom_args.
    70  	customArgs := custom_args.NewCustomArgs(arguments)
    71  	customArgs.AddFlags(pflag.CommandLine)
    72  
    73  	// Validate checks the given arguments.
    74  	if len(arguments.OutputFileBaseName) == 0 {
    75  		klog.Fatalf("output file base name cannot be empty")
    76  	}
    77  
    78  	// Run it.
    79  	if err := arguments.Execute(
    80  		generators.NameSystems(),
    81  		generators.DefaultNameSystem(),
    82  		generators.Packages,
    83  	); err != nil {
    84  		klog.Fatalf("Error: %v", err)
    85  	}
    86  	klog.V(2).Info("Completed successfully.")
    87  }