This repository has been archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
root.go
361 lines (326 loc) · 11.1 KB
/
root.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package cmd
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"
"github.com/jenkins-x/jx-api/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx-cli/pkg/cmd/namespace"
"github.com/jenkins-x/jx-cli/pkg/cmd/upgrade"
"github.com/jenkins-x/jx-cli/pkg/cmd/version"
"github.com/jenkins-x/jx-cli/pkg/plugins"
"github.com/jenkins-x/jx-helpers/pkg/cobras"
"github.com/jenkins-x/jx-helpers/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/pkg/extensions"
"github.com/jenkins-x/jx-helpers/pkg/homedir"
"github.com/jenkins-x/jx-helpers/pkg/kube/jxclient"
"github.com/jenkins-x/jx-helpers/pkg/termcolor"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Main creates the new command
func Main(args []string) *cobra.Command {
cmd := &cobra.Command{
Use: "jx",
Short: "Jenkins X 3.x alpha command line",
Run: runHelp,
}
po := &templates.Options{}
getPluginCommandGroups := func() (templates.PluginCommandGroups, bool) {
verifier := &extensions.CommandOverrideVerifier{
Root: cmd,
SeenPlugins: make(map[string]string),
}
pluginCommandGroups, err := po.GetPluginCommandGroups(verifier, plugins.Plugins)
if err != nil {
log.Logger().Errorf("%v", err)
}
return pluginCommandGroups, po.ManagedPluginsEnabled
}
doCmd := func(cmd *cobra.Command, args []string) {
handleCommand(po, cmd, args, getPluginCommandGroups)
}
generalCommands := []*cobra.Command{
cobras.SplitCommand(namespace.NewCmdNamespace()),
cobras.SplitCommand(upgrade.NewCmdUpgrade()),
cobras.SplitCommand(version.NewCmdVersion()),
}
// aliases to classic jx commands...
getCmd := &cobra.Command{
Use: "get TYPE [flags]",
Short: "Display one or more resources",
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
helper.CheckErr(err)
},
SuggestFor: []string{"list", "ps"},
}
getBuildCmd := &cobra.Command{
Use: "build TYPE [flags]",
Short: "Display one or more resources relating to a pipeline build",
Aliases: []string{"builds"},
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
helper.CheckErr(err)
},
}
createCmd := &cobra.Command{
Use: "create TYPE [flags]",
Short: "Create one or more resources",
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
helper.CheckErr(err)
},
SuggestFor: []string{"new", "make"},
}
startCmd := &cobra.Command{
Use: "start TYPE [flags]",
Short: "Starts a resource",
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
helper.CheckErr(err)
},
}
stopCmd := &cobra.Command{
Use: "stop TYPE [flags]",
Short: "Stops a resource",
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
helper.CheckErr(err)
},
}
getCmd.AddCommand(
getBuildCmd,
aliasCommand(cmd, doCmd, "activities", []string{"pipeline", "activities"}, "act", "activity"),
aliasCommand(cmd, doCmd, "application", []string{"application"}, "app", "apps", "applications"),
aliasCommand(cmd, doCmd, "pipelines", []string{"pipeline", "get"}, "pipeline"),
aliasCommand(cmd, doCmd, "previews", []string{"pipeline", "previews"}, "preview"),
)
getBuildCmd.AddCommand(
aliasCommand(cmd, doCmd, "logs", []string{"pipeline", "logs"}, "log"),
aliasCommand(cmd, doCmd, "pods", []string{"pipeline", "pods"}, "pod"),
)
createCmd.AddCommand(
aliasCommand(cmd, doCmd, "quickstart", []string{"project", "quickstart"}, "qs"),
aliasCommand(cmd, doCmd, "project", []string{"project"}),
aliasCommand(cmd, doCmd, "pullrequest", []string{"project", "pullrequest"}, "pr"),
)
startCmd.AddCommand(
aliasCommand(cmd, doCmd, "pipeline", []string{"pipeline", "start"}, "pipelines"),
)
stopCmd.AddCommand(
aliasCommand(cmd, doCmd, "pipeline", []string{"pipeline", "stop"}, "pipelines"),
)
generalCommands = append(generalCommands, getCmd, createCmd, startCmd, stopCmd,
aliasCommand(cmd, doCmd, "import", []string{"project", "import"}, "log"),
)
cmd.AddCommand(generalCommands...)
groups := templates.CommandGroups{
{
Message: "General:",
Commands: generalCommands,
},
}
groups.Add(cmd)
filters := []string{"options"}
templates.ActsAsRootCommand(cmd, filters, getPluginCommandGroups, groups...)
handleCommand(po, cmd, args, getPluginCommandGroups)
return cmd
}
func handleCommand(po *templates.Options, cmd *cobra.Command, args []string, getPluginCommandGroups func() (templates.PluginCommandGroups, bool)) {
managedPlugins := &managedPluginHandler{
JXClient: po.JXClient,
Namespace: po.Namespace,
}
localPlugins := &localPluginHandler{}
if len(args) == 0 {
args = os.Args
}
if len(args) > 1 {
cmdPathPieces := args[1:]
pluginDir, err := homedir.DefaultPluginBinDir()
if err != nil {
log.Logger().Errorf("%v", err)
os.Exit(1)
}
// only look for suitable executables if
// the specified command does not already exist
if _, _, err := cmd.Find(cmdPathPieces); err != nil {
if _, managedPluginsEnabled := getPluginCommandGroups(); managedPluginsEnabled {
if err := handleEndpointExtensions(managedPlugins, cmdPathPieces, pluginDir); err != nil {
log.Logger().Errorf("%v", err)
os.Exit(1)
}
} else {
if err := handleEndpointExtensions(localPlugins, cmdPathPieces, pluginDir); err != nil {
log.Logger().Errorf("%v", err)
os.Exit(1)
}
}
}
}
}
func aliasCommand(rootCmd *cobra.Command, fn func(cmd *cobra.Command, args []string), name string, args []string, aliases ...string) *cobra.Command {
realArgs := append([]string{"jx"}, args...)
cmd := &cobra.Command{
Use: name,
Short: "alias for: jx " + name,
Aliases: aliases,
Run: func(cmd *cobra.Command, args []string) {
realArgs = append(realArgs, args...)
log.Logger().Debugf("about to invoke alias: %s", strings.Join(realArgs, " "))
fn(rootCmd, realArgs)
},
SuggestFor: []string{"jx " + name},
DisableFlagParsing: true,
}
return cmd
}
func runHelp(cmd *cobra.Command, args []string) {
cmd.Help() //nolint:errcheck
}
// PluginHandler is capable of parsing command line arguments
// and performing executable filename lookups to search
// for valid plugin files, and execute found plugins.
type PluginHandler interface {
// Lookup receives a potential filename and returns
// a full or relative path to an executable, if one
// exists at the given filename, or an error.
Lookup(filename string, pluginBinDir string) (string, error)
// Execute receives an executable's filepath, a slice
// of arguments, and a slice of environment variables
// to relay to the executable.
Execute(executablePath string, cmdArgs, environment []string) error
}
type managedPluginHandler struct {
JXClient versioned.Interface
Namespace string
localPluginHandler
}
// Lookup implements PluginHandler
func (h *managedPluginHandler) Lookup(filename, pluginBinDir string) (string, error) {
jxClient, ns, err := jxclient.LazyCreateJXClientAndNamespace(h.JXClient, h.Namespace)
if err != nil {
return "", err
}
possibles, err := jxClient.JenkinsV1().Plugins(ns).List(metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", extensions.PluginCommandLabel, filename),
})
if err != nil {
return "", err
}
if len(possibles.Items) > 0 {
found := possibles.Items[0]
if len(possibles.Items) > 1 {
// There is a warning about this when you install extensions as well
log.Logger().Warnf("More than one plugin installed for %s by apps. Selecting the one installed by %s at random.",
filename, found.Name)
}
return extensions.EnsurePluginInstalled(found, pluginBinDir)
}
return h.localPluginHandler.Lookup(filename, pluginBinDir)
}
// Execute implements PluginHandler
func (h *managedPluginHandler) Execute(executablePath string, cmdArgs, environment []string) error {
return h.localPluginHandler.Execute(executablePath, cmdArgs, environment)
}
type localPluginHandler struct{}
// Lookup implements PluginHandler
func (h *localPluginHandler) Lookup(filename, pluginBinDir string) (string, error) {
// if on Windows, append the "exe" extension
// to the filename that we are looking up.
if runtime.GOOS == "windows" {
filename += ".exe"
}
return exec.LookPath(filename)
}
// Execute implements PluginHandler
func (h *localPluginHandler) Execute(executablePath string, cmdArgs, environment []string) error {
return syscall.Exec(executablePath, cmdArgs, environment)
}
func handleEndpointExtensions(pluginHandler PluginHandler, cmdArgs []string, pluginBinDir string) error {
remainingArgs := []string{} // all "non-flag" arguments
for idx := range cmdArgs {
if strings.HasPrefix(cmdArgs[idx], "-") {
break
}
remainingArgs = append(remainingArgs, strings.Replace(cmdArgs[idx], "-", "_", -1))
}
foundBinaryPath := ""
// attempt to find binary, starting at longest possible name with given cmdArgs
for len(remainingArgs) > 0 {
commandName := fmt.Sprintf("jx-%s", strings.Join(remainingArgs, "-"))
path, err := pluginHandler.Lookup(commandName, pluginBinDir)
if err != nil || path == "" {
// lets see if we have a local binary on the PATH
path = FindPluginBinary(pluginBinDir, commandName)
if path == "" {
// lets use the downloaded plugin instead
for i := range plugins.Plugins {
p := plugins.Plugins[i]
if p.Spec.Name == commandName {
path, err = extensions.EnsurePluginInstalled(p, pluginBinDir)
if err != nil {
return errors.Wrapf(err, "failed to install binary plugin %s version %s to %s", commandName, p.Spec.Version, pluginBinDir)
}
if path != "" {
break
}
}
}
}
if path != "" {
foundBinaryPath = path
break
}
/* Usually "executable file not found in $PATH", spams output of jx help subcommand:
if err != nil {
log.Logger().Errorf("Error installing plugin for command %s. %v\n", remainingArgs, err)
}
*/
remainingArgs = remainingArgs[:len(remainingArgs)-1]
continue
}
foundBinaryPath = path
break
}
if foundBinaryPath == "" {
return nil
}
nextArgs := cmdArgs[len(remainingArgs):]
log.Logger().Debugf("using the plugin command: %s", termcolor.ColorInfo(foundBinaryPath+" "+strings.Join(nextArgs, " ")))
// invoke cmd binary relaying the current environment and args given
// remainingArgs will always have at least one element.
// execute will make remainingArgs[0] the "binary name".
if err := pluginHandler.Execute(foundBinaryPath, append([]string{foundBinaryPath}, nextArgs...), os.Environ()); err != nil {
return err
}
return nil
}
// FindPluginBinary tries to find the jx-foo binary plugin in the plugins dir `~/.jx/plugins/jx/bin` dir `
func FindPluginBinary(pluginDir, commandName string) string {
if pluginDir != "" {
files, err := ioutil.ReadDir(pluginDir)
if err != nil {
log.Logger().Debugf("failed to read plugin dir %s", err.Error())
} else {
prefix := commandName + "-"
for _, f := range files {
name := f.Name()
if strings.HasPrefix(name, prefix) {
path := filepath.Join(pluginDir, name)
log.Logger().Debugf("found plugin %s at %s", commandName, path)
return path
}
}
}
}
return ""
}