-
Notifications
You must be signed in to change notification settings - Fork 30
/
helpers.go
158 lines (142 loc) · 4.4 KB
/
helpers.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
package templates
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
jxCore "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx-helpers/v3/pkg/extensions"
"github.com/jenkins-x/jx-helpers/v3/pkg/kube/jxclient"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
)
type Options struct {
ManagedPluginsEnabled bool
JXClient versioned.Interface
Namespace string
}
// GetPluginCommandGroups returns the plugin groups
func (o *Options) GetPluginCommandGroups(verifier extensions.PathVerifier, localPlugins []jxCore.Plugin) (PluginCommandGroups, error) {
otherCommands := PluginCommandGroup{
Message: "Other Commands",
}
groups := make(map[string]PluginCommandGroup, 0)
o.addPlugins(localPlugins, &otherCommands, groups)
err := o.addManagedPlugins(&otherCommands, groups)
if err != nil {
return nil, errors.Wrapf(err, "failed to add managed plugins")
}
pathCommands := 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 := &PluginCommand{
PluginSpec: jxCore.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 := 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, nil
}
func (o *Options) addManagedPlugins(otherCommands *PluginCommandGroup, groups map[string]PluginCommandGroup) error {
// Managed plugins
var err error
if o.ManagedPluginsEnabled {
o.JXClient, o.Namespace, err = jxclient.LazyCreateJXClientAndNamespace(o.JXClient, o.Namespace)
if err != nil {
return errors.Wrapf(err, "failed to create jx client")
}
pluginList, err := o.JXClient.JenkinsV1().Plugins(o.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil && apierrors.IsNotFound(err) {
err = nil
}
if err != nil {
log.Logger().Debugf("failed to find Plugin CRDs in kubernetes namespace %s due to: %s", o.Namespace, err.Error())
}
o.addPlugins(pluginList.Items, otherCommands, groups)
}
return nil
}
func (o *Options) addPlugins(pluginSlice []jxCore.Plugin, otherCommands *PluginCommandGroup, groups map[string]PluginCommandGroup) {
for _, plugin := range pluginSlice {
pluginCommand := &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] = PluginCommandGroup{
Message: fmt.Sprintf("%s:", plugin.Spec.Group),
Commands: []*PluginCommand{
pluginCommand,
},
}
} else {
g.Commands = append(g.Commands, pluginCommand)
}
}
}
}
// ActsAsRootCommand act as if the given set of plugins is a single root command
func ActsAsRootCommand(cmd *cobra.Command, filters []string, getPluginCommandGroups func() (PluginCommandGroups, bool), groups ...CommandGroup) FlagExposer {
if cmd == nil {
panic("nil root command")
}
templater := &templater{
RootCmd: cmd,
UsageTemplate: MainUsageTemplate(),
HelpTemplate: MainHelpTemplate(),
GetPluginCommandGroups: getPluginCommandGroups,
CommandGroups: groups,
Filtered: filters,
}
cmd.SetUsageFunc(templater.UsageFunc())
cmd.SetHelpFunc(templater.HelpFunc())
return templater
}