logs and fs

This commit is contained in:
Alex Dadgar 2017-08-22 13:11:32 -07:00
parent 02f26f3384
commit 17e362e162
4 changed files with 114 additions and 2 deletions

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

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