-
Notifications
You must be signed in to change notification settings - Fork 785
/
alpha.go
141 lines (125 loc) · 3.52 KB
/
alpha.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package alpha
import (
"io/ioutil"
"os"
"strings"
"github.com/ghodss/yaml"
jenkinsv1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
"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/templates"
"github.com/jenkins-x/jx/v2/pkg/extensions"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// Options are the options to execute the alpha command
type Options struct {
*opts.CommonOptions
}
const (
alphaYamlURL = "https://raw.githubusercontent.com/jenkins-x-labs/jxl/master/alpha/plugins.yml"
)
var (
alphaLong = templates.LongDesc(`
Provides alpha versions of existing commands or adds new alpha commands.
`)
alphaExample = templates.Examples(`
# Run the new helm3 / helmfile based version of boot
jx alpha boot
`)
)
// NewCmdAlpha creates the "jx alpha" command
func NewCmdAlpha(commonOpts *opts.CommonOptions) *cobra.Command {
options := &Options{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "alpha",
Short: "Provides alpha versions of existing commands or adds new alpha commands",
Long: alphaLong,
Example: alphaExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
plugins, err := options.getPluginSpecs()
if err != nil {
log.Logger().Warnf("failed to discover alpha commands from github: %s", err.Error())
}
for i := range plugins {
c := plugins[i]
subCmd := &cobra.Command{
Use: c.SubCommand,
Short: c.Description,
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {
err := options.RunPlugin(c, args)
helper.CheckErr(err)
},
}
cmd.AddCommand(subCmd)
}
return cmd
}
// Run implements this command
func (o *Options) Run() error {
return o.Cmd.Help()
}
// getPluginSpecs returns the plugins
func (o *Options) getPluginSpecs() ([]jenkinsv1.PluginSpec, error) {
pluginSpecs := []jenkinsv1.PluginSpec{}
httpClient := util.GetClient()
resp, err := httpClient.Get(alphaYamlURL)
if err != nil {
return nil, errors.Wrapf(err, "failed to get YAML from %s", alphaYamlURL)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "failed to read YAML from %s", alphaYamlURL)
}
err = yaml.Unmarshal(data, &pluginSpecs)
if err != nil {
return nil, errors.Wrap(err, "failed to unmmarshal plugins YAML")
}
// lets see if there are any version expressions in the URLs
for i := range pluginSpecs {
p := &pluginSpecs[i]
for j := range p.Binaries {
b := &p.Binaries[j]
b.URL = strings.ReplaceAll(b.URL, "$VERSION", p.Version)
}
}
return pluginSpecs, nil
}
// RunPlugin runs the given plugin command
func (o *Options) RunPlugin(pluginSpec jenkinsv1.PluginSpec, args []string) error {
plugin := jenkinsv1.Plugin{}
plugin.Spec = pluginSpec
plugin.Name = pluginSpec.Name
path, err := extensions.EnsurePluginInstalled(plugin)
if err != nil {
return errors.Wrapf(err, "failed to ensure plugin is installed %s", pluginSpec.Name)
}
log.Logger().Debugf("running plugin %s with args %#v", path, args)
c := util.Command{
Name: path,
Args: args,
Env: map[string]string{
"BINARY_NAME": "jx alpha " + pluginSpec.SubCommand,
"TOP_LEVEL_COMMAND": "jx-alpha-" + pluginSpec.SubCommand,
},
Out: os.Stdin,
Err: os.Stderr,
In: os.Stdin,
}
_, err = c.RunWithoutRetry()
if err != nil {
return err
}
return nil
}