From f79c14ac4cdc87e232f556143f0c4c5269b6ab4b Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 22 Aug 2017 13:39:06 -0700 Subject: [PATCH] flesh out the rest of the job subcommands --- command/job_deployments.go | 22 +++++++++++++++++++++ command/job_deployments_test.go | 34 +++++++++++++++++++++++++++++++++ command/job_dispatch.go | 21 ++++++++++++++++++++ command/job_dispatch_test.go | 34 +++++++++++++++++++++++++++++++++ command/job_history.go | 21 ++++++++++++++++++++ command/job_history_test.go | 34 +++++++++++++++++++++++++++++++++ command/job_promote.go | 21 ++++++++++++++++++++ command/job_promote_test.go | 34 +++++++++++++++++++++++++++++++++ command/job_revert.go | 22 +++++++++++++++++++++ command/job_revert_test.go | 34 +++++++++++++++++++++++++++++++++ 10 files changed, 277 insertions(+) diff --git a/command/job_deployments.go b/command/job_deployments.go index f4f4452fa..d7918bf4b 100644 --- a/command/job_deployments.go +++ b/command/job_deployments.go @@ -3,6 +3,9 @@ package command import ( "fmt" "strings" + + "github.com/hashicorp/nomad/api/contexts" + "github.com/posener/complete" ) type JobDeploymentsCommand struct { @@ -40,6 +43,25 @@ func (c *JobDeploymentsCommand) Synopsis() string { return "List deployments for a job" } +func (c *JobDeploymentsCommand) AutocompleteFlags() complete.Flags { + return nil +} + +func (c *JobDeploymentsCommand) 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 *JobDeploymentsCommand) Run(args []string) int { var json, latest, verbose bool var tmpl string diff --git a/command/job_deployments_test.go b/command/job_deployments_test.go index eb365da10..2be5d2548 100644 --- a/command/job_deployments_test.go +++ b/command/job_deployments_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 TestJobDeploymentsCommand_Implements(t *testing.T) { @@ -34,3 +37,34 @@ func TestJobDeploymentsCommand_Fails(t *testing.T) { } ui.ErrorWriter.Reset() } + +func TestJobDeploymentsCommand_AutocompleteArgs(t *testing.T) { + assert := assert.New(t) + t.Parallel() + + srv, _, url := testServer(t, true, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &JobDeploymentsCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + // Create a fake job + 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) +} diff --git a/command/job_dispatch.go b/command/job_dispatch.go index 52f96b42e..16d35b14b 100644 --- a/command/job_dispatch.go +++ b/command/job_dispatch.go @@ -6,7 +6,9 @@ import ( "os" "strings" + "github.com/hashicorp/nomad/api/contexts" flaghelper "github.com/hashicorp/nomad/helper/flag-helpers" + "github.com/posener/complete" ) type JobDispatchCommand struct { @@ -54,6 +56,25 @@ func (c *JobDispatchCommand) Synopsis() string { return "Dispatch an instance of a parameterized job" } +func (c *JobDispatchCommand) AutocompleteFlags() complete.Flags { + return nil +} + +func (c *JobDispatchCommand) 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 *JobDispatchCommand) Run(args []string) int { var detach, verbose bool var meta []string diff --git a/command/job_dispatch_test.go b/command/job_dispatch_test.go index d0180dfb5..4ee9a9d2a 100644 --- a/command/job_dispatch_test.go +++ b/command/job_dispatch_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 TestJobDispatchCommand_Implements(t *testing.T) { @@ -43,3 +46,34 @@ func TestJobDispatchCommand_Fails(t *testing.T) { } ui.ErrorWriter.Reset() } + +func TestJobDispatchCommand_AutocompleteArgs(t *testing.T) { + assert := assert.New(t) + t.Parallel() + + srv, _, url := testServer(t, true, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &JobDispatchCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + // Create a fake job + 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) +} diff --git a/command/job_history.go b/command/job_history.go index d3712ba14..4e5c3062f 100644 --- a/command/job_history.go +++ b/command/job_history.go @@ -7,6 +7,8 @@ import ( "time" "github.com/hashicorp/nomad/api" + "github.com/hashicorp/nomad/api/contexts" + "github.com/posener/complete" "github.com/ryanuber/columnize" ) @@ -52,6 +54,25 @@ func (c *JobHistoryCommand) Synopsis() string { return "Display all tracked versions of a job" } +func (c *JobHistoryCommand) AutocompleteFlags() complete.Flags { + return nil +} + +func (c *JobHistoryCommand) 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 *JobHistoryCommand) Run(args []string) int { var json, diff, full bool var tmpl, versionStr string diff --git a/command/job_history_test.go b/command/job_history_test.go index 3aaf6ef18..d3190cd11 100644 --- a/command/job_history_test.go +++ b/command/job_history_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 TestJobHistoryCommand_Implements(t *testing.T) { @@ -34,3 +37,34 @@ func TestJobHistoryCommand_Fails(t *testing.T) { } ui.ErrorWriter.Reset() } + +func TestJobHistoryCommand_AutocompleteArgs(t *testing.T) { + assert := assert.New(t) + t.Parallel() + + srv, _, url := testServer(t, true, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &JobHistoryCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + // Create a fake job + 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) +} diff --git a/command/job_promote.go b/command/job_promote.go index fc101bf40..80d0eebf9 100644 --- a/command/job_promote.go +++ b/command/job_promote.go @@ -5,7 +5,9 @@ import ( "strings" "github.com/hashicorp/nomad/api" + "github.com/hashicorp/nomad/api/contexts" flaghelper "github.com/hashicorp/nomad/helper/flag-helpers" + "github.com/posener/complete" ) type JobPromoteCommand struct { @@ -50,6 +52,25 @@ func (c *JobPromoteCommand) Synopsis() string { return "Promote a job's canaries" } +func (c *JobPromoteCommand) AutocompleteFlags() complete.Flags { + return nil +} + +func (c *JobPromoteCommand) 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 *JobPromoteCommand) Run(args []string) int { var detach, verbose bool var groups []string diff --git a/command/job_promote_test.go b/command/job_promote_test.go index 0eec70792..c42d98746 100644 --- a/command/job_promote_test.go +++ b/command/job_promote_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 TestJobPromoteCommand_Implements(t *testing.T) { @@ -34,3 +37,34 @@ func TestJobPromoteCommand_Fails(t *testing.T) { } ui.ErrorWriter.Reset() } + +func TestJobPromoteCommand_AutocompleteArgs(t *testing.T) { + assert := assert.New(t) + t.Parallel() + + srv, _, url := testServer(t, true, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &JobPromoteCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + // Create a fake job + 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) +} diff --git a/command/job_revert.go b/command/job_revert.go index f60ff1404..904246056 100644 --- a/command/job_revert.go +++ b/command/job_revert.go @@ -3,6 +3,9 @@ package command import ( "fmt" "strings" + + "github.com/hashicorp/nomad/api/contexts" + "github.com/posener/complete" ) type JobRevertCommand struct { @@ -37,6 +40,25 @@ func (c *JobRevertCommand) Synopsis() string { return "Revert to a prior version of the job" } +func (c *JobRevertCommand) AutocompleteFlags() complete.Flags { + return nil +} + +func (c *JobRevertCommand) 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 *JobRevertCommand) Run(args []string) int { var detach, verbose bool diff --git a/command/job_revert_test.go b/command/job_revert_test.go index 5841582d1..98839fc71 100644 --- a/command/job_revert_test.go +++ b/command/job_revert_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 TestJobRevertCommand_Implements(t *testing.T) { @@ -34,3 +37,34 @@ func TestJobRevertCommand_Fails(t *testing.T) { } ui.ErrorWriter.Reset() } + +func TestJobRevertCommand_AutocompleteArgs(t *testing.T) { + assert := assert.New(t) + t.Parallel() + + srv, _, url := testServer(t, true, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &JobRevertCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + // Create a fake job + 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) +}