github.com/mattyr/nomad@v0.3.3-0.20160919021406-3485a065154a/command/validate.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type ValidateCommand struct {
     9  	Meta
    10  	JobGetter
    11  }
    12  
    13  func (c *ValidateCommand) Help() string {
    14  	helpText := `
    15  Usage: nomad validate [options] <file>
    16  
    17    Checks if a given HCL job file has a valid specification. This can be used to
    18    check for any syntax errors or validation problems with a job.
    19  
    20    If the supplied path is "-", the jobfile is read from stdin. Otherwise
    21    it is read from the file at the supplied path or downloaded and
    22    read from URL specified.
    23  `
    24  	return strings.TrimSpace(helpText)
    25  }
    26  
    27  func (c *ValidateCommand) Synopsis() string {
    28  	return "Checks if a given job specification is valid"
    29  }
    30  
    31  func (c *ValidateCommand) Run(args []string) int {
    32  	flags := c.Meta.FlagSet("validate", FlagSetNone)
    33  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    34  	if err := flags.Parse(args); err != nil {
    35  		return 1
    36  	}
    37  
    38  	// Check that we got exactly one node
    39  	args = flags.Args()
    40  	if len(args) != 1 {
    41  		c.Ui.Error(c.Help())
    42  		return 1
    43  	}
    44  
    45  	// Get Job struct from Jobfile
    46  	job, err := c.JobGetter.StructJob(args[0])
    47  	if err != nil {
    48  		c.Ui.Error(fmt.Sprintf("Error getting job struct: %s", err))
    49  		return 1
    50  	}
    51  
    52  	// Initialize any fields that need to be.
    53  	job.Canonicalize()
    54  
    55  	// Check that the job is valid
    56  	if err := job.Validate(); err != nil {
    57  		c.Ui.Error(fmt.Sprintf("Error validating job: %s", err))
    58  		return 1
    59  	}
    60  
    61  	// Done!
    62  	c.Ui.Output("Job validation successful")
    63  	return 0
    64  }