Skip to content

Commit

Permalink
Better prompt completion for template parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
fxaguessy committed Nov 24, 2017
1 parent 9e5c007 commit 1f56b1b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

### Features

- Better prompt completion for template parameters
- 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).
* `awless create instance distro=redhat:rhel:7.2`
* `awless create launchconfiguration distro=canonical:ubuntu`
* `awless create instance distro=debian`
- Quick way to switch to profiles and regions. Ex: `awless switch eu-west-1`, `awless switch mfa us-west-1`
- Create a public subnet in only one command with: `awless create subnet public=true...`
- Save directly your newly created access key in `~/.aws/credentials` with : `awless create accesskey save=true`
- Better prompt completion for enum value
- Overall better logging output of template execution

### AWS Services
Expand Down
18 changes: 18 additions & 0 deletions aws/doc/enumsdoc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package awsdoc

import (
"github.com/wallix/awless/cloud"
"github.com/wallix/awless/cloud/properties"
)

var EnumDoc = map[string][]string{
"update.securitygroup.inbound": {"revoke", "authorize"},
"update.securitygroup.outbound": {"revoke", "authorize"},
Expand All @@ -17,4 +22,17 @@ var EnumDoc = map[string][]string{

"create.launchconfiguration.distro": {"amazonlinux", "canonical", "redhat", "debian", "suselinux", "windows"},
"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"},

"create.policy.action": {""},
"create.policy.effect": {"Allow", "Deny"},
"create.policy.resource": {"*"},
}

type ParamType struct {
ResourceType, PropertyName string
}

var ParamTypeDoc = map[string]*ParamType{
"create.accesskey.user": {ResourceType: cloud.User, PropertyName: properties.Name},
"update.securitygroup.cidr": {ResourceType: cloud.Subnet, PropertyName: properties.CIDR},
}
8 changes: 8 additions & 0 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func missingHolesStdinFunc() func(string, []string) interface{} {
fmt.Println("Please specify (Ctrl+C to quit, Tab for completion):")
}
var docs, enums []string
var typedParam *awsdoc.ParamType
for _, param := range paramPaths {
splits := strings.Split(param, ".")
if len(splits) != 3 {
Expand All @@ -145,12 +146,19 @@ func missingHolesStdinFunc() func(string, []string) interface{} {
if enum, hasEnum := awsdoc.EnumDoc[param]; hasEnum {
enums = append(enums, enum...)
}
if tparam, has := awsdoc.ParamTypeDoc[param]; has {
typedParam = tparam
}
}
if len(docs) > 0 {
fmt.Fprintln(os.Stderr, strings.Join(docs, "; ")+":")
}

autocomplete := holeAutoCompletion(allGraphsOnce.mustLoad(), hole)
if typedParam != nil {
autocomplete = typedParamCompletionFunc(allGraphsOnce.mustLoad(), typedParam.ResourceType, typedParam.PropertyName)
}

if len(enums) > 0 {
autocomplete = enumCompletionFunc(enums)
}
Expand Down
11 changes: 11 additions & 0 deletions commands/tabcompletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ func enumCompletionFunc(enum []string) readline.AutoCompleter {
return readline.NewPrefixCompleter(items...)
}

func typedParamCompletionFunc(g *graph.Graph, resourceType, propName string) readline.AutoCompleter {
var items []readline.PrefixCompleterInterface
resources, _ := g.GetAllResources(resourceType)
for _, res := range resources {
if val, ok := res.Properties[propName]; ok {
items = append(items, readline.PcItem(fmt.Sprint(val)))
}
}

return readline.NewPrefixCompleter(items...)
}
func holeAutoCompletion(g *graph.Graph, hole string) readline.AutoCompleter {
completeFunc := func(string) []string { return []string{} }

Expand Down
12 changes: 12 additions & 0 deletions commands/tabcompletion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ func TestEnumCompletionFunc(t *testing.T) {
}
}

func TestTypedParamCompletionFunc(t *testing.T) {
g := graph.NewGraph()
g.AddResource(resourcetest.Instance("1").Prop(p.Name, "broker_1").Build())
g.AddResource(resourcetest.Instance("2").Prop(p.Name, "broker_2").Build())
g.AddResource(resourcetest.Instance("3").Prop(p.Name, "redis").Build())

list, _ := typedParamCompletionFunc(g, "instance", p.Name).Do([]rune{'b'}, 1)
if got, want := list, toRune("roker_1 ", "roker_2 "); !reflect.DeepEqual(got, want) {
t.Fatalf("got %q, want %q", got, want)
}
}

func TestAutoCompletion(t *testing.T) {
g := graph.NewGraph()
g.AddResource(resourcetest.Instance("1").Prop(p.Name, "broker_1").Prop(p.Type, "t2.micro").Prop(p.Subnet, "1").Prop(p.ActiveServicesCount, 42).Build())
Expand Down

0 comments on commit 1f56b1b

Please sign in to comment.