forked from derailed/k9s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcm.go
70 lines (59 loc) · 1.68 KB
/
cm.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
package view
import (
"context"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/ui"
"github.com/gdamore/tcell/v2"
)
// ConfigMap represents a configmap viewer.
type ConfigMap struct {
ResourceViewer
}
// NewConfigMap returns a new viewer.
func NewConfigMap(gvr client.GVR) ResourceViewer {
s := ConfigMap{
ResourceViewer: NewBrowser(gvr),
}
s.AddBindKeysFn(s.bindKeys)
return &s
}
func (s *ConfigMap) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
ui.KeyU: ui.NewKeyAction("UsedBy", s.refCmd, true),
})
}
func (s *ConfigMap) refCmd(evt *tcell.EventKey) *tcell.EventKey {
return scanRefs(evt, s.App(), s.GetTable(), "v1/configmaps")
}
func scanRefs(evt *tcell.EventKey, a *App, t *Table, gvr string) *tcell.EventKey {
path := t.GetSelectedItem()
if path == "" {
return evt
}
ctx := context.Background()
refs, err := dao.ScanForRefs(refContext(gvr, path, true)(ctx), a.factory)
if err != nil {
a.Flash().Err(err)
return nil
}
if len(refs) == 0 {
a.Flash().Warnf("No references found at this time for %s::%s. Check again later!", gvr, path)
return nil
}
a.Flash().Infof("Viewing references for %s::%s", gvr, path)
view := NewReference(client.NewGVR("references"))
view.SetContextFn(refContext(gvr, path, false))
if err := a.inject(view); err != nil {
a.Flash().Err(err)
}
return nil
}
func refContext(gvr, path string, wait bool) ContextFunc {
return func(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, internal.KeyPath, path)
ctx = context.WithValue(ctx, internal.KeyGVR, gvr)
return context.WithValue(ctx, internal.KeyWait, wait)
}
}