/
completion.go
149 lines (133 loc) · 3.94 KB
/
completion.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
package cmd
import (
"log"
"os"
metalgo "github.com/metal-stack/metal-go"
"github.com/spf13/cobra"
)
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generates bash completion scripts",
Long: `To load completion run
. <(metalctl completion)
To configure your bash shell to load completions for each session add to your bashrc
# ~/.bashrc or ~/.profile
. <(metalctl completion)
`,
Run: func(cmd *cobra.Command, args []string) {
err := rootCmd.GenBashCompletion(os.Stdout)
if err != nil {
log.Fatalln(err)
}
},
}
var zshCompletionCmd = &cobra.Command{
Use: "zsh-completion",
Short: "Generates Z shell completion scripts",
Long: `To load completion run
. <(metalctl zsh-completion)
To configure your Z shell (with oh-my-zshell framework) to load completions for each session run
echo -e '#compdef _metalctl metalctl\n. <(metalctl zsh-completion)' > $ZSH/completions/_metalctl
rm -f ~/.zcompdump*
`,
Run: func(cmd *cobra.Command, args []string) {
err := rootCmd.GenZshCompletion(os.Stdout)
if err != nil {
log.Fatalln(err)
}
},
}
func imageListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.ImageList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, i := range resp.Image {
names = append(names, *i.ID)
}
return names, cobra.ShellCompDirectiveDefault
}
func partitionListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.PartitionList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, p := range resp.Partition {
names = append(names, *p.ID)
}
return names, cobra.ShellCompDirectiveDefault
}
func sizeListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.SizeList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, s := range resp.Size {
names = append(names, *s.ID)
}
return names, cobra.ShellCompDirectiveDefault
}
func machineListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.MachineList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, m := range resp.Machines {
names = append(names, *m.ID)
}
return names, cobra.ShellCompDirectiveDefault
}
func networkListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.NetworkList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, n := range resp.Networks {
names = append(names, *n.ID)
}
return names, cobra.ShellCompDirectiveDefault
}
func ipListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.IPList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, i := range resp.IPs {
names = append(names, *i.Ipaddress)
}
return names, cobra.ShellCompDirectiveDefault
}
func projectListCompletion(driver *metalgo.Driver) ([]string, cobra.ShellCompDirective) {
resp, err := driver.ProjectList()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, p := range resp.Project {
names = append(names, p.Meta.ID)
}
return names, cobra.ShellCompDirectiveDefault
}
func contextListCompletion() ([]string, cobra.ShellCompDirective) {
ctxs, err := getContexts()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for name := range ctxs.Contexts {
names = append(names, name)
}
return names, cobra.ShellCompDirectiveDefault
}
func outputFormatListCompletion() ([]string, cobra.ShellCompDirective) {
return []string{"table", "wide", "markdown", "json", "yaml", "template"}, cobra.ShellCompDirectiveDefault
}
func outputOrderListCompletion() ([]string, cobra.ShellCompDirective) {
return []string{"size", "id", "status", "event", "when", "partition", "project"}, cobra.ShellCompDirectiveDefault
}