forked from derailed/k9s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
275 lines (241 loc) · 5.71 KB
/
command.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
package view
import (
"errors"
"fmt"
"regexp"
"strings"
"sync"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/model"
"github.com/rs/zerolog/log"
)
var (
customViewers MetaViewers
canRX = regexp.MustCompile(`\Acan\s([u|g|s]):([\w-:]+)\b`)
)
// Command represents a user command.
type Command struct {
app *App
alias *dao.Alias
mx sync.Mutex
}
// NewCommand returns a new command.
func NewCommand(app *App) *Command {
return &Command{
app: app,
}
}
// Init initializes the command.
func (c *Command) Init() error {
c.alias = dao.NewAlias(c.app.factory)
if _, err := c.alias.Ensure(); err != nil {
log.Error().Err(err).Msgf("command init failed!")
return err
}
customViewers = loadCustomViewers()
return nil
}
// Reset resets Command and reload aliases.
func (c *Command) Reset(clear bool) error {
c.mx.Lock()
defer c.mx.Unlock()
if clear {
c.alias.Clear()
}
if _, err := c.alias.Ensure(); err != nil {
return err
}
return nil
}
func allowedXRay(gvr client.GVR) bool {
gg := []string{
"v1/pods",
"v1/services",
"apps/v1/deployments",
"apps/v1/daemonsets",
"apps/v1/statefulsets",
"apps/v1/replicasets",
}
for _, g := range gg {
if g == gvr.String() {
return true
}
}
return false
}
func (c *Command) xrayCmd(cmd string) error {
tokens := strings.Split(cmd, " ")
if len(tokens) < 2 {
return errors.New("You must specify a resource")
}
gvr, ok := c.alias.AsGVR(tokens[1])
if !ok {
return fmt.Errorf("`%s` command not found", cmd)
}
if !allowedXRay(gvr) {
return fmt.Errorf("`%s` command not found", cmd)
}
x := NewXray(gvr)
ns := c.app.Config.ActiveNamespace()
if len(tokens) == 3 {
ns = tokens[2]
}
if err := c.app.Config.SetActiveNamespace(client.CleanseNamespace(ns)); err != nil {
return err
}
if err := c.app.Config.Save(); err != nil {
return err
}
return c.exec(cmd, "xrays", x, true)
}
// Exec the Command by showing associated display.
func (c *Command) run(cmd, path string, clearStack bool) error {
if c.specialCmd(cmd, path) {
return nil
}
cmds := strings.Split(cmd, " ")
gvr, v, err := c.viewMetaFor(cmds[0])
if err != nil {
return err
}
switch cmds[0] {
case "ctx", "context", "contexts":
if len(cmds) == 2 {
return useContext(c.app, cmds[1])
}
return c.exec(cmd, gvr, c.componentFor(gvr, path, v), clearStack)
case "dir":
if len(cmds) != 2 {
return errors.New("You must specify a directory")
}
return c.app.dirCmd(cmds[1])
default:
// checks if Command includes a namespace
ns := c.app.Config.ActiveNamespace()
if len(cmds) == 2 {
ns = cmds[1]
}
if err := c.app.switchNS(ns); err != nil {
return err
}
if !c.alias.Check(cmds[0]) {
return fmt.Errorf("`%s` Command not found", cmd)
}
return c.exec(cmd, gvr, c.componentFor(gvr, path, v), clearStack)
}
}
func (c *Command) defaultCmd() error {
if !c.app.Conn().ConnectionOK() {
return c.run("ctx", "", true)
}
view := c.app.Config.ActiveView()
if view == "" {
return c.run("pod", "", true)
}
tokens := strings.Split(view, " ")
cmd := view
ns, err := c.app.Conn().Config().CurrentNamespaceName()
if err == nil && !isContextCmd(tokens[0]) {
cmd = tokens[0] + " " + ns
}
if err := c.run(cmd, "", true); err != nil {
log.Error().Err(err).Msgf("Default run command failed")
return c.run("meow", err.Error(), true)
}
return nil
}
func isContextCmd(c string) bool {
return c == "ctx" || c == "context"
}
func (c *Command) specialCmd(cmd, path string) bool {
cmds := strings.Split(cmd, " ")
switch cmds[0] {
case "meow":
c.app.meowCmd(path)
return true
case "q", "Q", "quit":
c.app.BailOut()
return true
case "?", "h", "help":
c.app.helpCmd(nil)
return true
case "a", "alias":
c.app.aliasCmd(nil)
return true
case "x", "xray":
if err := c.xrayCmd(cmd); err != nil {
c.app.Flash().Err(err)
}
return true
default:
if !canRX.MatchString(cmd) {
return false
}
tokens := canRX.FindAllStringSubmatch(cmd, -1)
if len(tokens) == 1 && len(tokens[0]) == 3 {
if err := c.app.inject(NewPolicy(c.app, tokens[0][1], tokens[0][2])); err != nil {
log.Error().Err(err).Msgf("policy view load failed")
return false
}
return true
}
}
return false
}
func (c *Command) viewMetaFor(cmd string) (string, *MetaViewer, error) {
gvr, ok := c.alias.AsGVR(cmd)
if !ok {
return "", nil, fmt.Errorf("`%s` command not found", cmd)
}
v, ok := customViewers[gvr]
if !ok {
return gvr.String(), &MetaViewer{viewerFn: NewBrowser}, nil
}
return gvr.String(), &v, nil
}
func (c *Command) componentFor(gvr, path string, v *MetaViewer) ResourceViewer {
var view ResourceViewer
if v.viewerFn != nil {
view = v.viewerFn(client.NewGVR(gvr))
} else {
view = NewBrowser(client.NewGVR(gvr))
}
view.SetInstance(path)
if v.enterFn != nil {
view.GetTable().SetEnterFn(v.enterFn)
}
return view
}
func (c *Command) exec(cmd, gvr string, comp model.Component, clearStack bool) (err error) {
defer func() {
if e := recover(); e != nil {
log.Error().Msgf("Something bad happened! %#v", e)
c.app.Content.Dump()
log.Debug().Msgf("History %v", c.app.cmdHistory.List())
hh := c.app.cmdHistory.List()
if len(hh) == 0 {
_ = c.run("pod", "", true)
} else {
_ = c.run(hh[0], "", true)
}
err = fmt.Errorf("Invalid command %q", cmd)
}
}()
if comp == nil {
return fmt.Errorf("No component found for %s", gvr)
}
c.app.Flash().Infof("Viewing %s...", client.NewGVR(gvr).R())
c.app.Config.SetActiveView(cmd)
if err := c.app.Config.Save(); err != nil {
log.Error().Err(err).Msg("Config save failed!")
}
if clearStack {
c.app.Content.Stack.Clear()
}
if err := c.app.inject(comp); err != nil {
return err
}
c.app.cmdHistory.Push(cmd)
return
}