Skip to content

Commit

Permalink
Merge pull request #990 from hashicorp/f-hcl-to-json
Browse files Browse the repository at this point in the history
nomad run -output emits the JSON representation of the job
  • Loading branch information
dadgar committed Mar 26, 2016
2 parents e633776 + c9c1ef2 commit f94d027
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
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
4 changes: 3 additions & 1 deletion website/source/docs/jobspec/json.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ description: |-
# Job Specification

Jobs can be specified either in [HCL](https://github.com/hashicorp/hcl) or JSON.
This guide covers the json syntax for submitting jobs to Nomad.
This guide covers the json syntax for submitting jobs to Nomad. A useful command
for generating valid JSON versions of HCL jobs is `nomad run -output <job.nomad>`
which will emit a JSON version of the job.

## JSON Syntax

Expand Down

0 comments on commit f94d027

Please sign in to comment.