flesh out the rest of the job subcommands

This commit is contained in:
Alex Dadgar 2017-08-22 13:39:06 -07:00
parent b41be1f85b
commit f79c14ac4c
10 changed files with 277 additions and 0 deletions

View file

@ -3,6 +3,9 @@ package command
import ( import (
"fmt" "fmt"
"strings" "strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
) )
type JobDeploymentsCommand struct { type JobDeploymentsCommand struct {
@ -40,6 +43,25 @@ func (c *JobDeploymentsCommand) Synopsis() string {
return "List deployments for a job" 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 { func (c *JobDeploymentsCommand) Run(args []string) int {
var json, latest, verbose bool var json, latest, verbose bool
var tmpl string var tmpl string

View file

@ -4,7 +4,10 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
) )
func TestJobDeploymentsCommand_Implements(t *testing.T) { func TestJobDeploymentsCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestJobDeploymentsCommand_Fails(t *testing.T) {
} }
ui.ErrorWriter.Reset() 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)
}

View file

@ -6,7 +6,9 @@ import (
"os" "os"
"strings" "strings"
"github.com/hashicorp/nomad/api/contexts"
flaghelper "github.com/hashicorp/nomad/helper/flag-helpers" flaghelper "github.com/hashicorp/nomad/helper/flag-helpers"
"github.com/posener/complete"
) )
type JobDispatchCommand struct { type JobDispatchCommand struct {
@ -54,6 +56,25 @@ func (c *JobDispatchCommand) Synopsis() string {
return "Dispatch an instance of a parameterized job" 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 { func (c *JobDispatchCommand) Run(args []string) int {
var detach, verbose bool var detach, verbose bool
var meta []string var meta []string

View file

@ -4,7 +4,10 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
) )
func TestJobDispatchCommand_Implements(t *testing.T) { func TestJobDispatchCommand_Implements(t *testing.T) {
@ -43,3 +46,34 @@ func TestJobDispatchCommand_Fails(t *testing.T) {
} }
ui.ErrorWriter.Reset() 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)
}

View file

@ -7,6 +7,8 @@ import (
"time" "time"
"github.com/hashicorp/nomad/api" "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
"github.com/ryanuber/columnize" "github.com/ryanuber/columnize"
) )
@ -52,6 +54,25 @@ func (c *JobHistoryCommand) Synopsis() string {
return "Display all tracked versions of a job" 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 { func (c *JobHistoryCommand) Run(args []string) int {
var json, diff, full bool var json, diff, full bool
var tmpl, versionStr string var tmpl, versionStr string

View file

@ -4,7 +4,10 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
) )
func TestJobHistoryCommand_Implements(t *testing.T) { func TestJobHistoryCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestJobHistoryCommand_Fails(t *testing.T) {
} }
ui.ErrorWriter.Reset() 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)
}

View file

@ -5,7 +5,9 @@ import (
"strings" "strings"
"github.com/hashicorp/nomad/api" "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
flaghelper "github.com/hashicorp/nomad/helper/flag-helpers" flaghelper "github.com/hashicorp/nomad/helper/flag-helpers"
"github.com/posener/complete"
) )
type JobPromoteCommand struct { type JobPromoteCommand struct {
@ -50,6 +52,25 @@ func (c *JobPromoteCommand) Synopsis() string {
return "Promote a job's canaries" 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 { func (c *JobPromoteCommand) Run(args []string) int {
var detach, verbose bool var detach, verbose bool
var groups []string var groups []string

View file

@ -4,7 +4,10 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
) )
func TestJobPromoteCommand_Implements(t *testing.T) { func TestJobPromoteCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestJobPromoteCommand_Fails(t *testing.T) {
} }
ui.ErrorWriter.Reset() 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)
}

View file

@ -3,6 +3,9 @@ package command
import ( import (
"fmt" "fmt"
"strings" "strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
) )
type JobRevertCommand struct { type JobRevertCommand struct {
@ -37,6 +40,25 @@ func (c *JobRevertCommand) Synopsis() string {
return "Revert to a prior version of the job" 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 { func (c *JobRevertCommand) Run(args []string) int {
var detach, verbose bool var detach, verbose bool

View file

@ -4,7 +4,10 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
) )
func TestJobRevertCommand_Implements(t *testing.T) { func TestJobRevertCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestJobRevertCommand_Fails(t *testing.T) {
} }
ui.ErrorWriter.Reset() 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)
}