-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
interpreter.go
229 lines (184 loc) 路 4.32 KB
/
interpreter.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package cmd
import (
"strings"
)
// Interpreter tracks user prompt input.
type Interpreter struct {
line string
cmd string
args args
}
// NewInterpreter returns a new instance.
func NewInterpreter(s string) *Interpreter {
c := Interpreter{
line: s,
args: make(args),
}
c.grok()
return &c
}
func (c *Interpreter) grok() {
ff := strings.Fields(c.line)
if len(ff) == 0 {
return
}
c.cmd = strings.ToLower(ff[0])
c.args = newArgs(c, ff[1:])
}
// HasNS returns true if ns is present in prompt.
func (c *Interpreter) HasNS() bool {
ns, ok := c.args[nsKey]
return ok && ns != ""
}
// Cmd returns the command.
func (c *Interpreter) Cmd() string {
return c.cmd
}
// IsBlank returns true if prompt is empty.
func (c *Interpreter) IsBlank() bool {
return c.line == ""
}
// Amend merges prompts.
func (c *Interpreter) Amend(c1 *Interpreter) {
c.cmd = c1.cmd
if c.args == nil {
c.args = make(args, len(c1.args))
}
for k, v := range c1.args {
if v != "" {
c.args[k] = v
}
}
}
// Reset resets with new command.
func (c *Interpreter) Reset(s string) *Interpreter {
c.line = s
c.grok()
return c
}
// GetLine teturns the prompt.
func (c *Interpreter) GetLine() string {
return strings.TrimSpace(c.line)
}
// IsCowCmd returns true if cow cmd is detected.
func (c *Interpreter) IsCowCmd() bool {
return c.cmd == cowCmd
}
// IsHelpCmd returns true if help cmd is detected.
func (c *Interpreter) IsHelpCmd() bool {
_, ok := helpCmd[c.cmd]
return ok
}
// IsBailCmd returns true if quit cmd is detected.
func (c *Interpreter) IsBailCmd() bool {
_, ok := bailCmd[c.cmd]
return ok
}
// IsAliasCmd returns true if alias cmd is detected.
func (c *Interpreter) IsAliasCmd() bool {
_, ok := aliasCmd[c.cmd]
return ok
}
// IsXrayCmd returns true if xray cmd is detected.
func (c *Interpreter) IsXrayCmd() bool {
_, ok := xrayCmd[c.cmd]
return ok
}
// IsContextCmd returns true if context cmd is detected.
func (c *Interpreter) IsContextCmd() bool {
_, ok := contextCmd[c.cmd]
return ok
}
// IsDirCmd returns true if dir cmd is detected.
func (c *Interpreter) IsDirCmd() bool {
_, ok := dirCmd[c.cmd]
return ok
}
// IsRBACCmd returns true if rbac cmd is detected.
func (c *Interpreter) IsRBACCmd() bool {
return c.cmd == canCmd
}
// ContextArg returns context cmd arg.
func (c *Interpreter) ContextArg() (string, bool) {
if !c.IsContextCmd() {
return "", false
}
return c.args[contextKey], true
}
// ResetContextArg deletes context arg.
func (c *Interpreter) ResetContextArg() {
delete(c.args, contextFlag)
}
// DirArg returns the directory is present.
func (c *Interpreter) DirArg() (string, bool) {
if !c.IsDirCmd() {
return "", false
}
d, ok := c.args[topicKey]
return d, ok && d != ""
}
// CowArg returns the cow message.
func (c *Interpreter) CowArg() (string, bool) {
if !c.IsCowCmd() {
return "", false
}
m, ok := c.args[nsKey]
return m, ok && m != ""
}
// RBACArgs returns the subject and topic is any.
func (c *Interpreter) RBACArgs() (string, string, bool) {
if !c.IsRBACCmd() {
return "", "", false
}
tt := rbacRX.FindStringSubmatch(c.line)
if len(tt) < 3 {
return "", "", false
}
return tt[1], tt[2], true
}
// XRayArgs return the gvr and ns if any.
func (c *Interpreter) XrayArgs() (string, string, bool) {
if !c.IsXrayCmd() {
return "", "", false
}
gvr, ok1 := c.args[topicKey]
if !ok1 {
return "", "", false
}
ns, ok2 := c.args[nsKey]
switch {
case ok1 && ok2:
return gvr, ns, true
case ok1 && !ok2:
return gvr, "", true
default:
return "", "", false
}
}
// FilterArg returns the current filter if any.
func (c *Interpreter) FilterArg() (string, bool) {
f, ok := c.args[filterKey]
return f, ok && f != ""
}
// FuzzyArg returns the fuzzy filter if any.
func (c *Interpreter) FuzzyArg() (string, bool) {
f, ok := c.args[fuzzyKey]
return f, ok && f != ""
}
// NSArg returns the current ns if any.
func (c *Interpreter) NSArg() (string, bool) {
ns, ok := c.args[nsKey]
return ns, ok && ns != ""
}
// HasContext returns the current context if any.
func (c *Interpreter) HasContext() (string, bool) {
ctx, ok := c.args[contextKey]
return ctx, ok && ctx != ""
}
// LabelsArg return the labels map if any.
func (c *Interpreter) LabelsArg() (map[string]string, bool) {
ll, ok := c.args[labelKey]
return ToLabels(ll), ok
}