github.com/jenkins-x/jx/v2@v2.1.155/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/sirupsen/logrus" 11 "github.com/spf13/cobra" 12 13 "github.com/jenkins-x/jx/v2/cmd/codegen/util" 14 ) 15 16 const ( 17 optionGroupWithVersion = "group-with-version" 18 optionInputPackage = "input-package" 19 optionOutputPackage = "output-package" 20 21 optionInputBase = "input-base" 22 optionOutputBase = "output-base" 23 optionBoilerplateFile = "boilerplate-file" 24 optionModuleName = "module-name" 25 global = "global" 26 optionVerbose = "verbose" 27 optionSemVer = "semver" 28 ) 29 30 // CommonOptions contains the common options 31 type CommonOptions struct { 32 Args []string 33 Cmd *cobra.Command 34 LogLevel string 35 GeneratorVersion string 36 Verbose bool 37 } 38 39 // GenerateOptions contain common code generation options 40 type GenerateOptions struct { 41 *CommonOptions 42 OutputBase string 43 BoilerplateFile string 44 GroupsWithVersions []string 45 InputPackage string 46 GoPathInputPackage string 47 GoPathOutputPackage string 48 GoPathOutputBase string 49 OutputPackage string 50 InputBase string 51 Global bool 52 SemVer string 53 } 54 55 func (o *GenerateOptions) configure() error { 56 err := util.SetLevel(o.LogLevel) 57 if err != nil { 58 return errors.Wrapf(err, "setting log level to %s", o.LogLevel) 59 } 60 61 if o.Verbose { 62 err := util.SetLevel(logrus.DebugLevel.String()) 63 if err != nil { 64 return errors.Wrapf(err, "setting log level to %s", o.LogLevel) 65 } 66 util.AppLogger().Debugf("debug logging enabled") 67 } 68 err = util.EnsureGoPath() 69 if err != nil { 70 return err 71 } 72 73 inputPath := filepath.Join(o.InputBase, o.InputPackage) 74 outputPath := filepath.Join(o.OutputBase, o.OutputPackage) 75 76 if !strings.HasPrefix(inputPath, build.Default.GOPATH) { 77 return errors.Errorf("input %s is not in GOPATH (%s)", inputPath, build.Default.GOPATH) 78 } 79 80 if !strings.HasPrefix(outputPath, build.Default.GOPATH) { 81 return errors.Errorf("output %s is not in GOPATH (%s)", outputPath, build.Default.GOPATH) 82 } 83 84 // Work out the InputPackage relative to GOROOT 85 o.GoPathInputPackage = strings.TrimPrefix(inputPath, 86 fmt.Sprintf("%s/", filepath.Join(build.Default.GOPATH, "src"))) 87 88 // Work out the OutputPackage relative to GOROOT 89 o.GoPathOutputPackage = strings.TrimPrefix(outputPath, 90 fmt.Sprintf("%s/", filepath.Join(build.Default.GOPATH, "src"))) 91 return nil 92 }