-
Notifications
You must be signed in to change notification settings - Fork 787
/
common_plugins.go
137 lines (121 loc) · 3.31 KB
/
common_plugins.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/extensions"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
jenkinsv1 "github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
)
func (o *CommonOptions) isManagedPluginsEnabled() bool {
apisClient, err := o.ApiExtensionsClient()
if err != nil {
if o.Verbose {
log.Warnf("Unable to load managed plugins because %v\n", err)
}
return false
}
_, err = apisClient.ApiextensionsV1beta1().CustomResourceDefinitions().Get("plugins."+jenkinsio.GroupName,
metav1.GetOptions{})
if err != nil {
if o.Verbose {
log.Warnf("Unable to load managed plugins because %v\n", err)
}
return false
}
return true
}
func (o *CommonOptions) getPluginCommandGroups(verifier extensions.PathVerifier) (templates.PluginCommandGroups, bool,
error) {
otherCommands := templates.PluginCommandGroup{
Message: "Other Commands",
}
groups := make(map[string]templates.PluginCommandGroup, 0)
// Managed plugins
managedPluginsEnabled := o.isManagedPluginsEnabled()
if managedPluginsEnabled {
jxClient, ns, err := o.CreateJXClient()
if err != nil {
return nil, false, err
}
plugs := jxClient.JenkinsV1().Plugins(ns)
pluginList, err := plugs.List(metav1.ListOptions{})
if err != nil {
return nil, false, err
}
for _, plugin := range pluginList.Items {
pluginCommand := &templates.PluginCommand{
PluginSpec: plugin.Spec,
}
if plugin.Spec.Group == "" {
otherCommands.Commands = append(otherCommands.Commands, pluginCommand)
} else {
if g, ok := groups[plugin.Spec.Group]; !ok {
groups[plugin.Spec.Group] = templates.PluginCommandGroup{
Message: fmt.Sprintf("%s:", plugin.Spec.Group),
Commands: []*templates.PluginCommand{
pluginCommand,
},
}
} else {
g.Commands = append(g.Commands, pluginCommand)
}
}
}
}
pathCommands := templates.PluginCommandGroup{
Message: "Locally Available Commands:",
}
path := "PATH"
if runtime.GOOS == "windows" {
path = "path"
}
paths := sets.NewString(filepath.SplitList(os.Getenv(path))...)
for _, dir := range paths.List() {
files, err := ioutil.ReadDir(dir)
if err != nil {
continue
}
for _, f := range files {
if f.IsDir() {
continue
}
if !strings.HasPrefix(f.Name(), "jx-") {
continue
}
pluginPath := filepath.Join(dir, f.Name())
subCommand := strings.TrimPrefix(strings.Replace(filepath.Base(pluginPath), "-", " ", -1), "jx ")
pc := &templates.PluginCommand{
PluginSpec: jenkinsv1.PluginSpec{
SubCommand: subCommand,
Description: pluginPath,
},
Errors: make([]error, 0),
}
pathCommands.Commands = append(pathCommands.Commands, pc)
if errs := verifier.Verify(filepath.Join(dir, f.Name())); len(errs) != 0 {
for _, err := range errs {
pc.Errors = append(pc.Errors, err)
}
}
}
}
pcgs := templates.PluginCommandGroups{}
for _, g := range groups {
pcgs = append(pcgs, g)
}
if len(otherCommands.Commands) > 0 {
pcgs = append(pcgs, otherCommands)
}
if len(pathCommands.Commands) > 0 {
pcgs = append(pcgs, pathCommands)
}
return pcgs, managedPluginsEnabled, nil
}