github.phpd.cn/hashicorp/packer@v1.3.2/builder/parallels/common/output_config.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path"
     7  
     8  	"github.com/hashicorp/packer/common"
     9  	"github.com/hashicorp/packer/template/interpolate"
    10  )
    11  
    12  // OutputConfig contains the configuration for builder's output.
    13  type OutputConfig struct {
    14  	OutputDir string `mapstructure:"output_directory"`
    15  }
    16  
    17  // Prepare configures the output directory or returns an error if it already exists.
    18  func (c *OutputConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
    19  	if c.OutputDir == "" {
    20  		c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
    21  	}
    22  
    23  	var errs []error
    24  
    25  	if path.IsAbs(c.OutputDir) {
    26  		c.OutputDir = path.Clean(c.OutputDir)
    27  	} else {
    28  		wd, err := os.Getwd()
    29  		if err != nil {
    30  			errs = append(errs, err)
    31  		}
    32  		c.OutputDir = path.Clean(path.Join(wd, c.OutputDir))
    33  	}
    34  
    35  	if !pc.PackerForce {
    36  		if _, err := os.Stat(c.OutputDir); err == nil {
    37  			errs = append(errs, fmt.Errorf(
    38  				"Output directory '%s' already exists. It must not exist.", c.OutputDir))
    39  		}
    40  	}
    41  
    42  	return errs
    43  }