Merge pull request #3076 from hashicorp/f-autocomplete-args

Autocomplete arguments for remaining commands
This commit is contained in:
Alex Dadgar 2017-08-22 14:04:22 -07:00 committed by GitHub
commit e05c45c4f5
38 changed files with 955 additions and 143 deletions

View File

@ -4,9 +4,10 @@ package contexts
type Context string
const (
Allocs Context = "allocs"
Evals Context = "evals"
Jobs Context = "jobs"
Nodes Context = "nodes"
All Context = ""
Allocs Context = "allocs"
Deployments Context = "deployment"
Evals Context = "evals"
Jobs Context = "jobs"
Nodes Context = "nodes"
All Context = ""
)

View File

@ -60,6 +60,26 @@ func (c *AllocStatusCommand) Synopsis() string {
return "Display allocation status information and metadata"
}
func (c *AllocStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
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{}
}
return resp.Matches[contexts.Allocs]
})
}
func (c *AllocStatusCommand) Run(args []string) int {
var short, displayStats, verbose, json bool
var tmpl string
@ -194,26 +214,6 @@ func (c *AllocStatusCommand) Run(args []string) int {
return 0
}
func (c *AllocStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
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{}
}
return resp.Matches[contexts.Allocs]
})
}
func formatAllocBasicInfo(alloc *api.Allocation, client *api.Client, uuidLength int, verbose bool) (string, error) {
basic := []string{
fmt.Sprintf("ID|%s", limit(alloc.ID, uuidLength)),

View File

@ -5,6 +5,7 @@ import (
"strings"
"testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/testutil"
"github.com/mitchellh/cli"
@ -149,40 +150,24 @@ func TestAllocStatusCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, client, url := testServer(t, true, nil)
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
jobID := "job1_sfx"
job1 := testJob(jobID)
resp, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}
// Create a fake alloc
state := srv.Agent.Server().State()
a := mock.Alloc()
assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}))
// get an alloc id
allocID := ""
if allocs, _, err := client.Jobs().Allocations(jobID, false, nil); err == nil {
if len(allocs) > 0 {
allocID = allocs[0].ID
}
}
if allocID == "" {
t.Fatal("unable to find an allocation")
}
prefix := allocID[:len(allocID)-5]
prefix := a.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(allocID, res[0])
assert.Equal(a.ID, res[0])
// Autocomplete should only complete once
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}

View File

