-
Notifications
You must be signed in to change notification settings - Fork 1
/
admin.go
214 lines (190 loc) · 4.98 KB
/
admin.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cmd
import (
"errors"
"github.com/geoffmore/esctl/pkg/admin"
"github.com/spf13/cobra"
"log"
"strconv"
)
// verbCmd should probably have its own file that can be referenced in places.
// Maybe esutil?
type verbCmd struct {
Command string
Aliases []string
}
// TODO - improve boilerplate for all functions in admin package
// TODO - removal optional status of admin package
// TODO - make verbCmd part of esutil and figure out a structure for commands at various levels
var listCmd = verbCmd{Command: "list", Aliases: []string{"ls"}}
//var showCmd = verbCmd{Command: "show"}
func init() {
rootCmd.AddCommand(adminCmd)
adminCmd.AddCommand(adminNode)
adminNode.AddCommand(adminNodeList)
adminCmd.AddCommand(listNodes)
adminCmd.AddCommand(listNodesStorage)
// TODO - add showShards function with newest boilerplate
// adminCmd.AddCommand(showShards)
adminCmd.AddCommand(showBigShards)
adminCmd.AddCommand(showSmallShards)
adminCmd.AddCommand(showShardUsageByNode)
}
var adminAliases = [...]string{"adm"}
// Maybe this could go under a ResourceCmd struct also under esutil?
var nodeAliases = [...]string{"nodes", "no"}
// Temporary until admin commands are refactored
var help = false
// I should add a --help flag that adds the pointer help field to a request. Not
// sure how to wrap that for the admin package. Maybe I don't need to?
var adminCmd = &cobra.Command{
// esctl admin
Use: "admin",
Aliases: adminAliases[:],
Short: "Commands useful for Elasticsearch operators",
Long: "Commands useful for Elasticsearch operators.\nMuch of this is inspired by https://github.com/slmingol/escli",
}
var adminNode = &cobra.Command{
Use: "node",
Aliases: nodeAliases[:],
Short: "node stuff",
}
var adminNodeList = &cobra.Command{
Use: listCmd.Command,
Aliases: listCmd.Aliases[:],
Short: "list nodes",
Run: func(cmd *cobra.Command, args []string) {
// Boilerplate //
// Command init
err := initCmdOpts(cmd, cmdOpts, args)
if err != nil {
log.Fatal(err)
}
// Client init
err = initCfgOpts(cmd, cfgOpts)
if err != nil {
log.Fatal(err)
}
client, err := genClientWOpts(cfgOpts)
if err != nil {
log.Fatal(err)
}
// Everything else
err = admin.NodeList(client, cmdOpts)
if err != nil {
log.Fatal(err)
}
},
}
var listNodes = &cobra.Command{
// esctl admin list-nodes
Use: "list-nodes",
Short: "No description",
Run: func(cmd *cobra.Command, args []string) {
// Boilerplate
client, err := genClient(context)
if err != nil {
log.Fatal(err)
}
err = admin.ListNodes(client, outputFmt, help)
if err != nil {
log.Fatal(err)
}
},
}
var listNodesStorage = &cobra.Command{
// esctl admin list-nodes
Use: "list-nodes-storage",
Short: "No description",
Run: func(cmd *cobra.Command, args []string) {
// Boilerplate
client, err := genClient(context)
if err != nil {
log.Fatal(err)
}
err = admin.ListNodesStorage(client, outputFmt, help)
if err != nil {
log.Fatal(err)
}
},
}
var showBigShards = &cobra.Command{
// esctl admin list-nodes
Use: "show-big-shards",
Short: "No description",
// first arg int
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires an integer argument")
}
i, err := strconv.Atoi(args[0])
if err != nil {
return errors.New("provided argument was not a an integer")
}
if i < 1 {
return errors.New("provided integer needs to be greater than 0")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
// Boilerplate
client, err := genClient(context)
if err != nil {
log.Fatal(err)
}
// Error checking is done in Args, so there is no need to duplicate logic
// in Run
i, _ := strconv.Atoi(args[0])
err = admin.ShowBigShards(client, outputFmt, help, i)
if err != nil {
log.Fatal(err)
}
},
}
var showSmallShards = &cobra.Command{
// esctl admin list-nodes
Use: "show-small-shards",
Short: "No description",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires an integer argument")
}
i, err := strconv.Atoi(args[0])
if err != nil {
return errors.New("provided argument was not a an integer")
}
if i < 1 {
return errors.New("provided integer needs to be greater than 0")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
// Boilerplate
client, err := genClient(context)
if err != nil {
log.Fatal(err)
}
// Error checking is done in Args, so there is no need to duplicate logic
// in Run
i, _ := strconv.Atoi(args[0])
err = admin.ShowSmallShards(client, outputFmt, help, i)
if err != nil {
log.Fatal(err)
}
},
}
var showShardUsageByNode = &cobra.Command{
// esctl admin show-shard-usage-by-node
Use: "show-shard-usage-by-node",
Short: "No description",
Run: func(cmd *cobra.Command, args []string) {
// Boilerplate
client, err := genClient(context)
if err != nil {
log.Fatal(err)
}
err = admin.ShowShardUsageByNode(client, outputFmt)
if err != nil {
log.Fatal(err)
}
},
}