knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/codegen/cmd/injection-gen/args/args.go (about)

     1  /*
     2  Copyright 2019 The Knative 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  package args
    18  
    19  import (
    20  	"errors"
    21  	"path/filepath"
    22  
    23  	"k8s.io/gengo/v2"
    24  	"k8s.io/klog/v2"
    25  
    26  	"github.com/spf13/pflag"
    27  )
    28  
    29  // Args is used by the gengo framework to pass args specific to this generator.
    30  type Args struct {
    31  	InputDirs         []string
    32  	OutputPackagePath string
    33  	OutputDir         string
    34  	GoHeaderFilePath  string
    35  
    36  	VersionedClientSetPackage        string
    37  	ExternalVersionsInformersPackage string
    38  	ListersPackage                   string
    39  	ForceKinds                       string
    40  	ListerHasPointerElem             bool
    41  	DisableInformerInit              bool
    42  
    43  	// PluralExceptions define a list of pluralizer exceptions in Type:PluralType format.
    44  	// The default list is "Endpoints:Endpoints"
    45  	PluralExceptions []string
    46  
    47  	Boilerplate []byte
    48  }
    49  
    50  func New() *Args {
    51  	return &Args{}
    52  }
    53  
    54  // AddFlags add the generator flags to the flag set.
    55  func (a *Args) AddFlags(fs *pflag.FlagSet) {
    56  	fs.StringSliceVarP(&a.InputDirs, "input-dirs", "i", a.InputDirs, "Comma-separated list of import paths to get input types from.")
    57  	fs.StringVarP(&a.OutputPackagePath, "output-package", "p", a.OutputPackagePath, "Base package path.")
    58  	fs.StringVarP(&a.OutputDir, "output-dir", "o", a.OutputDir, "Output directory.")
    59  	fs.StringVarP(&a.GoHeaderFilePath, "go-header-file", "h", a.GoHeaderFilePath, "File containing boilerplate header text. The string YEAR will be replaced with the current 4-digit year.")
    60  
    61  	fs.StringVar(&a.VersionedClientSetPackage, "versioned-clientset-package", a.VersionedClientSetPackage, "the full package name for the versioned injection clientset to use")
    62  	fs.StringVar(&a.ExternalVersionsInformersPackage, "external-versions-informers-package", a.ExternalVersionsInformersPackage, "the full package name for the external versions injection informer to use")
    63  	fs.StringVar(&a.ListersPackage, "listers-package", a.ListersPackage, "the full package name for client listers to use")
    64  	fs.StringVar(&a.ForceKinds, "force-genreconciler-kinds", a.ForceKinds, `force kinds will override the genreconciler tag setting for the given set of kinds, comma separated: "Foo,Bar,Baz"`)
    65  
    66  	fs.BoolVar(&a.ListerHasPointerElem, "lister-has-pointer-elem", false, "")
    67  	fs.MarkDeprecated("lister-has-pointer-elem", "this flag has no effect")
    68  
    69  	fs.BoolVar(&a.DisableInformerInit, "disable-informer-init", false, "disable generating the init function for the informer")
    70  	fs.StringSliceVar(&a.PluralExceptions, "plural-exceptions", a.PluralExceptions,
    71  		"list of comma separated plural exception definitions in Type:PluralizedType format")
    72  }
    73  
    74  // Validate checks the given arguments.
    75  func (a *Args) Validate() error {
    76  	var err error
    77  	a.Boilerplate, err = gengo.GoBoilerplate(a.GoHeaderFilePath, "", gengo.StdGeneratedBy)
    78  	if err != nil {
    79  		klog.Fatalf("Failed loading boilerplate: %v", err)
    80  	}
    81  
    82  	if len(a.OutputPackagePath) == 0 {
    83  		return errors.New("output package cannot be empty")
    84  	}
    85  	if len(a.VersionedClientSetPackage) == 0 {
    86  		return errors.New("versioned clientset package cannot be empty")
    87  	}
    88  	if len(a.ExternalVersionsInformersPackage) == 0 {
    89  		return errors.New("external versions informers package cannot be empty")
    90  	}
    91  	return nil
    92  }
    93  
    94  func (a *Args) GetOutputDir() string {
    95  	return filepath.Clean(a.OutputDir)
    96  }
    97  
    98  func (a *Args) GetOutputPackagePath() string {
    99  	return filepath.Clean(a.OutputPackagePath)
   100  }