@ -3,6 +3,9 @@ package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type DeploymentFailCommand struct {
@ -39,6 +42,25 @@ func (c *DeploymentFailCommand) Synopsis() string {
return "Manually fail a deployment"
}
func (c *DeploymentFailCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *DeploymentFailCommand) 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.Deployments)
if err != nil {
return []string{}
}
return resp.Matches[contexts.Deployments]
})
}
func (c *DeploymentFailCommand) Run(args []string) int {
var detach, verbose bool

View File

@ -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 TestDeploymentFailCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestDeploymentFailCommand_Fails(t *testing.T) {
}
ui.ErrorWriter.Reset()
}
func TestDeploymentFailCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &DeploymentFailCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a fake deployment
state := srv.Agent.Server().State()
d := mock.Deployment()
assert.Nil(state.UpsertDeployment(1000, d))
prefix := d.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(d.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 (
"fmt"
"strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type DeploymentPauseCommand struct {
@ -32,6 +35,25 @@ func (c *DeploymentPauseCommand) Synopsis() string {
return "Pause a deployment"
}
func (c *DeploymentPauseCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *DeploymentPauseCommand) 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.Deployments)
if err != nil {
return []string{}
}
return resp.Matches[contexts.Deployments]
})
}
func (c *DeploymentPauseCommand) Run(args []string) int {
var verbose bool

View File

@ -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 TestDeploymentPauseCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestDeploymentPauseCommand_Fails(t *testing.T) {
}
ui.ErrorWriter.Reset()
}
func TestDeploymentPauseCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &DeploymentPauseCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a fake deployment
state := srv.Agent.Server().State()
d := mock.Deployment()
assert.Nil(state.UpsertDeployment(1000, d))
prefix := d.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(d.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"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
flaghelper "github.com/hashicorp/nomad/helper/flag-helpers"
"github.com/posener/complete"
)
type DeploymentPromoteCommand struct {
@ -49,6 +51,25 @@ func (c *DeploymentPromoteCommand) Synopsis() string {
return "Promote canaries in a deployment"
}
func (c *DeploymentPromoteCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *DeploymentPromoteCommand) 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.Deployments)
if err != nil {
return []string{}
}
return resp.Matches[contexts.Deployments]
})
}
func (c *DeploymentPromoteCommand) Run(args []string) int {
var detach, verbose bool
var groups []string

View File

@ -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 TestDeploymentPromoteCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestDeploymentPromoteCommand_Fails(t *testing.T) {
}
ui.ErrorWriter.Reset()
}
func TestDeploymentPromoteCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &DeploymentPromoteCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a fake deployment
state := srv.Agent.Server().State()
d := mock.Deployment()
assert.Nil(state.UpsertDeployment(1000, d))
prefix := d.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(d.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 (
"fmt"
"strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type DeploymentResumeCommand struct {
@ -37,6 +40,25 @@ func (c *DeploymentResumeCommand) Synopsis() string {
return "Resume a paused deployment"
}
func (c *DeploymentResumeCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *DeploymentResumeCommand) 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.Deployments)
if err != nil {
return []string{}
}
return resp.Matches[contexts.Deployments]
})
}
func (c *DeploymentResumeCommand) Run(args []string) int {
var detach, verbose bool

View File

@ -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 TestDeploymentResumeCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestDeploymentResumeCommand_Fails(t *testing.T) {
}
ui.ErrorWriter.Reset()
}
func TestDeploymentResumeCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &DeploymentResumeCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a fake deployment
state := srv.Agent.Server().State()
d := mock.Deployment()
assert.Nil(state.UpsertDeployment(1000, d))
prefix := d.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(d.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,6 +5,8 @@ import (
"strings"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type DeploymentStatusCommand struct {
@ -40,6 +42,25 @@ func (c *DeploymentStatusCommand) Synopsis() string {
return "Display the status of a deployment"
}
func (c *DeploymentStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *DeploymentStatusCommand) 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.Deployments)
if err != nil {
return []string{}
}
return resp.Matches[contexts.Deployments]
})
}
func (c *DeploymentStatusCommand) Run(args []string) int {
var json, verbose bool
var tmpl string

View File

@ -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 TestDeploymentStatusCommand_Implements(t *testing.T) {
@ -34,3 +37,34 @@ func TestDeploymentStatusCommand_Fails(t *testing.T) {
}
ui.ErrorWriter.Reset()
}
func TestDeploymentStatusCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &DeploymentStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a fake deployment
state := srv.Agent.Server().State()
d := mock.Deployment()
assert.Nil(state.UpsertDeployment(1000, d))
prefix := d.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(d.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

@ -48,6 +48,25 @@ func (c *EvalStatusCommand) Synopsis() string {
return "Display evaluation status and placement failure reasons"
}
func (c *EvalStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
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{}
}
return resp.Matches[contexts.Evals]
})
}
func (c *EvalStatusCommand) Run(args []string) int {
var monitor, verbose, json bool
var tmpl string
@ -220,25 +239,6 @@ func (c *EvalStatusCommand) Run(args []string) int {
return 0
}
func (c *EvalStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
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{}
}
return resp.Matches[contexts.Evals]
})
}
func sortedTaskGroupFromMetrics(groups map[string]*api.AllocationMetric) []string {
tgs := make([]string, 0, len(groups))
for tg, _ := range groups {

View File

@ -4,6 +4,8 @@ import (
"strings"
"testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
@ -63,40 +65,24 @@ func TestEvalStatusCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, client, url := testServer(t, true, nil)
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &EvalStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
jobID := "job1_sfx"
job1 := testJob(jobID)
resp, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}
// Create a fake eval
state := srv.Agent.Server().State()
e := mock.Eval()
assert.Nil(state.UpsertEvals(1000, []*structs.Evaluation{e}))
// get an eval id
evalID := ""
if evals, _, err := client.Jobs().Evaluations(jobID, nil); err == nil {
if len(evals) > 0 {
evalID = evals[0].ID
}
}
if evalID == "" {
t.Fatal("unable to find an evaluation")
}
prefix := evalID[:len(evalID)-5]
prefix := e.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(evalID, res[0])
assert.Equal(e.ID, res[0])
// Autocomplete should only complete once
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}

View File

@ -12,6 +12,8 @@ import (
humanize "github.com/dustin/go-humanize"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
const (
@ -77,6 +79,26 @@ func (f *FSCommand) Synopsis() string {
return "Inspect the contents of an allocation directory"
}
func (f *FSCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (f *FSCommand) AutocompleteArgs() complete.Predictor {
client, _ := f.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{}
}
return resp.Matches[contexts.Allocs]
})
}
func (f *FSCommand) Run(args []string) int {
var verbose, machine, job, stat, tail, follow bool
var numLines, numBytes int64

View File

@ -4,7 +4,11 @@ import (
"strings"
"testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
)
func TestFSCommand_Implements(t *testing.T) {
@ -81,5 +85,35 @@ func TestFSCommand_Fails(t *testing.T) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
t.Fatalf("expected not found error, got: %s", out)
}
}
func TestFSCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &FSCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a fake alloc
state := srv.Agent.Server().State()
a := mock.Alloc()
assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}))
prefix := a.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(a.ID, 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)
}

