github.com/dkerwin/nomad@v0.3.3-0.20160525181927-74554135514b/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  	// Initialize any fields that need to be.
    52  	job.InitFields()
    53  
    54  	// Check that the job is valid
    55  	if err := job.Validate(); err != nil {
    56  		c.Ui.Error(fmt.Sprintf("Error validating job: %s", err))
    57  		return 1
    58  	}
    59  
    60  	// Done!
    61  	c.Ui.Output("Job validation successful")
    62  	return 0
    63  }