diff --git a/command/inspect_test.go b/command/inspect_test.go index 9861fccc3..797bfa897 100644 --- a/command/inspect_test.go +++ b/command/inspect_test.go @@ -67,7 +67,7 @@ func TestInspectCommand_AutocompleteArgs(t *testing.T) { defer srv.Shutdown() ui := new(cli.MockUi) - cmd := &JobStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}} + cmd := &InspectCommand{Meta: Meta{Ui: ui, flagAddress: url}} state := srv.Agent.Server().State() j := mock.Job() diff --git a/command/stop.go b/command/stop.go index 5d3934174..e95192d35 100644 --- a/command/stop.go +++ b/command/stop.go @@ -3,6 +3,9 @@ package command import ( "fmt" "strings" + + "github.com/hashicorp/nomad/api/contexts" + "github.com/posener/complete" ) type StopCommand struct { @@ -48,6 +51,25 @@ func (c *StopCommand) Synopsis() string { return "Stop a running job" } +func (c *StopCommand) AutocompleteFlags() complete.Flags { + return nil +} + +func (c *StopCommand) 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{} + } + return resp.Matches[contexts.Jobs] + }) +} + func (c *StopCommand) Run(args []string) int { var detach, purge, verbose, autoYes bool diff --git a/command/stop_test.go b/command/stop_test.go index 6732927a4..f89ca3357 100644 --- a/command/stop_test.go +++ b/command/stop_test.go @@ -4,7 +4,10 @@ import ( "strings" "testing" + "github.com/hashicorp/nomad/nomad/mock" "github.com/mitchellh/cli" + "github.com/posener/complete" + "github.com/stretchr/testify/assert" ) func TestStopCommand_Implements(t *testing.T) { @@ -46,3 +49,33 @@ func TestStopCommand_Fails(t *testing.T) { t.Fatalf("expected failed query error, got: %s", out) } } + +func TestStopCommand_AutocompleteArgs(t *testing.T) { + assert := assert.New(t) + t.Parallel() + + srv, _, url := testServer(t, true, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &StopCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + state := srv.Agent.Server().State() + j := mock.Job() + assert.Nil(state.UpsertJob(1000, j)) + + prefix := j.ID[:len(j.ID)-5] + args := complete.Args{Last: prefix} + predictor := cmd.AutocompleteArgs() + + res := predictor.Predict(args) + assert.Equal(1, len(res)) + assert.Equal(j.ID, res[0]) + + // Autocomplete should only complete once + args = complete.Args{Last: prefix, Completed: []string{prefix, "a", "b"}} + predictor = cmd.AutocompleteArgs() + + res = predictor.Predict(args) + assert.Nil(res) +}