github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/command/validate/command.go (about)

     1  package validate
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	cmdcommon "github.com/mitchellh/packer/common/command"
     7  	"github.com/mitchellh/packer/packer"
     8  	"log"
     9  	"strings"
    10  )
    11  
    12  type Command byte
    13  
    14  func (Command) Help() string {
    15  	return strings.TrimSpace(helpString)
    16  }
    17  
    18  func (c Command) Run(env packer.Environment, args []string) int {
    19  	var cfgSyntaxOnly bool
    20  	buildOptions := new(cmdcommon.BuildOptions)
    21  
    22  	cmdFlags := flag.NewFlagSet("validate", flag.ContinueOnError)
    23  	cmdFlags.Usage = func() { env.Ui().Say(c.Help()) }
    24  	cmdFlags.BoolVar(&cfgSyntaxOnly, "syntax-only", false, "check syntax only")
    25  	cmdcommon.BuildOptionFlags(cmdFlags, buildOptions)
    26  	if err := cmdFlags.Parse(args); err != nil {
    27  		return 1
    28  	}
    29  
    30  	args = cmdFlags.Args()
    31  	if len(args) != 1 {
    32  		cmdFlags.Usage()
    33  		return 1
    34  	}
    35  
    36  	if err := buildOptions.Validate(); err != nil {
    37  		env.Ui().Error(err.Error())
    38  		env.Ui().Error("")
    39  		env.Ui().Error(c.Help())
    40  		return 1
    41  	}
    42  
    43  	userVars, err := buildOptions.AllUserVars()
    44  	if err != nil {
    45  		env.Ui().Error(fmt.Sprintf("Error compiling user variables: %s", err))
    46  		env.Ui().Error("")
    47  		env.Ui().Error(c.Help())
    48  		return 1
    49  	}
    50  
    51  	// Parse the template into a machine-usable format
    52  	log.Printf("Reading template: %s", args[0])
    53  	tpl, err := packer.ParseTemplateFile(args[0])
    54  	if err != nil {
    55  		env.Ui().Error(fmt.Sprintf("Failed to parse template: %s", err))
    56  		return 1
    57  	}
    58  
    59  	if cfgSyntaxOnly {
    60  		env.Ui().Say("Syntax-only check passed. Everything looks okay.")
    61  		return 0
    62  	}
    63  
    64  	errs := make([]error, 0)
    65  
    66  	// The component finder for our builds
    67  	components := &packer.ComponentFinder{
    68  		Builder:       env.Builder,
    69  		Hook:          env.Hook,
    70  		PostProcessor: env.PostProcessor,
    71  		Provisioner:   env.Provisioner,
    72  	}
    73  
    74  	// Otherwise, get all the builds
    75  	builds, err := buildOptions.Builds(tpl, components)
    76  	if err != nil {
    77  		env.Ui().Error(err.Error())
    78  		return 1
    79  	}
    80  
    81  	// Check the configuration of all builds
    82  	for _, b := range builds {
    83  		log.Printf("Preparing build: %s", b.Name())
    84  		err := b.Prepare(userVars)
    85  		if err != nil {
    86  			errs = append(errs, fmt.Errorf("Errors validating build '%s'. %s", b.Name(), err))
    87  		}
    88  	}
    89  
    90  	if len(errs) > 0 {
    91  		env.Ui().Error("Template validation failed. Errors are shown below.\n")
    92  		for i, err := range errs {
    93  			env.Ui().Error(err.Error())
    94  
    95  			if (i + 1) < len(errs) {
    96  				env.Ui().Error("")
    97  			}
    98  		}
    99  
   100  		return 1
   101  	}
   102  
   103  	env.Ui().Say("Template validated successfully.")
   104  	return 0
   105  }
   106  
   107  func (Command) Synopsis() string {
   108  	return "check that a template is valid"
   109  }