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
245 lines (213 loc) · 7.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
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/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/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-logging/pkg/log"
"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,
}
generalCommands := []*cobra.Command{
cobras.SplitCommand(upgrade.NewCmdUpgrade()),
cobras.SplitCommand(version.NewCmdVersion()),
}
cmd.AddCommand(generalCommands...)
groups := templates.CommandGroups{
{
Message: "General:",
Commands: generalCommands,
},
}
groups.Add(cmd)
filters := []string{"options"}
po := &templates.Options{}
getPluginCommandGroups := func() (templates.PluginCommandGroups, bool) {
verifier := &extensions.CommandOverrideVerifier{
Root: cmd,
SeenPlugins: make(map[string]string, 0),
}
pluginCommandGroups, err := po.GetPluginCommandGroups(verifier, plugins.Plugins)
if err != nil {
log.Logger().Errorf("%v", err)
}
return pluginCommandGroups, po.ManagedPluginsEnabled
}
templates.ActsAsRootCommand(cmd, filters, getPluginCommandGroups, groups...)
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)
}
}
}
}
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 string, 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 string, pluginBinDir string) (string, error) {
// if on Windows, append the "exe" extension
// to the filename that we are looking up.
if runtime.GOOS == "windows" {
filename = 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 || len(path) == 0 {
// lets see if we have previously downloaded this binary plugin
path = FindPluginBinary(pluginBinDir, commandName)
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 len(foundBinaryPath) == 0 {
return nil
}
// invoke cmd binary relaying the current environment and args given
// remainingArgs will always have at least one element.
// execve will make remainingArgs[0] the "binary name".
if err := pluginHandler.Execute(foundBinaryPath, append([]string{foundBinaryPath}, cmdArgs[len(remainingArgs):]...), 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 string, 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 ""
}