View File

@ -5,6 +5,8 @@ import (
"strings"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type InspectCommand struct {
@ -39,6 +41,25 @@ func (c *InspectCommand) Synopsis() string {
return "Inspect a submitted job"
}
func (c *InspectCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *InspectCommand) 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 *InspectCommand) Run(args []string) int {
var json bool
var tmpl, versionStr string

View File

@ -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 TestInspectCommand_Implements(t *testing.T) {
@ -55,3 +58,33 @@ func TestInspectCommand_Fails(t *testing.T) {
t.Fatalf("expected getting formatter error, got: %s", out)
}
}
func TestInspectCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &InspectCommand{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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -60,6 +60,25 @@ func (c *JobStatusCommand) Synopsis() string {
return "Display status information about jobs"
}
func (c *JobStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
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{}
}
return resp.Matches[contexts.Jobs]
})
}
func (c *JobStatusCommand) Run(args []string) int {
var short bool
@ -527,25 +546,6 @@ func (c *JobStatusCommand) outputFailedPlacements(failedEval *api.Evaluation) {
}
}
func (c *JobStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
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{}
}
return resp.Matches[contexts.Jobs]
})
}
// list general information about a list of jobs
func createStatusListOutput(jobs []*api.JobListStub) string {
out := make([]string, len(jobs)+1)

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
@ -194,33 +195,27 @@ func TestJobStatusCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, client, url := testServer(t, true, nil)
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &JobStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
jobID := "job1_sfx"
job1 := testJob(jobID)
resp, _, err := client.Jobs().Register(job1, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if code := waitForSuccess(ui, client, fullId, t, resp.EvalID); code != 0 {
t.Fatalf("status code non zero saw %d", code)
}
// Create a fake job
state := srv.Agent.Server().State()
j := mock.Job()
assert.Nil(state.UpsertJob(1000, j))
prefix := jobID[:len(jobID)-5]
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(jobID, res[0])
assert.Equal(j.ID, 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)

View File

@ -10,6 +10,8 @@ import (
"time"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type LogsCommand struct {
@ -59,6 +61,26 @@ func (l *LogsCommand) Synopsis() string {
return "Streams the logs of a task."
}
func (l *LogsCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (l *LogsCommand) AutocompleteArgs() complete.Predictor {
client, _ := l.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{}
}
return resp.Matches[contexts.Allocs]
})
}
func (l *LogsCommand) Run(args []string) int {
var verbose, job, tail, stderr, follow bool
var numLines, numBytes int64

View File

@ -4,7 +4,11 @@ import (
"strings"
"testing"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
)
func TestLogsCommand_Implements(t *testing.T) {
@ -63,5 +67,35 @@ func TestLogsCommand_Fails(t *testing.T) {
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
t.Fatalf("expected not found error, got: %s", out)
}
}
func TestLogsCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, _, url := testServer(t, true, nil)
defer srv.Shutdown()
ui := new(cli.MockUi)
cmd := &LogsCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a fake alloc
state := srv.Agent.Server().State()
a := mock.Alloc()
assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}))
prefix := a.ID[:5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
res := predictor.Predict(args)
assert.Equal(1, len(res))
assert.Equal(a.ID, 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)
}

