github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/command/validate.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/jobspec" 8 ) 9 10 type ValidateCommand struct { 11 Meta 12 } 13 14 func (c *ValidateCommand) Help() string { 15 helpText := ` 16 Usage: nomad validate [options] <file> 17 18 Checks if a given job file has a valid specification. This can be used to check 19 for any syntax errors or validation problems with a job. 20 21 ` 22 return strings.TrimSpace(helpText) 23 } 24 25 func (c *ValidateCommand) Synopsis() string { 26 return "Checks if a given job specification is valid" 27 } 28 29 func (c *ValidateCommand) Run(args []string) int { 30 flags := c.Meta.FlagSet("validate", FlagSetNone) 31 flags.Usage = func() { c.Ui.Output(c.Help()) } 32 if err := flags.Parse(args); err != nil { 33 return 1 34 } 35 36 // Check that we got exactly one node 37 args = flags.Args() 38 if len(args) != 1 { 39 c.Ui.Error(c.Help()) 40 return 1 41 } 42 file := args[0] 43 44 // Parse the job file 45 job, err := jobspec.ParseFile(file) 46 if err != nil { 47 c.Ui.Error(fmt.Sprintf("Error parsing job file %s: %s", file, err)) 48 return 1 49 } 50 51 // Check that the job is valid 52 if err := job.Validate(); err != nil { 53 c.Ui.Error(fmt.Sprintf("Error validating job: %s", err)) 54 return 1 55 } 56 57 // Done! 58 c.Ui.Output("Job validation successful") 59 return 0 60 }