github.com/jenkins-x/jx/v2@v2.1.155/cmd/codegen/app/generate_clientset.go (about)

     1  package app
     2  
     3  import (
     4  	"go/build"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/jenkins-x/jx/v2/cmd/codegen/generator"
     9  	"github.com/jenkins-x/jx/v2/cmd/codegen/util"
    10  
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  // ClientSetGenerationOptions contain the options for the clientset generation.
    17  type ClientSetGenerationOptions struct {
    18  	GenerateOptions
    19  	Generators []string
    20  }
    21  
    22  var (
    23  	createClientGoLong = `This command code generates clients for the specified custom resources.`
    24  
    25  	createClientGoExample = `
    26  # lets generate a client
    27  codegen clientset
    28  	--output-package=github.com/jenkins-x/jx/v2/pkg/client \
    29  	--input-package=github.com/jenkins-x/pkg-apis \
    30  	--group-with-version=jenkins.io:v1
    31  
    32  # You will normally want to add a target to your Makefile that looks like
    33  generate-clients:
    34  	codegen clientset
    35  		--output-package=github.com/jenkins-x/jx/v2/pkg/client \
    36  		--input-package=github.com/jenkins-x/jx/v2/pkg/apis \
    37  		--group-with-version=jenkins.io:v1
    38  
    39  # and then call
    40  make generate-clien
    41  `
    42  )
    43  
    44  // NewGenerateClientSetCmd creates the command
    45  func NewGenerateClientSetCmd(genOpts GenerateOptions) *cobra.Command {
    46  	o := &ClientSetGenerationOptions{
    47  		GenerateOptions: genOpts,
    48  	}
    49  
    50  	cobraCmd := &cobra.Command{
    51  		Use:     "clientset",
    52  		Short:   "Creates Go client for Custom Resources",
    53  		Long:    createClientGoLong,
    54  		Example: createClientGoExample,
    55  
    56  		Run: func(c *cobra.Command, args []string) {
    57  			o.Cmd = c
    58  			o.Args = args
    59  			err := o.Run()
    60  			util.CheckErr(err)
    61  		},
    62  	}
    63  
    64  	availableGenerators := []string{
    65  		"deepcopy",
    66  		"clientset",
    67  		"listers",
    68  		"informers",
    69  	}
    70  
    71  	wd, err := os.Getwd()
    72  	if err != nil {
    73  		util.AppLogger().Warnf("Error getting working directory for %v\n", err)
    74  	}
    75  
    76  	cobraCmd.Flags().StringArrayVarP(&o.Generators, "generator", "", availableGenerators, "Enable a generator")
    77  	cobraCmd.Flags().StringVarP(&o.OutputBase, "output-base", "", wd, "Output base directory, "+
    78  		"by the current working directory")
    79  	cobraCmd.Flags().StringVarP(&o.BoilerplateFile, optionBoilerplateFile, "", "custom-boilerplate.go.txt",
    80  		"Custom boilerplate to add to all files if the file is missing it will be ignored")
    81  	cobraCmd.Flags().StringArrayVarP(&o.GroupsWithVersions, optionGroupWithVersion, "g", make([]string, 0),
    82  		"group name:version (e.g. jenkins.io:v1) to generate, must specify at least once")
    83  	cobraCmd.Flags().StringVarP(&o.InputPackage, optionInputPackage, "i", "", "Input package, must specify")
    84  	cobraCmd.Flags().StringVarP(&o.OutputPackage, optionOutputPackage, "o", "", "Output package, must specify")
    85  	cobraCmd.Flags().StringVarP(&o.InputBase, optionInputBase, "", wd, "Input base, defaults working directory")
    86  	cobraCmd.Flags().BoolVarP(&o.Global, global, "", false, "use the users GOPATH")
    87  	cobraCmd.Flags().StringVarP(&o.SemVer, optionSemVer, "", "", "semantic version to use in packages")
    88  	return cobraCmd
    89  }
    90  
    91  // Run executes this command.
    92  func (o *ClientSetGenerationOptions) Run() error {
    93  	var err error
    94  	o.BoilerplateFile, err = generator.GetBoilerplateFile(o.BoilerplateFile)
    95  	if err != nil {
    96  		return errors.Wrapf(err, "reading file %s specified by %s", o.BoilerplateFile, optionBoilerplateFile)
    97  	}
    98  	if len(o.GroupsWithVersions) < 1 {
    99  		return util.InvalidOptionf(optionGroupWithVersion, o.GroupsWithVersions, "must specify at least once")
   100  	}
   101  	if o.InputPackage == "" {
   102  		return util.MissingOption(optionInputPackage)
   103  	}
   104  	if o.OutputPackage == "" {
   105  		return util.MissingOption(optionOutputPackage)
   106  	}
   107  
   108  	err = o.configure()
   109  	if err != nil {
   110  		return errors.Wrapf(err, "ensure GOPATH is set correctly")
   111  	}
   112  
   113  	cleanupFunc := func() {}
   114  	gopath := util.GoPath()
   115  	if !o.Global {
   116  		gopath, err = util.IsolatedGoPath()
   117  		if err != nil {
   118  			return errors.Wrapf(err, "getting isolated gopath")
   119  		}
   120  		cleanupFunc, err = util.BackupGoModAndGoSum()
   121  		if err != nil {
   122  			return errors.Wrapf(err, "backing up go.mod and go.sum")
   123  		}
   124  	}
   125  	defer cleanupFunc()
   126  
   127  	err = generator.InstallCodeGenerators(o.GeneratorVersion, gopath)
   128  	if err != nil {
   129  		return errors.Wrapf(err, "installing kubernetes code generator tools")
   130  	}
   131  	util.AppLogger().Infof("generating Go code to %s in package %s from package %s\n", o.OutputBase, o.GoPathOutputPackage, o.GoPathInputPackage)
   132  	return generator.GenerateClient(o.Generators, o.GroupsWithVersions, o.GoPathInputPackage, o.GoPathOutputPackage, o.OutputPackage,
   133  		filepath.Join(build.Default.GOPATH, "src"), o.BoilerplateFile, gopath, o.SemVer)
   134  }