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