/
restart_extender.go
86 lines (72 loc) · 1.95 KB
/
restart_extender.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
package view
import (
"context"
"errors"
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/kswapd/k13s/internal/dao"
"github.com/kswapd/k13s/internal/ui"
"github.com/kswapd/k13s/internal/ui/dialog"
)
// RestartExtender represents a restartable resource.
type RestartExtender struct {
ResourceViewer
}
// NewRestartExtender returns a new extender.
func NewRestartExtender(v ResourceViewer) ResourceViewer {
r := RestartExtender{ResourceViewer: v}
v.AddBindKeysFn(r.bindKeys)
return &r
}
// BindKeys creates additional menu actions.
func (r *RestartExtender) bindKeys(aa ui.KeyActions) {
if r.App().Config.K9s.IsReadOnly() {
return
}
aa.Add(ui.KeyActions{
ui.KeyR: ui.NewKeyAction("Restart", r.restartCmd, true),
})
}
func (r *RestartExtender) restartCmd(evt *tcell.EventKey) *tcell.EventKey {
paths := r.GetTable().GetSelectedItems()
if len(paths) == 0 || paths[0] == "" {
return nil
}
r.Stop()
defer r.Start()
msg := fmt.Sprintf("Restart %s %s?", singularize(r.GVR().R()), paths[0])
if len(paths) > 1 {
msg = fmt.Sprintf("Restart %d %s?", len(paths), r.GVR().R())
}
dialog.ShowConfirm(r.App().Styles.Dialog(), r.App().Content.Pages, "Confirm Restart", msg, func() {
ctx, cancel := context.WithTimeout(context.Background(), r.App().Conn().Config().CallTimeout())
defer cancel()
for _, path := range paths {
if err := r.restartRollout(ctx, path); err != nil {
r.App().Flash().Err(err)
} else {
r.App().Flash().Infof("Restart in progress for `%s...", path)
}
}
}, func() {})
return nil
}
func (r *RestartExtender) restartRollout(ctx context.Context, path string) error {
res, err := dao.AccessorFor(r.App().factory, r.GVR())
if err != nil {
return err
}
s, ok := res.(dao.Restartable)
if !ok {
return errors.New("resource is not restartable")
}
return s.Restart(ctx, path)
}
// Helpers...
func singularize(s string) string {
if strings.LastIndex(s, "s") == len(s)-1 {
return s[:len(s)-1]
}
return s
}