github.com/galaxyobe/gen@v0.0.0-20220910125335-392fa8f0990f/cmd/option-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  // option-gen is a tool for auto-generating with struct field functions.
    18  //
    19  // option-gen can be generated for individual types, rather than the
    20  // entire package by specifying a comment on the type or filed definition of the form:
    21  //
    22  //	// +gen:option=true
    23  //
    24  // You can not participate in the generation by setting the field a
    25  // comment on the type definition of the form:
    26  //
    27  //	// +gen:option=true
    28  //	type Struct struct {
    29  //		// +option=false
    30  //		Field string
    31  //	}
    32  //
    33  // You can also specify the field name that needs to be generated a
    34  // comment on the type definition of the form:
    35  //
    36  //	// +gen:option:fields=field1,field2,field3
    37  //
    38  // Option names and suffixes can be customized by setting the field a
    39  // comment on the type definition of the form:
    40  //
    41  //	// +gen:option:name=StructOptionName
    42  //
    43  //	// +gen:option:suffix=StructOptionNameSuffix
    44  package main
    45  
    46  import (
    47  	"k8s.io/gengo/args"
    48  
    49  	"github.com/spf13/pflag"
    50  	"k8s.io/klog/v2"
    51  
    52  	"github.com/galaxyobe/gen/cmd/option-gen/generators"
    53  	custom_args "github.com/galaxyobe/gen/pkg/custom_args"
    54  	"github.com/galaxyobe/gen/pkg/util"
    55  )
    56  
    57  // main option-gen at project root, program flag:
    58  // -v 7 -i github.com/galaxyobe/gen/cmd/option-gen/output_tests/... --trim-path-prefix github.com/galaxyobe/gen -o .
    59  func main() {
    60  	klog.InitFlags(nil)
    61  	arguments := args.Default()
    62  
    63  	// Override defaults.
    64  	arguments.GoHeaderFilePath = util.BoilerplatePath()
    65  	arguments.OutputFileBaseName = "option_generated"
    66  
    67  	// Custom custom_args.
    68  	customArgs := custom_args.NewCustomArgs(arguments)
    69  	customArgs.AddFlags(pflag.CommandLine)
    70  
    71  	// Validate checks the given arguments.
    72  	if len(arguments.OutputFileBaseName) == 0 {
    73  		klog.Fatalf("output file base name cannot be empty")
    74  	}
    75  
    76  	// Run it.
    77  	if err := arguments.Execute(
    78  		generators.NameSystems(),
    79  		generators.DefaultNameSystem(),
    80  		generators.Packages,
    81  	); err != nil {
    82  		klog.Fatalf("Error: %v", err)
    83  	}
    84  	klog.V(2).Info("Completed successfully.")
    85  }