-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcommands.go
466 lines (394 loc) · 14.3 KB
/
commands.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package kubexp
import (
"fmt"
"github.com/alitari/gocui"
)
type commandType struct {
Name string
f func(g *gocui.Gui, v *gocui.View) error
}
type keyEventType struct {
Viewname string
Key interface{}
mod gocui.Modifier
}
type keyBindingType struct {
show bool
KeyEvent keyEventType
Command commandType
}
func newScaleCommand(name string, replicas int) commandType {
var scaleCommand = commandType{Name: name, f: func(g *gocui.Gui, v *gocui.View) error {
res := selectedResource()
if res.Name == "deployments" || res.Name == "replicationcontrollers" || res.Name == "replicasets" || res.Name == "daemonsets" || res.Name == "statefulsets" {
scaleResource(replicas)
}
return nil
}}
return scaleCommand
}
func newExecCommand(name, cmd string, containerNr int) commandType {
var execCommand = commandType{Name: name, f: func(g *gocui.Gui, v *gocui.View) error {
res := selectedResource()
if res.Name != "pods" {
return nil
}
ns := selectedResourceItemNamespace()
rname := selectedResourceItemName()
details := resourceItemsList.widget.items[resourceItemsList.widget.selectedItem]
containerNames = resItemContainers(details)
if containerNr > len(containerNames)-1 {
containerNr = 0
}
cmd := kubectl(backend.context.Name, "-n", ns, "exec", "-c", containerNames[containerNr], "-it", rname, cmd)
exe <- cmd
return gocui.ErrQuit
}}
return execCommand
}
func newPortForwardCommand(name string, useSamePort bool) commandType {
var portForwardCommand = commandType{Name: name, f: func(g *gocui.Gui, v *gocui.View) error {
res := selectedResource()
if res.Name != "pods" {
return nil
}
podName := selectedResourceItemName()
ns := selectedResourceItemNamespace()
if portforwardProxies[ns+"/"+podName] != nil {
err := removePortforwardProxyofPod(ns, podName)
if err != nil {
showError("Can't remove port-forward proxy", err)
return nil
}
return nil
}
pod := resourceItemsList.widget.items[resourceItemsList.widget.selectedItem]
ports := ports(pod)
for _, cp := range ports {
var localPort int
if useSamePort {
localPort = cp.port
} else {
localPort = currentPortforwardPort
currentPortforwardPort++
}
err := createPortforwardProxy(ns, podName, portMapping{localPort, cp})
if err != nil {
showError("Can't create port-forward proxy", err)
return nil
}
}
return nil
}}
return portForwardCommand
}
var portForwardSamePortCommand = newPortForwardCommand("toggle port forward (same port)", true)
var portForwardCommand = newPortForwardCommand("toggle port forward", false)
var quitCommand = commandType{Name: "Quit", f: func(g *gocui.Gui, v *gocui.View) error {
removeAllPortforwardProxies()
leaveApp = true
return gocui.ErrQuit
}}
var nextResourceCommand = commandType{Name: "Next resource", f: func(g *gocui.Gui, v *gocui.View) error {
resourceMenu.widget.nextSelectedItem()
newResource()
return nil
}}
var previousResourceCommand = commandType{Name: "Previous resource", f: func(g *gocui.Gui, v *gocui.View) error {
resourceMenu.widget.previousSelectedItem()
newResource()
return nil
}}
func newConfirmCommand(name string, command commandType) commandType {
var confirmCommand = commandType{Name: name, f: func(g *gocui.Gui, v *gocui.View) error {
showConfirm(fmt.Sprintf("Delete %s from %s ?", selectedResourceItemName(), selectedResource().Name), command)
return nil
}}
return confirmCommand
}
func newLoadingCommand(name string, command commandType) commandType {
var loadingCommand = commandType{Name: name, f: func(g *gocui.Gui, v *gocui.View) error {
showLoading(command, g, v)
return nil
}}
return loadingCommand
}
var executeConfirmCommand = commandType{Name: "Execute command to confirm", f: func(g *gocui.Gui, v *gocui.View) error {
quitWidgetCommand.f(g, v)
confirmCommand.f(g, v)
return nil
}}
var deleteCommand = commandType{Name: "Delete resource", f: func(g *gocui.Gui, v *gocui.View) error {
deleteResource(false)
return nil
}}
var deleteNoGracePeriodCommand = commandType{Name: "Delete resource", f: func(g *gocui.Gui, v *gocui.View) error {
deleteResource(true)
return nil
}}
var deleteConfirmCommand = newConfirmCommand("Delete resource", deleteCommand)
var deleteNoGracePeriodConfirmCommand = newConfirmCommand("Delete resource immediately", deleteNoGracePeriodCommand)
var nameSortCommand = commandType{Name: "Sort by name", f: func(g *gocui.Gui, v *gocui.View) error {
nameSorting()
newResource()
return nil
}}
var ageSortCommand = commandType{Name: "Sort by age", f: func(g *gocui.Gui, v *gocui.View) error {
ageSorting()
newResource()
return nil
}}
var scaleUpCommand = newScaleCommand("Scale up", 1)
var scaleDownCommand = newScaleCommand("Scale down", -1)
var execBashCommand0 = newExecCommand("Exec first container bash", "bash", 0)
var execBashCommand1 = newExecCommand("Exec second container bash", "bash", 1)
var execBashCommand2 = newExecCommand("Exec third container bash", "bash", 2)
var execShellCommand0 = newExecCommand("Exec first container sh", "sh", 0)
var execShellCommand1 = newExecCommand("Exec second container sh", "sh", 1)
var execShellCommand2 = newExecCommand("Exec third container sh", "sh", 2)
var nextResourceItemDetailPartCommand = commandType{Name: "Next detail part", f: func(g *gocui.Gui, v *gocui.View) error {
leaveResourceItemDetailsPart()
resourcesItemDetailsMenu.widget.nextSelectedItem()
setResourceItemDetailsPart()
return nil
}}
var previousResourceItemDetailPartCommand = commandType{Name: "Previous detail part", f: func(g *gocui.Gui, v *gocui.View) error {
leaveResourceItemDetailsPart()
resourcesItemDetailsMenu.widget.previousSelectedItem()
setResourceItemDetailsPart()
return nil
}}
var reloadResourceItemDetailPartCommand = commandType{Name: "Reload detail part", f: func(g *gocui.Gui, v *gocui.View) error {
reloadResourceItemDetailsPart()
return nil
}}
var nextLineCommand = commandType{Name: "Next resource item", f: func(g *gocui.Gui, v *gocui.View) error {
if resourceItemDetailsWidget.visible {
return nil
}
resourceItemsList.widget.nextSelectedItem()
return nil
}}
var previousLineCommand = commandType{Name: "Previous resource item", f: func(g *gocui.Gui, v *gocui.View) error {
if resourceItemDetailsWidget.visible {
return nil
}
resourceItemsList.widget.previousSelectedItem()
return nil
}}
var nextResourceItemListPageCommand = commandType{Name: "Next resource item page ", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemsList.widget.nextPage()
return nil
}}
var previousResourceItemListPageCommand = commandType{Name: "Previous resource item page ", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemsList.widget.previousPage()
return nil
}}
var toggleResourceItemDetailsCommand = commandType{Name: "Toggle resource item details ", f: func(g *gocui.Gui, v *gocui.View) error {
toggleDetailBrowseState()
return nil
}}
var findNextCommand = commandType{Name: "find next on resource item details ", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.findNext()
return nil
}}
var findPreviousCommand = commandType{Name: "find previous on resource item details ", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.findPrevious()
return nil
}}
var homeCommand = commandType{Name: "TextArea home", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.scrollUp(1<<63 - 1)
return nil
}}
var endCommand = commandType{Name: "TextArea end", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.scrollDown((1<<63 - 1) / 2)
return nil
}}
var pageUpCommand = commandType{Name: "TextArea page up", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.scrollUp(resourceItemDetailsWidget.h)
return nil
}}
var pageDownCommand = commandType{Name: "TextArea page down", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.scrollDown(resourceItemDetailsWidget.h)
return nil
}}
var scrollUpCommand = commandType{Name: "TextArea scroll up", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.scrollUp(1)
return nil
}}
var scrollDownCommand = commandType{Name: "TextArea scroll down", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.scrollDown(1)
return nil
}}
var scrollUpHelpCommand = commandType{Name: "Help scroll up", f: func(g *gocui.Gui, v *gocui.View) error {
helpWidget.scrollUp(1)
return nil
}}
var scrollDownHelpCommand = commandType{Name: "Help scroll down", f: func(g *gocui.Gui, v *gocui.View) error {
helpWidget.scrollDown(1)
return nil
}}
var scrollRightCommand = commandType{Name: "TextArea scroll right", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.scrollRight()
return nil
}}
var scrollLeftCommand = commandType{Name: "TextArea scroll left", f: func(g *gocui.Gui, v *gocui.View) error {
resourceItemDetailsWidget.scrollLeft()
return nil
}}
var gotoSelectContextStateCommand = commandType{Name: "Select context", f: func(g *gocui.Gui, v *gocui.View) error {
setState(selectContextState)
return nil
}}
var loadContextCommand = newLoadingCommand("reload context", commandType{Name: "sc", f: func(g *gocui.Gui, v *gocui.View) error {
newContext()
return nil
}})
var nextContextCommand = commandType{Name: "Next context", f: func(g *gocui.Gui, v *gocui.View) error {
clusterList.widget.nextSelectedItem()
return nil
}}
var previousContextCommand = commandType{Name: "Previous context", f: func(g *gocui.Gui, v *gocui.View) error {
clusterList.widget.previousSelectedItem()
return nil
}}
var nextContextPageCommand = commandType{Name: "Next context", f: func(g *gocui.Gui, v *gocui.View) error {
clusterList.widget.nextPage()
return nil
}}
var previousContextPageCommand = commandType{Name: "Next context", f: func(g *gocui.Gui, v *gocui.View) error {
clusterList.widget.previousPage()
return nil
}}
var gotoSelectNamespaceStateCommand = commandType{Name: "Select namespace", f: func(g *gocui.Gui, v *gocui.View) error {
if namespaceList.widget.visible {
setState(selectNsState)
}
return nil
}}
var selectNamespaceLoadingCommand = newLoadingCommand("Select namespace", commandType{Name: "sn", f: func(g *gocui.Gui, v *gocui.View) error {
findResourceCategoryWithResources(1)
return nil
}})
var nextNamespaceCommand = commandType{Name: "Next namespace", f: func(g *gocui.Gui, v *gocui.View) error {
namespaceList.widget.nextSelectedItem()
return nil
}}
var previousNamespaceCommand = commandType{Name: "Previous namespace", f: func(g *gocui.Gui, v *gocui.View) error {
namespaceList.widget.previousSelectedItem()
return nil
}}
var nextNamespacePageCommand = commandType{Name: "Next namespace", f: func(g *gocui.Gui, v *gocui.View) error {
namespaceList.widget.nextPage()
return nil
}}
var previousNamespacePageCommand = commandType{Name: "Previous namespace", f: func(g *gocui.Gui, v *gocui.View) error {
namespaceList.widget.previousPage()
return nil
}}
var nextResourceCategoryCommand = commandType{Name: "Next resource category", f: func(g *gocui.Gui, v *gocui.View) error {
nextResourceCategory(1)
findResourceCategoryWithResources(1)
return nil
}}
var previousResourceCategoryCommand = commandType{Name: "Next resource category", f: func(g *gocui.Gui, v *gocui.View) error {
nextResourceCategory(-1)
findResourceCategoryWithResources(-1)
return nil
}}
var nextContainerCommand = commandType{Name: "Pod logs: Next container ", f: func(g *gocui.Gui, v *gocui.View) error {
nextLogContainer()
return nil
}}
var showHelpCommand = commandType{Name: "Show help", f: func(g *gocui.Gui, v *gocui.View) error {
setState(helpState)
return nil
}}
var quitWidgetCommand = commandType{Name: "quit", f: func(g *gocui.Gui, v *gocui.View) error {
setState(browseState)
return nil
}}
var uploadFileCommand = commandType{Name: "Upload file", f: func(g *gocui.Gui, v *gocui.View) error {
startFiletransfer(true)
return nil
}}
var downloadFileCommand = commandType{Name: "Download file", f: func(g *gocui.Gui, v *gocui.View) error {
startFiletransfer(false)
return nil
}}
var nextFileCommand = commandType{Name: "Next File", f: func(g *gocui.Gui, v *gocui.View) error {
fileList.widget.nextSelectedItem()
return nil
}}
var previousFileCommand = commandType{Name: "Previous File", f: func(g *gocui.Gui, v *gocui.View) error {
fileList.widget.previousSelectedItem()
return nil
}}
var nextFilePageCommand = commandType{Name: "Next File", f: func(g *gocui.Gui, v *gocui.View) error {
fileList.widget.nextPage()
return nil
}}
var previousFilePageCommand = commandType{Name: "Previous File", f: func(g *gocui.Gui, v *gocui.View) error {
fileList.widget.previousPage()
return nil
}}
var gotoFileCommand = commandType{Name: "Transfer file", f: func(g *gocui.Gui, v *gocui.View) error {
fileItem := fileList.widget.items[fileList.widget.selectedItem].(map[string]interface{})
//filename := item["name"].(string)
tracelog.Printf("selected file:'%v'", fileItem)
if fileBrowser.sourceSelection {
if fileItem["dir"].(bool) {
setFileListContent(fileItem["name"].(string))
} else {
sourceFile = fileBrowser.getPath(fileItem["name"].(string))
if fileBrowser.local {
fileBrowser = newRemoteFileBrowser(false, "/")
} else {
fileBrowser = newLocalFileBrowser(false, ".")
}
setFileListContent("")
}
} else {
if fileItem["dir"].(bool) {
setFileListContent(fileItem["name"].(string))
} else {
transferFile(fileBrowser.getPath(""))
}
}
return nil
}}
var nextContainerFiletransferCommand = commandType{Name: "Next container", f: func(g *gocui.Gui, v *gocui.View) error {
nextFileTransferContainer()
return nil
}}
func bindKey(g *gocui.Gui, show bool, keyBind keyEventType, command commandType) {
if err := g.SetKeybinding(keyBind.Viewname, keyBind.Key, keyBind.mod, command.f); err != nil {
errorlog.Panicln(err)
}
kb := keyBindingType{show, keyBind, command}
keyBindings = append(keyBindings, kb)
}
func filterKeyBindings(filter func(kb keyBindingType) bool) []keyBindingType {
result := []keyBindingType{}
for _, k := range keyBindings {
if filter(k) {
result = append(result, k)
}
}
return result
}
func keyBindingsForView(viewName string) string {
kbs := filterKeyBindings(func(kb keyBindingType) bool {
return viewName == kb.KeyEvent.Viewname && kb.show
})
res := ""
for _, kb := range kbs {
res = res + "," + kbToText(kb)
}
return res
}
func kbToText(kb keyBindingType) string {
k := kb.KeyEvent
cmd := kb.Command.Name
return fmt.Sprintf("%s %s", keyString(k), cmd)
}