-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_action.go
144 lines (112 loc) · 3.76 KB
/
cluster_action.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
package kubernetes
import (
"fmt"
"github.com/spf13/cobra"
"github.com/flowswiss/cli/v2/internal/commands"
"github.com/flowswiss/cli/v2/pkg/api/kubernetes"
"github.com/flowswiss/cli/v2/pkg/filter"
)
func ClusterActionCommand(app commands.Application) *cobra.Command {
cmd := &cobra.Command{
Use: "action",
Aliases: []string{"actions"},
Short: "Manage your kubernetes cluster actions",
}
commands.Add(app, cmd,
&clusterActionListCommand{},
&clusterActionRunCommand{},
)
return cmd
}
type clusterActionListCommand struct {
filter string
}
func (c *clusterActionListCommand) Run(cmd *cobra.Command, args []string) error {
cluster, err := findCluster(cmd.Context(), args[0])
if err != nil {
return err
}
actions := make([]kubernetes.ClusterAction, len(cluster.Status.Actions))
for i, action := range cluster.Status.Actions {
actions[i] = kubernetes.ClusterAction(action)
}
if len(c.filter) != 0 {
actions = filter.Find(actions, c.filter)
}
return commands.PrintStdout(actions)
}
func (c *clusterActionListCommand) CompleteArg(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completeCluster(cmd.Context(), toComplete)
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
func (c *clusterActionListCommand) Build(app commands.Application) *cobra.Command {
cmd := &cobra.Command{
Use: "list CLUSTER",
Aliases: []string{"show", "ls", "get"},
Short: "List available actions",
Long: "Prints a table of all available kubernetes cluster actions for the selected cluster.",
Args: cobra.ExactArgs(1),
ValidArgsFunction: c.CompleteArg,
RunE: c.Run,
}
cmd.Flags().StringVar(&c.filter, "filter", "", "custom term to filter the results")
return cmd
}
type clusterActionRunCommand struct{}
func (c *clusterActionRunCommand) Run(cmd *cobra.Command, args []string) error {
cluster, err := findCluster(cmd.Context(), args[0])
if err != nil {
return err
}
actions := make([]kubernetes.ClusterAction, len(cluster.Status.Actions))
for i, action := range cluster.Status.Actions {
actions[i] = kubernetes.ClusterAction(action)
}
action, err := filter.FindOne(actions, args[1])
if err != nil {
return fmt.Errorf("find action: %w", err)
}
data := kubernetes.ClusterPerformAction{
Action: action.Command,
}
cluster, err = kubernetes.NewClusterService(commands.Config.Client).PerformAction(cmd.Context(), cluster.ID, data)
if err != nil {
return fmt.Errorf("run cluster action: %w", err)
}
return commands.PrintStdout(cluster)
}
func (c *clusterActionRunCommand) CompleteArg(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completeCluster(cmd.Context(), toComplete)
}
if len(args) == 1 {
cluster, err := findCluster(cmd.Context(), args[0])
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
actions := make([]kubernetes.ClusterAction, len(cluster.Status.Actions))
for i, action := range cluster.Status.Actions {
actions[i] = kubernetes.ClusterAction(action)
}
filtered := filter.Find(actions, toComplete)
names := make([]string, len(filtered))
for i, action := range filtered {
names[i] = action.Command
}
return names, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
func (c *clusterActionRunCommand) Build(app commands.Application) *cobra.Command {
cmd := &cobra.Command{
Use: "run CLUSTER ACTION",
Short: "Run action",
Long: "Runs the given action on the selected kubernetes cluster.",
Args: cobra.ExactArgs(2),
ValidArgsFunction: c.CompleteArg,
RunE: c.Run,
}
return cmd
}