-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
context.go
63 lines (54 loc) 路 1.41 KB
/
context.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
package view
import (
"errors"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/gdamore/tcell/v2"
"github.com/rs/zerolog/log"
)
// Context presents a context viewer.
type Context struct {
ResourceViewer
}
// NewContext returns a new viewer.
func NewContext(gvr client.GVR) ResourceViewer {
c := Context{
ResourceViewer: NewBrowser(gvr),
}
c.GetTable().SetEnterFn(c.useCtx)
c.GetTable().SetColorerFn(render.Context{}.ColorerFunc())
c.AddBindKeysFn(c.bindKeys)
return &c
}
func (c *Context) bindKeys(aa ui.KeyActions) {
aa.Delete(ui.KeyShiftA, tcell.KeyCtrlSpace, ui.KeySpace)
}
func (c *Context) useCtx(app *App, model ui.Tabular, gvr, path string) {
log.Debug().Msgf("SWITCH CTX %q--%q", gvr, path)
if err := useContext(app, path); err != nil {
app.Flash().Err(err)
return
}
c.Refresh()
c.GetTable().Select(1, 0)
}
func useContext(app *App, name string) error {
if app.Content.Top() != nil {
app.Content.Top().Stop()
}
res, err := dao.AccessorFor(app.factory, client.NewGVR("contexts"))
if err != nil {
return err
}
switcher, ok := res.(dao.Switchable)
if !ok {
return errors.New("Expecting a switchable resource")
}
if err := switcher.Switch(name); err != nil {
log.Error().Err(err).Msgf("Context switch failed")
return err
}
return app.switchContext(name, true)
}