Merge pull request #3076 from hashicorp/f-autocomplete-args
Autocomplete arguments for remaining commands
This commit is contained in:
commit
e05c45c4f5
|
@ -5,6 +5,7 @@ type Context string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Allocs Context = "allocs"
|
Allocs Context = "allocs"
|
||||||
|
Deployments Context = "deployment"
|
||||||
Evals Context = "evals"
|
Evals Context = "evals"
|
||||||
Jobs Context = "jobs"
|
Jobs Context = "jobs"
|
||||||
Nodes Context = "nodes"
|
Nodes Context = "nodes"
|
||||||
|
|
|
@ -60,6 +60,26 @@ func (c *AllocStatusCommand) Synopsis() string {
|
||||||
return "Display allocation status information and metadata"
|
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 {
|
func (c *AllocStatusCommand) Run(args []string) int {
|
||||||
var short, displayStats, verbose, json bool
|
var short, displayStats, verbose, json bool
|
||||||
var tmpl string
|
var tmpl string
|
||||||
|
@ -194,26 +214,6 @@ func (c *AllocStatusCommand) Run(args []string) int {
|
||||||
return 0
|
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) {
|
func formatAllocBasicInfo(alloc *api.Allocation, client *api.Client, uuidLength int, verbose bool) (string, error) {
|
||||||
basic := []string{
|
basic := []string{
|
||||||
fmt.Sprintf("ID|%s", limit(alloc.ID, uuidLength)),
|
fmt.Sprintf("ID|%s", limit(alloc.ID, uuidLength)),
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/nomad/mock"
|
||||||
"github.com/hashicorp/nomad/nomad/structs"
|
"github.com/hashicorp/nomad/nomad/structs"
|
||||||
"github.com/hashicorp/nomad/testutil"
|
"github.com/hashicorp/nomad/testutil"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
|
@ -149,40 +150,24 @@ func TestAllocStatusCommand_AutocompleteArgs(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
srv, client, url := testServer(t, true, nil)
|
srv, _, url := testServer(t, true, nil)
|
||||||
defer srv.Shutdown()
|
defer srv.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui := new(cli.MockUi)
|
||||||
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
cmd := &AllocStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
||||||
|
|
||||||
jobID := "job1_sfx"
|
// Create a fake alloc
|
||||||
job1 := testJob(jobID)
|
state := srv.Agent.Server().State()
|
||||||
resp, _, err := client.Jobs().Register(job1, nil)
|
a := mock.Alloc()
|
||||||
if err != nil {
|
assert.Nil(state.UpsertAllocs(1000, []*structs.Allocation{a}))
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// get an alloc id
|
prefix := a.ID[:5]
|
||||||
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]
|
|
||||||
args := complete.Args{Last: prefix}
|
args := complete.Args{Last: prefix}
|
||||||
predictor := cmd.AutocompleteArgs()
|
predictor := cmd.AutocompleteArgs()
|
||||||
|
|
||||||
res := predictor.Predict(args)
|
res := predictor.Predict(args)
|
||||||
assert.Equal(1, len(res))
|
assert.Equal(1, len(res))
|
||||||
assert.Equal(allocID, res[0])
|
assert.Equal(a.ID, res[0])
|
||||||
|
|
||||||
// Autocomplete should only complete once
|
// Autocomplete should only complete once
|
||||||
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}
|
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}
|
||||||
|
|
|
@ -3,6 +3,9 @@ package command
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeploymentFailCommand struct {
|
type DeploymentFailCommand struct {
|
||||||
|
@ -39,6 +42,25 @@ func (c *DeploymentFailCommand) Synopsis() string {
|
||||||
return "Manually fail a deployment"
|
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 {
|
func (c *DeploymentFailCommand) Run(args []string) int {
|
||||||
var detach, verbose bool
|
var detach, verbose bool
|
||||||
|
|
||||||
|
|
|
@ -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 TestDeploymentFailCommand_Implements(t *testing.T) {
|
func TestDeploymentFailCommand_Implements(t *testing.T) {
|
||||||
|
@ -34,3 +37,34 @@ func TestDeploymentFailCommand_Fails(t *testing.T) {
|
||||||
}
|
}
|
||||||
ui.ErrorWriter.Reset()
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,9 @@ package command
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeploymentPauseCommand struct {
|
type DeploymentPauseCommand struct {
|
||||||
|
@ -32,6 +35,25 @@ func (c *DeploymentPauseCommand) Synopsis() string {
|
||||||
return "Pause a deployment"
|
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 {
|
func (c *DeploymentPauseCommand) Run(args []string) int {
|
||||||
var verbose bool
|
var verbose bool
|
||||||
|
|
||||||
|
|
|
@ -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 TestDeploymentPauseCommand_Implements(t *testing.T) {
|
func TestDeploymentPauseCommand_Implements(t *testing.T) {
|
||||||
|
@ -34,3 +37,34 @@ func TestDeploymentPauseCommand_Fails(t *testing.T) {
|
||||||
}
|
}
|
||||||
ui.ErrorWriter.Reset()
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -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 DeploymentPromoteCommand struct {
|
type DeploymentPromoteCommand struct {
|
||||||
|
@ -49,6 +51,25 @@ func (c *DeploymentPromoteCommand) Synopsis() string {
|
||||||
return "Promote canaries in a deployment"
|
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 {
|
func (c *DeploymentPromoteCommand) Run(args []string) int {
|
||||||
var detach, verbose bool
|
var detach, verbose bool
|
||||||
var groups []string
|
var groups []string
|
||||||
|
|
|
@ -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 TestDeploymentPromoteCommand_Implements(t *testing.T) {
|
func TestDeploymentPromoteCommand_Implements(t *testing.T) {
|
||||||
|
@ -34,3 +37,34 @@ func TestDeploymentPromoteCommand_Fails(t *testing.T) {
|
||||||
}
|
}
|
||||||
ui.ErrorWriter.Reset()
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,9 @@ package command
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeploymentResumeCommand struct {
|
type DeploymentResumeCommand struct {
|
||||||
|
@ -37,6 +40,25 @@ func (c *DeploymentResumeCommand) Synopsis() string {
|
||||||
return "Resume a paused deployment"
|
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 {
|
func (c *DeploymentResumeCommand) Run(args []string) int {
|
||||||
var detach, verbose bool
|
var detach, verbose bool
|
||||||
|
|
||||||
|
|
|
@ -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 TestDeploymentResumeCommand_Implements(t *testing.T) {
|
func TestDeploymentResumeCommand_Implements(t *testing.T) {
|
||||||
|
@ -34,3 +37,34 @@ func TestDeploymentResumeCommand_Fails(t *testing.T) {
|
||||||
}
|
}
|
||||||
ui.ErrorWriter.Reset()
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/nomad/api"
|
"github.com/hashicorp/nomad/api"
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DeploymentStatusCommand struct {
|
type DeploymentStatusCommand struct {
|
||||||
|
@ -40,6 +42,25 @@ func (c *DeploymentStatusCommand) Synopsis() string {
|
||||||
return "Display the status of a deployment"
|
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 {
|
func (c *DeploymentStatusCommand) Run(args []string) int {
|
||||||
var json, verbose bool
|
var json, verbose bool
|
||||||
var tmpl string
|
var tmpl string
|
||||||
|
|
|
@ -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 TestDeploymentStatusCommand_Implements(t *testing.T) {
|
func TestDeploymentStatusCommand_Implements(t *testing.T) {
|
||||||
|
@ -34,3 +37,34 @@ func TestDeploymentStatusCommand_Fails(t *testing.T) {
|
||||||
}
|
}
|
||||||
ui.ErrorWriter.Reset()
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -48,6 +48,25 @@ func (c *EvalStatusCommand) Synopsis() string {
|
||||||
return "Display evaluation status and placement failure reasons"
|
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 {
|
func (c *EvalStatusCommand) Run(args []string) int {
|
||||||
var monitor, verbose, json bool
|
var monitor, verbose, json bool
|
||||||
var tmpl string
|
var tmpl string
|
||||||
|
@ -220,25 +239,6 @@ func (c *EvalStatusCommand) Run(args []string) int {
|
||||||
return 0
|
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 {
|
func sortedTaskGroupFromMetrics(groups map[string]*api.AllocationMetric) []string {
|
||||||
tgs := make([]string, 0, len(groups))
|
tgs := make([]string, 0, len(groups))
|
||||||
for tg, _ := range groups {
|
for tg, _ := range groups {
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/nomad/mock"
|
||||||
|
"github.com/hashicorp/nomad/nomad/structs"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"github.com/posener/complete"
|
"github.com/posener/complete"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -63,40 +65,24 @@ func TestEvalStatusCommand_AutocompleteArgs(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
srv, client, url := testServer(t, true, nil)
|
srv, _, url := testServer(t, true, nil)
|
||||||
defer srv.Shutdown()
|
defer srv.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui := new(cli.MockUi)
|
||||||
cmd := &EvalStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
cmd := &EvalStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
||||||
|
|
||||||
jobID := "job1_sfx"
|
// Create a fake eval
|
||||||
job1 := testJob(jobID)
|
state := srv.Agent.Server().State()
|
||||||
resp, _, err := client.Jobs().Register(job1, nil)
|
e := mock.Eval()
|
||||||
if err != nil {
|
assert.Nil(state.UpsertEvals(1000, []*structs.Evaluation{e}))
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// get an eval id
|
prefix := e.ID[:5]
|
||||||
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]
|
|
||||||
args := complete.Args{Last: prefix}
|
args := complete.Args{Last: prefix}
|
||||||
predictor := cmd.AutocompleteArgs()
|
predictor := cmd.AutocompleteArgs()
|
||||||
|
|
||||||
res := predictor.Predict(args)
|
res := predictor.Predict(args)
|
||||||
assert.Equal(1, len(res))
|
assert.Equal(1, len(res))
|
||||||
assert.Equal(evalID, res[0])
|
assert.Equal(e.ID, res[0])
|
||||||
|
|
||||||
// Autocomplete should only complete once
|
// Autocomplete should only complete once
|
||||||
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}
|
args = complete.Args{Last: prefix, Completed: []string{prefix, "1", "2"}}
|
||||||
|
|
|
@ -12,6 +12,8 @@ import (
|
||||||
|
|
||||||
humanize "github.com/dustin/go-humanize"
|
humanize "github.com/dustin/go-humanize"
|
||||||
"github.com/hashicorp/nomad/api"
|
"github.com/hashicorp/nomad/api"
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -77,6 +79,26 @@ func (f *FSCommand) Synopsis() string {
|
||||||
return "Inspect the contents of an allocation directory"
|
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 {
|
func (f *FSCommand) Run(args []string) int {
|
||||||
var verbose, machine, job, stat, tail, follow bool
|
var verbose, machine, job, stat, tail, follow bool
|
||||||
var numLines, numBytes int64
|
var numLines, numBytes int64
|
||||||
|
|
|
@ -4,7 +4,11 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/nomad/mock"
|
||||||
|
"github.com/hashicorp/nomad/nomad/structs"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
|
"github.com/posener/complete"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFSCommand_Implements(t *testing.T) {
|
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") {
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
|
||||||
t.Fatalf("expected not found error, got: %s", out)
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/nomad/api"
|
"github.com/hashicorp/nomad/api"
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InspectCommand struct {
|
type InspectCommand struct {
|
||||||
|
@ -39,6 +41,25 @@ func (c *InspectCommand) Synopsis() string {
|
||||||
return "Inspect a submitted job"
|
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 {
|
func (c *InspectCommand) Run(args []string) int {
|
||||||
var json bool
|
var json bool
|
||||||
var tmpl, versionStr string
|
var tmpl, versionStr string
|
||||||
|
|
|
@ -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 TestInspectCommand_Implements(t *testing.T) {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
@ -60,6 +60,25 @@ func (c *JobStatusCommand) Synopsis() string {
|
||||||
return "Display status information about jobs"
|
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 {
|
func (c *JobStatusCommand) Run(args []string) int {
|
||||||
var short bool
|
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
|
// list general information about a list of jobs
|
||||||
func createStatusListOutput(jobs []*api.JobListStub) string {
|
func createStatusListOutput(jobs []*api.JobListStub) string {
|
||||||
out := make([]string, len(jobs)+1)
|
out := make([]string, len(jobs)+1)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/nomad/api"
|
"github.com/hashicorp/nomad/api"
|
||||||
|
"github.com/hashicorp/nomad/nomad/mock"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"github.com/posener/complete"
|
"github.com/posener/complete"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -194,33 +195,27 @@ func TestJobStatusCommand_AutocompleteArgs(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
srv, client, url := testServer(t, true, nil)
|
srv, _, url := testServer(t, true, nil)
|
||||||
defer srv.Shutdown()
|
defer srv.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui := new(cli.MockUi)
|
||||||
cmd := &JobStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
cmd := &JobStatusCommand{Meta: Meta{Ui: ui, flagAddress: url}}
|
||||||
|
|
||||||
jobID := "job1_sfx"
|
// Create a fake job
|
||||||
job1 := testJob(jobID)
|
state := srv.Agent.Server().State()
|
||||||
resp, _, err := client.Jobs().Register(job1, nil)
|
j := mock.Job()
|
||||||
if err != nil {
|
assert.Nil(state.UpsertJob(1000, j))
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
prefix := jobID[:len(jobID)-5]
|
prefix := j.ID[:len(j.ID)-5]
|
||||||
args := complete.Args{Last: prefix}
|
args := complete.Args{Last: prefix}
|
||||||
predictor := cmd.AutocompleteArgs()
|
predictor := cmd.AutocompleteArgs()
|
||||||
|
|
||||||
res := predictor.Predict(args)
|
res := predictor.Predict(args)
|
||||||
assert.Equal(1, len(res))
|
assert.Equal(1, len(res))
|
||||||
assert.Equal(jobID, res[0])
|
assert.Equal(j.ID, res[0])
|
||||||
|
|
||||||
// Autocomplete should only complete once
|
// Autocomplete should only complete once
|
||||||
args = complete.Args{Last: prefix, Completed: []string{prefix, "a", "b"}}
|
args = complete.Args{Last: prefix, Completed: []string{prefix, "a", "b"}}
|
||||||
prefix = jobID[:len(jobID)-5]
|
|
||||||
predictor = cmd.AutocompleteArgs()
|
predictor = cmd.AutocompleteArgs()
|
||||||
|
|
||||||
res = predictor.Predict(args)
|
res = predictor.Predict(args)
|
||||||
|
|
|
@ -10,6 +10,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/nomad/api"
|
"github.com/hashicorp/nomad/api"
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogsCommand struct {
|
type LogsCommand struct {
|
||||||
|
@ -59,6 +61,26 @@ func (l *LogsCommand) Synopsis() string {
|
||||||
return "Streams the logs of a task."
|
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 {
|
func (l *LogsCommand) Run(args []string) int {
|
||||||
var verbose, job, tail, stderr, follow bool
|
var verbose, job, tail, stderr, follow bool
|
||||||
var numLines, numBytes int64
|
var numLines, numBytes int64
|
||||||
|
|
|
@ -4,7 +4,11 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/nomad/mock"
|
||||||
|
"github.com/hashicorp/nomad/nomad/structs"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
|
"github.com/posener/complete"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogsCommand_Implements(t *testing.T) {
|
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") {
|
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No allocation(s) with prefix or id") {
|
||||||
t.Fatalf("expected not found error, got: %s", out)
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,9 @@ package command
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NodeDrainCommand struct {
|
type NodeDrainCommand struct {
|
||||||
|
@ -42,6 +45,25 @@ func (c *NodeDrainCommand) Synopsis() string {
|
||||||
return "Toggle drain mode on a given node"
|
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 {
|
func (c *NodeDrainCommand) Run(args []string) int {
|
||||||
var enable, disable, self, autoYes bool
|
var enable, disable, self, autoYes bool
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/testutil"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
|
"github.com/posener/complete"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNodeDrainCommand_Implements(t *testing.T) {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -85,6 +85,25 @@ func (c *NodeStatusCommand) Synopsis() string {
|
||||||
return "Display status information about nodes"
|
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 {
|
func (c *NodeStatusCommand) Run(args []string) int {
|
||||||
|
|
||||||
flags := c.Meta.FlagSet("node-status", FlagSetClient)
|
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
|
// getRunningAllocs returns a slice of allocation id's running on the node
|
||||||
func getRunningAllocs(client *api.Client, nodeID string) ([]*api.Allocation, error) {
|
func getRunningAllocs(client *api.Client, nodeID string) ([]*api.Allocation, error) {
|
||||||
var allocs []*api.Allocation
|
var allocs []*api.Allocation
|
||||||
|
|
|
@ -3,6 +3,9 @@ package command
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hashicorp/nomad/api/contexts"
|
||||||
|
"github.com/posener/complete"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StopCommand struct {
|
type StopCommand struct {
|
||||||
|
@ -48,6 +51,25 @@ func (c *StopCommand) Synopsis() string {
|
||||||
return "Stop a running job"
|
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 {
|
func (c *StopCommand) Run(args []string) int {
|
||||||
var detach, purge, verbose, autoYes bool
|
var detach, purge, verbose, autoYes bool
|
||||||
|
|
||||||
|
|
|
@ -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 TestStopCommand_Implements(t *testing.T) {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue