Skip to content

Commit 1f56b1b

Browse files
committed
Better prompt completion for template parameters
1 parent 9e5c007 commit 1f56b1b

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
### Features
44

5+
- Better prompt completion for template parameters
56
- Create instance/launchconfiguration from community distro names (`awless create instance distro=debian`). In default config value, deprecation of `instance.image` in favor of `instance.distro` (migration should be seamless).
67
* `awless create instance distro=redhat:rhel:7.2`
78
* `awless create launchconfiguration distro=canonical:ubuntu`
89
* `awless create instance distro=debian`
910
- Quick way to switch to profiles and regions. Ex: `awless switch eu-west-1`, `awless switch mfa us-west-1`
1011
- Create a public subnet in only one command with: `awless create subnet public=true...`
1112
- Save directly your newly created access key in `~/.aws/credentials` with : `awless create accesskey save=true`
12-
- Better prompt completion for enum value
1313
- Overall better logging output of template execution
1414

1515
### AWS Services

aws/doc/enumsdoc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package awsdoc
22

3+
import (
4+
"github.com/wallix/awless/cloud"
5+
"github.com/wallix/awless/cloud/properties"
6+
)
7+
38
var EnumDoc = map[string][]string{
49
"update.securitygroup.inbound": {"revoke", "authorize"},
510
"update.securitygroup.outbound": {"revoke", "authorize"},
@@ -17,4 +22,17 @@ var EnumDoc = map[string][]string{
1722

1823
"create.launchconfiguration.distro": {"amazonlinux", "canonical", "redhat", "debian", "suselinux", "windows"},
1924
"create.launchconfiguration.type": {"t2.nano", "t2.micro", "t2.small", "t2.medium", "t2.large", "t2.xlarge", "t2.2xlarge", "m4.large", "m4.xlarge", "c4.large", "c4.xlarge"},
25+
26+
"create.policy.action": {""},
27+
"create.policy.effect": {"Allow", "Deny"},
28+
"create.policy.resource": {"*"},
29+
}
30+
31+
type ParamType struct {
32+
ResourceType, PropertyName string
33+
}
34+
35+
var ParamTypeDoc = map[string]*ParamType{
36+
"create.accesskey.user": {ResourceType: cloud.User, PropertyName: properties.Name},
37+
"update.securitygroup.cidr": {ResourceType: cloud.Subnet, PropertyName: properties.CIDR},
2038
}

commands/run.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func missingHolesStdinFunc() func(string, []string) interface{} {
134134
fmt.Println("Please specify (Ctrl+C to quit, Tab for completion):")
135135
}
136136
var docs, enums []string
137+
var typedParam *awsdoc.ParamType
137138
for _, param := range paramPaths {
138139
splits := strings.Split(param, ".")
139140
if len(splits) != 3 {
@@ -145,12 +146,19 @@ func missingHolesStdinFunc() func(string, []string) interface{} {
145146
if enum, hasEnum := awsdoc.EnumDoc[param]; hasEnum {
146147
enums = append(enums, enum...)
147148
}
149+
if tparam, has := awsdoc.ParamTypeDoc[param]; has {
150+
typedParam = tparam
151+
}
148152
}
149153
if len(docs) > 0 {
150154
fmt.Fprintln(os.Stderr, strings.Join(docs, "; ")+":")
151155
}
152156

153157
autocomplete := holeAutoCompletion(allGraphsOnce.mustLoad(), hole)
158+
if typedParam != nil {
159+
autocomplete = typedParamCompletionFunc(allGraphsOnce.mustLoad(), typedParam.ResourceType, typedParam.PropertyName)
160+
}
161+
154162
if len(enums) > 0 {
155163
autocomplete = enumCompletionFunc(enums)
156164
}

commands/tabcompletion.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ func enumCompletionFunc(enum []string) readline.AutoCompleter {
2121
return readline.NewPrefixCompleter(items...)
2222
}
2323

24+
func typedParamCompletionFunc(g *graph.Graph, resourceType, propName string) readline.AutoCompleter {
25+
var items []readline.PrefixCompleterInterface
26+
resources, _ := g.GetAllResources(resourceType)
27+
for _, res := range resources {
28+
if val, ok := res.Properties[propName]; ok {
29+
items = append(items, readline.PcItem(fmt.Sprint(val)))
30+
}
31+
}
32+
33+
return readline.NewPrefixCompleter(items...)
34+
}
2435
func holeAutoCompletion(g *graph.Graph, hole string) readline.AutoCompleter {
2536
completeFunc := func(string) []string { return []string{} }
2637

commands/tabcompletion_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ func TestEnumCompletionFunc(t *testing.T) {
1616
}
1717
}
1818

19+
func TestTypedParamCompletionFunc(t *testing.T) {
20+
g := graph.NewGraph()
21+
g.AddResource(resourcetest.Instance("1").Prop(p.Name, "broker_1").Build())
22+
g.AddResource(resourcetest.Instance("2").Prop(p.Name, "broker_2").Build())
23+
g.AddResource(resourcetest.Instance("3").Prop(p.Name, "redis").Build())
24+
25+
list, _ := typedParamCompletionFunc(g, "instance", p.Name).Do([]rune{'b'}, 1)
26+
if got, want := list, toRune("roker_1 ", "roker_2 "); !reflect.DeepEqual(got, want) {
27+
t.Fatalf("got %q, want %q", got, want)
28+
}
29+
}
30+
1931
func TestAutoCompletion(t *testing.T) {
2032
g := graph.NewGraph()
2133
g.AddResource(resourcetest.Instance("1").Prop(p.Name, "broker_1").Prop(p.Type, "t2.micro").Prop(p.Subnet, "1").Prop(p.ActiveServicesCount, 42).Build())

0 commit comments

Comments
 (0)