-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
args.go
79 lines (66 loc) 路 1.47 KB
/
args.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package cmd
import (
"strings"
)
const (
nsKey = "ns"
topicKey = "topic"
filterKey = "filter"
fuzzyKey = "fuzzy"
labelKey = "labels"
contextKey = "context"
)
type args map[string]string
func newArgs(p *Interpreter, aa []string) args {
args := make(args, len(aa))
if len(aa) == 0 {
return args
}
for i := 0; i < len(aa); i++ {
a := strings.TrimSpace(aa[i])
switch {
case strings.Index(a, contextFlag) == 0:
args[contextKey] = a[1:]
case strings.Index(a, fuzzyFlag) == 0:
if a == fuzzyFlag {
if i++; i < len(aa) {
args[fuzzyKey] = strings.ToLower(strings.TrimSpace(aa[i]))
}
} else {
args[fuzzyKey] = strings.ToLower(a[2:])
}
case strings.Index(a, filterFlag) == 0:
args[filterKey] = strings.ToLower(a[1:])
case strings.Contains(a, labelFlag):
if ll := ToLabels(a); len(ll) != 0 {
args[labelKey] = strings.ToLower(a)
}
default:
switch {
case p.IsContextCmd():
args[contextKey] = a
case p.IsDirCmd():
if _, ok := args[topicKey]; !ok {
args[topicKey] = a
}
case p.IsXrayCmd():
if _, ok := args[topicKey]; ok {
args[nsKey] = strings.ToLower(a)
} else {
args[topicKey] = strings.ToLower(a)
}
default:
args[nsKey] = strings.ToLower(a)
}
}
}
return args
}
func (a args) hasFilters() bool {
_, fok := a[filterKey]
_, zok := a[fuzzyKey]
_, lok := a[labelKey]
return fok || zok || lok
}