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