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

     1  package generator
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/jenkins-x/jx/v2/cmd/codegen/util"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func defaultGenerate(generator string, name string, groupsWithVersions []string, inputPackage string,
    13  	outputPackage string, outputBase string, boilerplateFile string, gopath string, args ...string) error {
    14  	util.AppLogger().Infof("generating %s structs for %s at %s\n", name, groupsWithVersions, outputPackage)
    15  
    16  	generateCommand := util.Command{
    17  		Name: filepath.Join(util.GoPathBin(gopath), generator),
    18  		Args: []string{
    19  			"--output-base",
    20  			outputBase,
    21  			"--go-header-file",
    22  			boilerplateFile,
    23  		},
    24  		Env: map[string]string{
    25  			"GO111MODULE": "on",
    26  		},
    27  	}
    28  	if name == "clientset" {
    29  		inputDirs := make([]string, 0)
    30  		for _, gv := range groupsWithVersions {
    31  			groupVersion := strings.Split(gv, ":")
    32  			if len(groupVersion) != 2 {
    33  				return errors.Errorf("argument %s must be like cheese:v1", gv)
    34  			}
    35  			inputDirs = append(inputDirs, fmt.Sprintf("%s/%s", groupVersion[0], groupVersion[1]))
    36  		}
    37  		inputDirsStr := strings.Join(inputDirs, ",")
    38  		generateCommand.Args = append(generateCommand.Args, "--input", inputDirsStr, "--input-base", inputPackage)
    39  	} else {
    40  		inputDirs, err := packagesForGroupsWithVersions(inputPackage, groupsWithVersions)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		inputDirsStr := strings.Join(inputDirs, ",")
    45  		generateCommand.Args = append(generateCommand.Args, "--input-dirs", inputDirsStr)
    46  	}
    47  	for _, arg := range args {
    48  		generateCommand.Args = append(generateCommand.Args, arg)
    49  	}
    50  	out, err := generateCommand.RunWithoutRetry()
    51  	if err != nil {
    52  		return errors.Wrapf(err, "running %s, output %s", generateCommand.String(), out)
    53  	}
    54  	return nil
    55  }
    56  
    57  func packagesForGroupsWithVersions(inputPackage string, groupsWithVersions []string) ([]string, error) {
    58  	inputDirs := make([]string, 0)
    59  	for _, gv := range groupsWithVersions {
    60  		groupVersion := strings.Split(gv, ":")
    61  		if len(groupVersion) != 2 {
    62  			return nil, errors.Errorf("argument %s must be like cheese:v1", gv)
    63  		}
    64  		inputDirs = append(inputDirs, fmt.Sprintf("%s/%s/%s", inputPackage, groupVersion[0], groupVersion[1]))
    65  	}
    66  	return inputDirs, nil
    67  }