View File

@ -3,6 +3,9 @@ package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type NodeDrainCommand struct {
@ -42,6 +45,25 @@ func (c *NodeDrainCommand) Synopsis() string {
return "Toggle drain mode on a given node"
}
func (c *NodeDrainCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *NodeDrainCommand) 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{}
}
return resp.Matches[contexts.Nodes]
})
}
func (c *NodeDrainCommand) Run(args []string) int {
var enable, disable, self, autoYes bool

View File

@ -1,10 +1,14 @@
package command
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/nomad/testutil"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"github.com/stretchr/testify/assert"
)
func TestNodeDrainCommand_Implements(t *testing.T) {
@ -82,3 +86,45 @@ func TestNodeDrainCommand_Fails(t *testing.T) {
t.Fatalf("expected not exist error, got: %s", out)
}
}
func TestNodeDrainCommand_AutocompleteArgs(t *testing.T) {
assert := assert.New(t)
t.Parallel()
srv, client, url := testServer(t, true, nil)
defer srv.Shutdown()
// Wait for a node to appear
var nodeID string
testutil.WaitForResult(func() (bool, error) {
nodes, _, err := client.Nodes().List(nil)
if err != nil {
return false, err
}
if len(nodes) == 0 {
return false, fmt.Errorf("missing node")
}
nodeID = nodes[0].ID
return true, nil
}, func(err error) {
t.Fatalf("err: %s", err)
})
ui := new(cli.MockUi)
cmd := &NodeDrainCommand{Meta: Meta{Ui: ui, flagAddress: url}}
prefix := nodeID[:len(nodeID)-5]
args := complete.Args{Last: prefix}
predictor := cmd.AutocompleteArgs()
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)
}

View File

@ -85,6 +85,25 @@ func (c *NodeStatusCommand) Synopsis() string {
return "Display status information about nodes"
}
func (c *NodeStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
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{}
}
return resp.Matches[contexts.Nodes]
})
}
func (c *NodeStatusCommand) Run(args []string) int {
flags := c.Meta.FlagSet("node-status", FlagSetClient)
@ -443,25 +462,6 @@ func (c *NodeStatusCommand) printDiskStats(hostStats *api.HostStats) {
}
}
func (c *NodeStatusCommand) AutocompleteFlags() complete.Flags {
return nil
}
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{}
}
return resp.Matches[contexts.Nodes]
})
}
// getRunningAllocs returns a slice of allocation id's running on the node
func getRunningAllocs(client *api.Client, nodeID string) ([]*api.Allocation, error) {
var allocs []*api.Allocation

View File

@ -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

View File

@ -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,34 @@ 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}}
// 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)
}