Skip to content

Commit

Permalink
nomad run -output emits the JSON representation of the job
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Mar 26, 2016
1 parent 9a5120c commit fb25340
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
19 changes: 18 additions & 1 deletion command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -50,6 +51,10 @@ Run Options:
-verbose
Display full information.
-output
Output the JSON that would be submitted to the HTTP API without submitting
the job.
`
return strings.TrimSpace(helpText)
}
Expand All @@ -59,12 +64,13 @@ func (c *RunCommand) Synopsis() string {
}

func (c *RunCommand) Run(args []string) int {
var detach, verbose bool
var detach, verbose, output bool

flags := c.Meta.FlagSet("run", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&detach, "detach", false, "")
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&output, "output", false, "")

if err := flags.Parse(args); err != nil {
return 1
Expand Down Expand Up @@ -110,6 +116,17 @@ func (c *RunCommand) Run(args []string) int {
return 1
}

if output {
buf, err := json.MarshalIndent(apiJob, "", " ")
if err != nil {
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
return 1
}

c.Ui.Output(string(buf))
return 0
}

// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions command/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ func TestRunCommand_Implements(t *testing.T) {
var _ cli.Command = &RunCommand{}
}

func TestRunCommand_Output_Json(t *testing.T) {
ui := new(cli.MockUi)
cmd := &RunCommand{Meta: Meta{Ui: ui}}

fh, err := ioutil.TempFile("", "nomad")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(fh.Name())
_, err = fh.WriteString(`
job "job1" {
type = "service"
datacenters = [ "dc1" ]
group "group1" {
count = 1
task "task1" {
driver = "exec"
resources = {
cpu = 1000
disk = 150
memory = 512
}
}
}
}`)
if err != nil {
t.Fatalf("err: %s", err)
}
if code := cmd.Run([]string{"-output", fh.Name()}); code != 0 {
t.Fatalf("expected exit code 0, got: %d", code)
}
if out := ui.OutputWriter.String(); !strings.Contains(out, `"Region": "global",`) {
t.Fatalf("Expected JSON output: %v", out)
}
}

func TestRunCommand_Fails(t *testing.T) {
ui := new(cli.MockUi)
cmd := &RunCommand{Meta: Meta{Ui: ui}}
Expand Down
3 changes: 3 additions & 0 deletions website/source/docs/commands/run.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ client connection issues or internal errors, are indicated by exit code 1.
will be output, which can be used to call the monitor later using the
[eval-monitor](/docs/commands/eval-monitor.html) command.

* `-output`: Output the JSON that would be submitted to the HTTP API without
submitting the job.

## Status Options

* `-verbose`: Show full information.
Expand Down

0 comments on commit fb25340

Please sign in to comment.