github.com/jenkins-x/jx-api@v0.0.24/cmd/codegen/app/generate_options.go (about)

     1  package app
     2  
     3  import (
     4  	"fmt"
     5  	"go/build"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/pkg/errors"
    10  	"github.com/spf13/cobra"
    11  
    12  	"github.com/jenkins-x/jx-api/cmd/codegen/util"
    13  )
    14  
    15  const (
    16  	optionGroupWithVersion = "group-with-version"
    17  	optionInputPackage     = "input-package"
    18  	optionOutputPackage    = "output-package"
    19  
    20  	optionInputBase       = "input-base"
    21  	optionOutputBase      = "output-base"
    22  	optionBoilerplateFile = "boilerplate-file"
    23  	optionModuleName      = "module-name"
    24  	global                = "global"
    25  	optionSemVer          = "semver"
    26  )
    27  
    28  // CommonOptions contains the common options
    29  type CommonOptions struct {
    30  	Args             []string
    31  	Cmd              *cobra.Command
    32  	LogLevel         string
    33  	GeneratorVersion string
    34  }
    35  
    36  // GenerateOptions contain common code generation options
    37  type GenerateOptions struct {
    38  	*CommonOptions
    39  	OutputBase          string
    40  	BoilerplateFile     string
    41  	GroupsWithVersions  []string
    42  	InputPackage        string
    43  	GoPathInputPackage  string
    44  	GoPathOutputPackage string
    45  	GoPathOutputBase    string
    46  	OutputPackage       string
    47  	InputBase           string
    48  	Global              bool
    49  	SemVer              string
    50  }
    51  
    52  func (o *GenerateOptions) configure() error {
    53  	err := util.EnsureGoPath()
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	inputPath := filepath.Join(o.InputBase, o.InputPackage)
    59  	outputPath := filepath.Join(o.OutputBase, o.OutputPackage)
    60  
    61  	if !strings.HasPrefix(inputPath, build.Default.GOPATH) {
    62  		return errors.Errorf("input %s is not in GOPATH (%s)", inputPath, build.Default.GOPATH)
    63  	}
    64  
    65  	if !strings.HasPrefix(outputPath, build.Default.GOPATH) {
    66  		return errors.Errorf("output %s is not in GOPATH (%s)", outputPath, build.Default.GOPATH)
    67  	}
    68  
    69  	// Work out the InputPackage relative to GOROOT
    70  	o.GoPathInputPackage = strings.TrimPrefix(inputPath,
    71  		fmt.Sprintf("%s/", filepath.Join(build.Default.GOPATH, "src")))
    72  
    73  	// Work out the OutputPackage relative to GOROOT
    74  	o.GoPathOutputPackage = strings.TrimPrefix(outputPath,
    75  		fmt.Sprintf("%s/", filepath.Join(build.Default.GOPATH, "src")))
    76  	return nil
    77  }