forked from wallix/awless
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tabcompletion.go
131 lines (111 loc) · 2.93 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
package commands
import (
"fmt"
"sort"
"strings"
"github.com/chzyer/readline"
"github.com/wallix/awless/aws"
"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 readline.NewPrefixCompleter(readline.PcItemDynamic(completeFunc))
}
if entityProp != "" {
var validPropName string
for _, r := range resources {
for key := range r.Properties {
if strings.ToLower(key) == strings.ToLower(entityProp) {
validPropName = key
}
}
}
if validPropName != "" {
completeFunc = func(s string) (suggest []string) {
for _, res := range resources {
if val, ok := res.Properties[validPropName].(string); ok {
suggest = append(suggest, val)
}
}
suggest = quotedSortedSet(suggest)
return
}
}
return readline.NewPrefixCompleter(readline.PcItemDynamic(completeFunc))
}
completeFunc = func(s string) (suggest []string) {
for _, res := range resources {
id := res.Id()
if strings.Contains(id, s) {
suggest = append(suggest, id)
}
if val, ok := res.Properties["Name"]; ok {
switch val.(type) {
case string:
name := val.(string)
prefixed := fmt.Sprintf("@%s", name)
if strings.Contains(prefixed, s) && name != "" {
suggest = append(suggest, prefixed)
}
}
}
}
suggest = quotedSortedSet(suggest)
return
}
}
return readline.NewPrefixCompleter(readline.PcItemDynamic(completeFunc))
}
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 "", ""
}
var resourcesTypesWithPlural []string
func init() {
for _, r := range aws.ResourceTypes {
resourcesTypesWithPlural = append(resourcesTypesWithPlural, r, cloud.PluralizeResource(r))
}
}