nomad run -output emits the JSON representation of the job

This commit is contained in:
Alex Dadgar 2016-03-25 18:24:51 -07:00
parent 7de1232722
commit f9a48e23f9
3 changed files with 57 additions and 1 deletions

View File

@ -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 {

View File

@ -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}}

View File

@ -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.