Skip to content

Commit f94d027

Browse files
committed
Merge pull request #990 from hashicorp/f-hcl-to-json
nomad run -output emits the JSON representation of the job
2 parents e633776 + c9c1ef2 commit f94d027

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

command/run.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
import (
44
"bytes"
55
"encoding/gob"
6+
"encoding/json"
67
"fmt"
78
"strings"
89
"time"
@@ -50,6 +51,10 @@ Run Options:
5051
5152
-verbose
5253
Display full information.
54+
55+
-output
56+
Output the JSON that would be submitted to the HTTP API without submitting
57+
the job.
5358
`
5459
return strings.TrimSpace(helpText)
5560
}
@@ -59,12 +64,13 @@ func (c *RunCommand) Synopsis() string {
5964
}
6065

6166
func (c *RunCommand) Run(args []string) int {
62-
var detach, verbose bool
67+
var detach, verbose, output bool
6368

6469
flags := c.Meta.FlagSet("run", FlagSetClient)
6570
flags.Usage = func() { c.Ui.Output(c.Help()) }
6671
flags.BoolVar(&detach, "detach", false, "")
6772
flags.BoolVar(&verbose, "verbose", false, "")
73+
flags.BoolVar(&output, "output", false, "")
6874

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

119+
if output {
120+
buf, err := json.MarshalIndent(apiJob, "", " ")
121+
if err != nil {
122+
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
123+
return 1
124+
}
125+
126+
c.Ui.Output(string(buf))
127+
return 0
128+
}
129+
113130
// Get the HTTP client
114131
client, err := c.Meta.Client()
115132
if err != nil {

command/run_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,42 @@ func TestRunCommand_Implements(t *testing.T) {
1313
var _ cli.Command = &RunCommand{}
1414
}
1515

16+
func TestRunCommand_Output_Json(t *testing.T) {
17+
ui := new(cli.MockUi)
18+
cmd := &RunCommand{Meta: Meta{Ui: ui}}
19+
20+
fh, err := ioutil.TempFile("", "nomad")
21+
if err != nil {
22+
t.Fatalf("err: %s", err)
23+
}
24+
defer os.Remove(fh.Name())
25+
_, err = fh.WriteString(`
26+
job "job1" {
27+
type = "service"
28+
datacenters = [ "dc1" ]
29+
group "group1" {
30+
count = 1
31+
task "task1" {
32+
driver = "exec"
33+
resources = {
34+
cpu = 1000
35+
disk = 150
36+
memory = 512
37+
}
38+
}
39+
}
40+
}`)
41+
if err != nil {
42+
t.Fatalf("err: %s", err)
43+
}
44+
if code := cmd.Run([]string{"-output", fh.Name()}); code != 0 {
45+
t.Fatalf("expected exit code 0, got: %d", code)
46+
}
47+
if out := ui.OutputWriter.String(); !strings.Contains(out, `"Region": "global",`) {
48+
t.Fatalf("Expected JSON output: %v", out)
49+
}
50+
}
51+
1652
func TestRunCommand_Fails(t *testing.T) {
1753
ui := new(cli.MockUi)
1854
cmd := &RunCommand{Meta: Meta{Ui: ui}}

website/source/docs/commands/run.html.md.erb

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ client connection issues or internal errors, are indicated by exit code 1.
4242
will be output, which can be used to call the monitor later using the
4343
[eval-monitor](/docs/commands/eval-monitor.html) command.
4444

45+
* `-output`: Output the JSON that would be submitted to the HTTP API without
46+
submitting the job.
47+
4548
## Status Options
4649

4750
* `-verbose`: Show full information.

website/source/docs/jobspec/json.html.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ description: |-
99
# Job Specification
1010

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

1416
## JSON Syntax
1517

0 commit comments

Comments
 (0)