-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_get_dependency_version.go
109 lines (95 loc) · 3.33 KB
/
step_get_dependency_version.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package get
import (
"fmt"
"strings"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/dependencymatrix"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
getDependencyVersionLong = templates.LongDesc(`
Outputs the version of a specific dependency from the dependency matrix in the version stream or a local directory
`)
getDependencyVersionExample = templates.Examples(`
# display the version of jx in the version stream
jx step get dependency-version --host=github.com --owner=jenkins-x --repo=jx
# display the version of jx in a local directory containing a "dependency-matrix" subdirectory, only logging the version
jx step get dependency-version --host=github.com --owner=jenkins-x --repo=jx --dir=/some/directory --short
`)
)
// StepGetDependencyVersionOptions contains the command line flags
type StepGetDependencyVersionOptions struct {
step.StepOptions
Host string
Owner string
Repo string
Dir string
ShortOutput bool
}
// NewCmdStepGetDependencyVersion Creates a new Command object
func NewCmdStepGetDependencyVersion(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepGetDependencyVersionOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "dependency-version",
Short: "Outputs the version of a dependency from the Jenkins X dependency matrix",
Long: getDependencyVersionLong,
Example: getDependencyVersionExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Host, "host", "", "", "Host for dependency repo in the matrix")
cmd.Flags().StringVarP(&options.Owner, "owner", "", "", "Owner for dependency repo in the matrix")
cmd.Flags().StringVarP(&options.Repo, "repo", "", "", "Repo name for dependency repo in the matrix")
cmd.Flags().StringVarP(&options.Dir, "dir", "", "", "Directory to read dependency matrix from instead of using the version stream")
cmd.Flags().BoolVarP(&options.ShortOutput, "short", "", false, "Display the dependency version only")
return cmd
}
// Run implements this command
func (o *StepGetDependencyVersionOptions) Run() error {
var missingArgs []string
if o.Host == "" {
missingArgs = append(missingArgs, "host")
}
if o.Owner == "" {
missingArgs = append(missingArgs, "owner")
}
if o.Repo == "" {
missingArgs = append(missingArgs, "repo")
}
if len(missingArgs) > 0 {
return fmt.Errorf("one or more required arguments are missing: %s", strings.Join(missingArgs, ", "))
}
if o.Dir == "" {
resolver, err := o.GetVersionResolver()
if err != nil {
return err
}
o.Dir = resolver.VersionsDir
}
matrix, err := dependencymatrix.LoadDependencyMatrix(o.Dir)
if err != nil {
return errors.Wrapf(err, "failed to load dependency matrix at %s", o.Dir)
}
version, err := matrix.FindVersionForDependency(o.Host, o.Owner, o.Repo)
if err != nil {
return err
}
if o.ShortOutput {
fmt.Fprintf(o.Out, "%s\n", version)
return nil
}
fmt.Fprintf(o.Out, "Version for host %s, owner %s, repo %s in matrix at %s is: %s\n", o.Host, o.Owner, o.Repo, o.Dir, version)
return nil
}