2014-04-06 13:21:22 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2016-02-21 10:52:44 -05:00
|
|
|
"path"
|
2015-05-27 16:49:31 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/common"
|
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2014-04-06 13:21:22 -04:00
|
|
|
)
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// OutputConfig contains the configuration for builder's output.
|
2014-04-06 13:21:22 -04:00
|
|
|
type OutputConfig struct {
|
|
|
|
OutputDir string `mapstructure:"output_directory"`
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:51:55 -05:00
|
|
|
// Prepare configures the output directory or returns an error if it already exists.
|
2015-05-27 16:49:31 -04:00
|
|
|
func (c *OutputConfig) Prepare(ctx *interpolate.Context, pc *common.PackerConfig) []error {
|
2014-04-06 13:21:22 -04:00
|
|
|
if c.OutputDir == "" {
|
|
|
|
c.OutputDir = fmt.Sprintf("output-%s", pc.PackerBuildName)
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:49:31 -04:00
|
|
|
var errs []error
|
2016-02-21 10:52:44 -05:00
|
|
|
|
|
|
|
if path.IsAbs(c.OutputDir) {
|
|
|
|
c.OutputDir = path.Clean(c.OutputDir)
|
|
|
|
} else {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
c.OutputDir = path.Clean(path.Join(wd, c.OutputDir))
|
|
|
|
}
|
|
|
|
|
2014-04-06 13:21:22 -04:00
|
|
|
if !pc.PackerForce {
|
|
|
|
if _, err := os.Stat(c.OutputDir); err == nil {
|
|
|
|
errs = append(errs, fmt.Errorf(
|
|
|
|
"Output directory '%s' already exists. It must not exist.", c.OutputDir))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errs
|
|
|
|
}
|