forked from kubesphere-sigs/ks
/
root.go
74 lines (67 loc) · 1.89 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
package pipeline
import (
"context"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/linuxsuren/ks/kubectl-plugin/types"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
)
// NewPipelineCmd returns a command of pipeline
func NewPipelineCmd(client dynamic.Interface) (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "pipeline",
Aliases: []string{"pip"},
Short: "Manage the Pipeline of KubeSphere DevOps",
RunE: func(cmd *cobra.Command, args []string) (err error) {
var pips []string
if _, pips, err = getPipelines(client, args); err == nil {
for _, pip := range pips {
fmt.Println(pip)
}
}
return
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) (suggestion []string, directive cobra.ShellCompDirective) {
suggestion = getAllNamespace(client)
directive = cobra.ShellCompDirectiveNoFileComp
return
},
}
cmd.AddCommand(NewDelPipelineCmd(client),
NewPipelineEditCmd(client),
NewPipelineViewCmd(client),
newPipelineCreateCmd(client))
return
}
func getNamespace(client dynamic.Interface, args []string) (ns string, err error) {
if len(args) == 0 {
nsList := getAllNamespace(client)
if len(nsList) == 0 {
err = fmt.Errorf("no pipeline namespace found in this cluster")
return
}
prompt := &survey.Select{
Message: "Please select the namespace which you want to check:",
Options: nsList,
}
if err = survey.AskOne(prompt, &ns); err != nil {
return
}
} else {
ns = args[0]
}
return
}
func getAllNamespace(client dynamic.Interface) (nsList []string) {
if list, err := client.Resource(types.GetNamespaceSchema()).List(context.TODO(), metav1.ListOptions{
LabelSelector: "kubesphere.io/devopsproject",
}); err == nil {
nsList = make([]string, len(list.Items))
for i, item := range list.Items {
nsList[i] = item.GetName()
}
}
return
}