github.com/sneal/packer@v0.5.2/builder/virtualbox/common/output_config.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/packer/common"
     6  	"github.com/mitchellh/packer/packer"
     7  	"os"
     8  )
     9  
    10  type OutputConfig struct {
    11  	OutputDir string `mapstructure:"output_directory"`
    12  }
    13  
    14  func (c *OutputConfig) Prepare(t *packer.ConfigTemplate, pc *common.PackerConfig) []error {
    15  	if c.OutputDir == "" {
    16  		c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
    17  	}
    18  
    19  	templates := map[string]*string{
    20  		"output_directory": &c.OutputDir,
    21  	}
    22  
    23  	errs := make([]error, 0)
    24  	for n, ptr := range templates {
    25  		var err error
    26  		*ptr, err = t.Process(*ptr, nil)
    27  		if err != nil {
    28  			errs = append(errs, fmt.Errorf("Error processing %s: %s", n, err))
    29  		}
    30  	}
    31  
    32  	if !pc.PackerForce {
    33  		if _, err := os.Stat(c.OutputDir); err == nil {
    34  			errs = append(errs, fmt.Errorf(
    35  				"Output directory '%s' already exists. It must not exist.", c.OutputDir))
    36  		}
    37  	}
    38  
    39  	return errs
    40  }