-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #952 from hashicorp/f-nomad-inspect
cli: Add nomad inspect command
- Loading branch information
Showing
7 changed files
with
239 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type InspectCommand struct { | ||
Meta | ||
} | ||
|
||
func (c *InspectCommand) Help() string { | ||
helpText := ` | ||
Usage: nomad inspect [options] <job> | ||
Inspect is used to see the specification of a submitted job. | ||
General Options: | ||
` + generalOptionsUsage() | ||
|
||
return strings.TrimSpace(helpText) | ||
} | ||
|
||
func (c *InspectCommand) Synopsis() string { | ||
return "Inspect a submitted job" | ||
} | ||
|
||
func (c *InspectCommand) Run(args []string) int { | ||
flags := c.Meta.FlagSet("inspect", FlagSetClient) | ||
flags.Usage = func() { c.Ui.Output(c.Help()) } | ||
|
||
if err := flags.Parse(args); err != nil { | ||
return 1 | ||
} | ||
|
||
// Check that we got exactly one job | ||
args = flags.Args() | ||
if len(args) != 1 { | ||
c.Ui.Error(c.Help()) | ||
return 1 | ||
} | ||
jobID := args[0] | ||
|
||
// Get the HTTP client | ||
client, err := c.Meta.Client() | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) | ||
return 1 | ||
} | ||
|
||
// Check if the job exists | ||
jobs, _, err := client.Jobs().PrefixList(jobID) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err)) | ||
return 1 | ||
} | ||
if len(jobs) == 0 { | ||
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID)) | ||
return 1 | ||
} | ||
if len(jobs) > 1 { | ||
out := make([]string, len(jobs)+1) | ||
out[0] = "ID|Type|Priority|Status" | ||
for i, job := range jobs { | ||
out[i+1] = fmt.Sprintf("%s|%s|%d|%s", | ||
job.ID, | ||
job.Type, | ||
job.Priority, | ||
job.Status) | ||
} | ||
c.Ui.Output(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", formatList(out))) | ||
return 0 | ||
} | ||
|
||
// Prefix lookup matched a single job | ||
job, _, err := client.Jobs().RawJob(jobs[0].ID, nil) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error inspecting job: %s", err)) | ||
return 1 | ||
} | ||
|
||
// Print the contents of the job | ||
c.Ui.Output(job) | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package command | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/mitchellh/cli" | ||
) | ||
|
||
func TestInspectCommand_Implements(t *testing.T) { | ||
var _ cli.Command = &InspectCommand{} | ||
} | ||
|
||
func TestInspectCommand_Fails(t *testing.T) { | ||
srv, _, url := testServer(t, nil) | ||
defer srv.Stop() | ||
|
||
ui := new(cli.MockUi) | ||
cmd := &InspectCommand{Meta: Meta{Ui: ui}} | ||
|
||
// Fails on misuse | ||
if code := cmd.Run([]string{"some", "bad", "args"}); code != 1 { | ||
t.Fatalf("expected exit code 1, got: %d", code) | ||
} | ||
if out := ui.ErrorWriter.String(); !strings.Contains(out, cmd.Help()) { | ||
t.Fatalf("expected help output, got: %s", out) | ||
} | ||
ui.ErrorWriter.Reset() | ||
|
||
// Fails on non-existent job ID | ||
if code := cmd.Run([]string{"-address=" + url, "nope"}); code != 1 { | ||
t.Fatalf("expect exit 1, got: %d", code) | ||
} | ||
if out := ui.ErrorWriter.String(); !strings.Contains(out, "No job(s) with prefix or id") { | ||
t.Fatalf("expect not found error, got: %s", out) | ||
} | ||
ui.ErrorWriter.Reset() | ||
|
||
// Fails on connection failure | ||
if code := cmd.Run([]string{"-address=nope", "nope"}); code != 1 { | ||
t.Fatalf("expected exit code 1, got: %d", code) | ||
} | ||
if out := ui.ErrorWriter.String(); !strings.Contains(out, "Error inspecting job") { | ||
t.Fatalf("expected failed query error, got: %s", out) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters