From fcacdd5c73b93358d67d361db13ee4cec4ecad89 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Tue, 22 Aug 2017 13:19:29 -0700 Subject: [PATCH] inspect --- command/inspect.go | 21 +++++++++++++++++++++ command/inspect_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/command/inspect.go b/command/inspect.go index 382d98222..6f17b79b6 100644 --- a/command/inspect.go +++ b/command/inspect.go @@ -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 diff --git a/command/inspect_test.go b/command/inspect_test.go index 12eb9f004..9861fccc3 100644 --- a/command/inspect_test.go +++ b/command/inspect_test.go @@ -4,7 +4,10 @@ import ( "strings" "testing" + "github.com/hashicorp/nomad/nomad/mock" "github.com/mitchellh/cli" + "github.com/posener/complete" + "github.com/stretchr/testify/assert" ) func 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 := &JobStatusCommand{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) +}