This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
333 lines (276 loc) · 6.19 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/jroimartin/gocui"
)
var version = "master"
var LOG_MOD string = "pod"
var NAMESPACE string = "default"
// Configure globale keys
var keys []Key = []Key{
Key{"", gocui.KeyCtrlC, actionGlobalQuit},
Key{"", gocui.KeyCtrlD, actionGlobalToggleViewDebug},
Key{"pods", gocui.KeyCtrlN, actionGlobalToggleViewNamespaces},
Key{"pods", gocui.KeyArrowUp, actionViewPodsUp},
Key{"pods", gocui.KeyArrowDown, actionViewPodsDown},
Key{"pods", 'd', actionViewPodsDelete},
Key{"pods", 'l', actionViewPodsLogs},
Key{"logs", 'l', actionViewPodsLogsHide},
Key{"logs", gocui.KeyArrowUp, actionViewPodsLogsUp},
Key{"logs", gocui.KeyArrowDown, actionViewPodsLogsDown},
Key{"namespaces", gocui.KeyArrowUp, actionViewNamespacesUp},
Key{"namespaces", gocui.KeyArrowDown, actionViewNamespacesDown},
Key{"namespaces", gocui.KeyEnter, actionViewNamespacesSelect},
}
// Main or not main, that's the question^^
func main() {
c := getConfig()
// Ask version
if c.askVersion {
fmt.Println(versionFull())
os.Exit(0)
}
// Ask Help
if c.askHelp {
fmt.Println(versionFull())
fmt.Println(HELP)
os.Exit(0)
}
// Only used to check errors
getClientSet()
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.Highlight = true
g.SelFgColor = gocui.ColorGreen
g.SetManagerFunc(uiLayout)
if err := uiKey(g); err != nil {
log.Panicln(err)
}
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
// Define the UI layout
func uiLayout(g *gocui.Gui) error {
maxX, maxY := g.Size()
viewDebug(g, maxX, maxY)
viewLogs(g, maxX, maxY)
viewNamespaces(g, maxX, maxY)
viewOverlay(g, maxX, maxY)
viewTitle(g, maxX, maxY)
viewPods(g, maxX, maxY)
viewStatusBar(g, maxX, maxY)
return nil
}
// Useful to debug Pody (display with CTRL+D)
func debug(g *gocui.Gui, output interface{}) {
v, err := g.View("debug")
if err == nil {
t := time.Now()
tf := t.Format("2006-01-02 15:04:05")
output = tf + " > " + output.(string)
fmt.Fprintln(v, output)
}
}
// Move view cursor to the bottom
func moveViewCursorDown(g *gocui.Gui, v *gocui.View, allowEmpty bool) error {
cx, cy := v.Cursor()
ox, oy := v.Origin()
nextLine, err := getNextViewLine(g, v)
if err != nil {
return err
}
if !allowEmpty && nextLine == "" {
return nil
}
if err := v.SetCursor(cx, cy+1); err != nil {
if err := v.SetOrigin(ox, oy+1); err != nil {
return err
}
}
return nil
}
// Move view cursor to the top
func moveViewCursorUp(g *gocui.Gui, v *gocui.View, dY int) error {
ox, oy := v.Origin()
cx, cy := v.Cursor()
if cy > dY {
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
if err := v.SetOrigin(ox, oy-1); err != nil {
return err
}
}
}
return nil
}
// Get view line (relative to the cursor)
func getViewLine(g *gocui.Gui, v *gocui.View) (string, error) {
var l string
var err error
_, cy := v.Cursor()
if l, err = v.Line(cy); err != nil {
l = ""
}
return l, err
}
// Get the next view line (relative to the cursor)
func getNextViewLine(g *gocui.Gui, v *gocui.View) (string, error) {
var l string
var err error
_, cy := v.Cursor()
if l, err = v.Line(cy + 1); err != nil {
l = ""
}
return l, err
}
// Set view cursor to line
func setViewCursorToLine(g *gocui.Gui, v *gocui.View, lines []string, selLine string) error {
ox, _ := v.Origin()
cx, _ := v.Cursor()
for y, line := range lines {
if line == selLine {
if err := v.SetCursor(ox, y); err != nil {
if err := v.SetOrigin(cx, y); err != nil {
return err
}
}
}
}
return nil
}
// Get pod name form line
func getPodNameFromLine(line string) string {
if line == "" {
return ""
}
i := strings.Index(line, " ")
if i == -1 {
return line
}
return line[0:i]
}
// Get selected pod
func getSelectedPod(g *gocui.Gui) (string, error) {
v, err := g.View("pods")
if err != nil {
return "", err
}
l, err := getViewLine(g, v)
if err != nil {
return "", err
}
p := getPodNameFromLine(l)
return p, nil
}
// Show views logs
func showViewPodsLogs(g *gocui.Gui) error {
vn := "logs"
switch LOG_MOD {
case "pod":
// Get current selected pod
p, err := getSelectedPod(g)
if err != nil {
return err
}
// Display pod containers
vLc, err := g.View(vn + "-containers")
if err != nil {
return err
}
vLc.Clear()
for _, c := range getPodContainers(p) {
fmt.Fprintln(vLc, c)
}
vLc.SetCursor(0, 0)
// Display logs
refreshPodsLogs(g)
}
debug(g, "Action: Show view logs")
g.SetViewOnTop(vn)
g.SetViewOnTop(vn + "-containers")
g.SetCurrentView(vn)
return nil
}
// Refresh pods logs
func refreshPodsLogs(g *gocui.Gui) error {
vn := "logs"
// Get current selected pod
p, err := getSelectedPod(g)
if err != nil {
return err
}
vLc, err := g.View(vn + "-containers")
if err != nil {
return err
}
c, err := getViewLine(g, vLc)
if err != nil {
return err
}
vL, err := g.View(vn)
if err != nil {
return err
}
getPodContainerLogs(p, c, vL)
return nil
}
// Display error
func displayError(g *gocui.Gui, e error) error {
lMaxX, lMaxY := g.Size()
minX := lMaxX / 6
minY := lMaxY / 6
maxX := 5 * (lMaxX / 6)
maxY := 5 * (lMaxY / 6)
if v, err := g.SetView("errors", minX, minY, maxX, maxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Title = " ERROR "
v.Frame = true
v.Wrap = true
v.Autoscroll = true
v.BgColor = gocui.ColorRed
v.FgColor = gocui.ColorWhite
// Content
v.Clear()
fmt.Fprintln(v, e.Error())
// Send to forground
g.SetCurrentView(v.Name())
}
return nil
}
// Hide error box
func hideError(g *gocui.Gui) {
g.DeleteView("errors")
}
// Display confirmation message
func displayConfirmation(g *gocui.Gui, m string) error {
lMaxX, lMaxY := g.Size()
if v, err := g.SetView("confirmation", -1, lMaxY-3, lMaxX, lMaxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Frame = false
// Content
fmt.Fprintln(v, textPadCenter(m, lMaxX))
// Auto-hide message
hide := func() {
hideConfirmation(g)
}
time.AfterFunc(time.Duration(2)*time.Second, hide)
}
return nil
}
// Hide confirmation message
func hideConfirmation(g *gocui.Gui) {
g.DeleteView("confirmation")
}