forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tabcompletion.go
210 lines (185 loc) · 4.84 KB
/
tabcompletion.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
package commands
import (
"fmt"
"sort"
"strings"
"unicode"
"github.com/chzyer/readline"
"github.com/wallix/awless/aws/services"
"github.com/wallix/awless/cloud"
"github.com/wallix/awless/graph"
"github.com/wallix/awless/template"
)
func holeAutoCompletion(g *graph.Graph, hole string) readline.AutoCompleter {
completeFunc := func(string) []string { return []string{} }
if entityType, entityProp := guessEntityTypeFromHoleQuestion(hole); entityType != "" {
resources, err := g.GetAllResources(entityType)
exitOn(err)
if len(resources) == 0 {
return &prefixCompleter{callback: completeFunc}
}
if entityProp != "" {
var validPropName string
for _, r := range resources {
for propName := range r.Properties {
if keyCorrespondsToProperty(entityProp, propName) {
validPropName = propName
}
}
}
if validPropName != "" {
completeFunc = func(s string) (suggest []string) {
for _, res := range resources {
if v, ok := res.Properties[validPropName]; ok {
switch prop := v.(type) {
case string, float64, int, bool:
suggest = appendIfContains(suggest, fmt.Sprint(prop), s)
case []string:
for _, str := range prop {
suggest = appendIfContains(suggest, str, s)
}
case []*graph.KeyValue:
for _, kv := range prop {
suggest = appendIfContains(suggest, fmt.Sprintf("%s:%s", kv.KeyName, kv.Value), s)
}
}
}
}
suggest = quotedSortedSet(suggest)
return
}
}
return &prefixCompleter{callback: completeFunc, splitChar: ","}
}
completeFunc = func(s string) (suggest []string) {
s = splitKeepLast(s, ",")
s = strings.TrimLeft(s, "'@\"")
for _, res := range resources {
suggest = appendIfContains(suggest, res.Id(), s)
if val, ok := res.Properties["Name"]; ok {
switch val.(type) {
case string:
name := val.(string)
if name != "" {
suggest = appendIfContains(suggest, fmt.Sprintf("@%s", name), s)
}
}
}
}
suggest = quotedSortedSet(suggest)
return
}
}
return &prefixCompleter{callback: completeFunc, splitChar: ","}
}
type prefixCompleter struct {
callback readline.DynamicCompleteFunc
splitChar string
}
func (p *prefixCompleter) Do(line []rune, pos int) (newLine [][]rune, offset int) {
var lines []string
lines, offset = doInternal(p, string(line), pos, line)
for _, l := range lines {
newLine = append(newLine, []rune(l))
}
return
}
func doInternal(p *prefixCompleter, line string, pos int, origLine []rune) (newLine []string, offset int) {
strings.TrimLeftFunc(line[:pos], func(r rune) bool {
return unicode.IsSpace(r)
})
if p.splitChar != "" {
line = splitKeepLast(line, p.splitChar)
}
for _, suggest := range p.callback(line) {
if len(line) >= len(suggest) {
if strings.HasPrefix(line, suggest) {
if len(line) != len(suggest) {
newLine = append(newLine, suggest)
}
offset = len(suggest)
}
} else {
if strings.HasPrefix(suggest, line) {
newLine = append(newLine, suggest[len(line):])
offset = len(line)
}
}
}
return
}
func splitKeepLast(s, sep string) (last string) {
if !strings.Contains(s, sep) {
last = s
return
}
offset := strings.LastIndex(s, sep)
if offset+1 < len(s) {
last = s[offset+1 : len(s)]
}
return
}
func quotedSortedSet(list []string) (out []string) {
unique := make(map[string]bool)
for _, l := range list {
unique[l] = true
}
for k := range unique {
if !template.MatchStringParamValue(k) {
k = "'" + k + "'"
}
out = append(out, k)
}
sort.Strings(out)
return
}
// Return 2 strings according to holes questions:
// notmatching? -> ["", ""]
// entitytype? -> [entitytype, ""]
// entitytype.anything? -> [entitytype, anything]
// entitytype.otherentitytype? -> [otherentitytype, ""]
func guessEntityTypeFromHoleQuestion(hole string) (string, string) {
var types []string
splits := strings.Split(hole, ".")
for _, t := range splits {
for _, r := range resourcesTypesWithPlural {
if t == r {
types = append(types, cloud.SingularizeResource(r))
break
}
}
}
var prop string
if len(splits) == 2 {
prop = splits[1]
}
if l := len(types); l == 1 {
return types[0], prop
} else if l == 2 {
return types[1], ""
}
return "", ""
}
func keyCorrespondsToProperty(holekey, prop string) bool {
holekey = strings.ToLower(holekey)
prop = strings.ToLower(prop)
if holekey == prop {
return true
}
if strings.Replace(holekey, "-", "", -1) == prop {
return true
}
return false
}
func appendIfContains(slice []string, value, subst string) []string {
if strings.Contains(value, subst) && value != "" {
return append(slice, value)
}
return slice
}
var resourcesTypesWithPlural []string
func init() {
for _, r := range awsservices.ResourceTypes {
resourcesTypesWithPlural = append(resourcesTypesWithPlural, r, cloud.PluralizeResource(r))
}
}