From fb2534084ffaf5e7a0be3338ebc575adce43ec4f Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Fri, 25 Mar 2016 18:24:51 -0700 Subject: [PATCH 1/2] nomad run -output emits the JSON representation of the job --- command/run.go | 19 ++++++++++- command/run_test.go | 36 ++++++++++++++++++++ website/source/docs/commands/run.html.md.erb | 3 ++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/command/run.go b/command/run.go index 877496e10a17..c386b4417a6c 100644 --- a/command/run.go +++ b/command/run.go @@ -3,6 +3,7 @@ package command import ( "bytes" "encoding/gob" + "encoding/json" "fmt" "strings" "time" @@ -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) } @@ -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 @@ -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 { diff --git a/command/run_test.go b/command/run_test.go index efd92817eb2e..2bf09088b7d0 100644 --- a/command/run_test.go +++ b/command/run_test.go @@ -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}} diff --git a/website/source/docs/commands/run.html.md.erb b/website/source/docs/commands/run.html.md.erb index c5f709217175..6c1b6e4aae71 100644 --- a/website/source/docs/commands/run.html.md.erb +++ b/website/source/docs/commands/run.html.md.erb @@ -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. From c9c1ef26e200d4e2f2359b94ae30c45e92490007 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Fri, 25 Mar 2016 18:34:01 -0700 Subject: [PATCH 2/2] JSON page docs --- website/source/docs/jobspec/json.html.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/website/source/docs/jobspec/json.html.md b/website/source/docs/jobspec/json.html.md index c62a8d1ec216..70c596f7cbc1 100644 --- a/website/source/docs/jobspec/json.html.md +++ b/website/source/docs/jobspec/json.html.md @@ -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 ` +which will emit a JSON version of the job. ## JSON Syntax