Skip to content

Commit

Permalink
Merge pull request #3037 from hashicorp/b-limit-tab-autocomplete
Browse files Browse the repository at this point in the history
Tab completion should only happen once
  • Loading branch information
chelseakomlo authored Aug 16, 2017
2 parents 99a1162 + ca6562a commit b90980b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion command/alloc_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/dustin/go-humanize"
humanize "github.com/dustin/go-humanize"
"github.com/mitchellh/colorstring"

"github.com/hashicorp/nomad/api"
Expand Down Expand Up @@ -200,7 +200,12 @@ func (c *AllocStatusCommand) AutocompleteFlags() complete.Flags {

func (c *AllocStatusCommand) AutocompleteArgs() complete.Predictor {
client, _ := c.Meta.Client()

return complete.PredictFunc(func(a complete.Args) []string {
if len(a.Completed) > 1 {
return nil
}

resp, err := client.Search().PrefixSearch(a.Last, contexts.Allocs)
if err != nil {
return []string{}
Expand Down
7 changes: 7 additions & 0 deletions command/alloc_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,11 @@ func TestAllocStatusCommand_AutocompleteArgs(t *testing.T) {
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(allocID, res[0])

// Autocomplete should only complete once
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}
predictor = cmd.AutocompleteArgs()

res = predictor.Predict(args)
assert.Nil(res)
}
4 changes: 4 additions & 0 deletions command/eval_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ func (c *EvalStatusCommand) AutocompleteFlags() complete.Flags {
func (c *EvalStatusCommand) AutocompleteArgs() complete.Predictor {
client, _ := c.Meta.Client()
return complete.PredictFunc(func(a complete.Args) []string {
if len(a.Completed) > 1 {
return nil
}

resp, err := client.Search().PrefixSearch(a.Last, contexts.Evals)
if err != nil {
return []string{}
Expand Down
7 changes: 7 additions & 0 deletions command/eval_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,11 @@ func TestEvalStatusCommand_AutocompleteArgs(t *testing.T) {
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(evalID, res[0])

// Autocomplete should only complete once
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}
predictor = cmd.AutocompleteArgs()

res = predictor.Predict(args)
assert.Nil(res)
}
4 changes: 4 additions & 0 deletions command/job_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ func (c *JobStatusCommand) AutocompleteFlags() complete.Flags {
func (c *JobStatusCommand) AutocompleteArgs() complete.Predictor {
client, _ := c.Meta.Client()
return complete.PredictFunc(func(a complete.Args) []string {
if len(a.Completed) > 1 {
return nil
}

resp, err := client.Search().PrefixSearch(a.Last, contexts.Jobs)
if err != nil {
return []string{}
Expand Down
8 changes: 8 additions & 0 deletions command/job_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ func TestJobStatusCommand_AutocompleteArgs(t *testing.T) {
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(jobID, res[0])

// Autocomplete should only complete once
args = complete.Args{Last: prefix, Completed: []string{prefix, "a", "b"}}
prefix = jobID[:len(jobID)-5]
predictor = cmd.AutocompleteArgs()

res = predictor.Predict(args)
assert.Nil(res)
}

func waitForSuccess(ui cli.Ui, client *api.Client, length int, t *testing.T, evalId string) int {
Expand Down
4 changes: 4 additions & 0 deletions command/node_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ func (c *NodeStatusCommand) AutocompleteFlags() complete.Flags {
func (c *NodeStatusCommand) AutocompleteArgs() complete.Predictor {
client, _ := c.Meta.Client()
return complete.PredictFunc(func(a complete.Args) []string {
if len(a.Completed) > 1 {
return nil
}

resp, err := client.Search().PrefixSearch(a.Last, contexts.Nodes)
if err != nil {
return []string{}
Expand Down
7 changes: 7 additions & 0 deletions command/node_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,11 @@ func TestNodeStatusCommand_AutocompleteArgs(t *testing.T) {
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(nodeID, res[0])

// Autocomplete should only complete once
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}
predictor = cmd.AutocompleteArgs()

res = predictor.Predict(args)
assert.Nil(res)
}

0 comments on commit b90980b

Please sign in to comment.