2019-10-14 10:43:59 -04:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
|
|
|
|
2015-06-12 20:24:03 -04:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2015-06-12 20:24:03 -04:00
|
|
|
)
|
|
|
|
|
2015-06-12 21:18:38 -04:00
|
|
|
var ErrTargetRequired = fmt.Errorf("target required")
|
|
|
|
var ErrContentSourceConflict = fmt.Errorf("Cannot specify source file AND content")
|
|
|
|
|
2015-06-12 20:24:03 -04:00
|
|
|
type Config struct {
|
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
|
|
|
|
2015-06-12 21:18:38 -04:00
|
|
|
Source string `mapstructure:"source"`
|
|
|
|
Target string `mapstructure:"target"`
|
|
|
|
Content string `mapstructure:"content"`
|
2015-06-12 20:24:03 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (c *Config) Prepare(raws ...interface{}) ([]string, error) {
|
2015-06-12 20:24:03 -04:00
|
|
|
warnings := []string{}
|
|
|
|
|
|
|
|
err := config.Decode(c, &config.DecodeOpts{
|
|
|
|
Interpolate: true,
|
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{},
|
|
|
|
},
|
|
|
|
}, raws...)
|
|
|
|
if err != nil {
|
2019-12-17 05:25:56 -05:00
|
|
|
return warnings, err
|
2015-06-12 20:24:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var errs *packer.MultiError
|
|
|
|
|
2015-06-12 21:18:38 -04:00
|
|
|
if c.Target == "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, ErrTargetRequired)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Content == "" && c.Source == "" {
|
|
|
|
warnings = append(warnings, "Both source file and contents are blank; target will have no content")
|
2015-06-12 20:24:03 -04:00
|
|
|
}
|
|
|
|
|
2015-06-12 21:18:38 -04:00
|
|
|
if c.Content != "" && c.Source != "" {
|
|
|
|
errs = packer.MultiErrorAppend(errs, ErrContentSourceConflict)
|
2015-06-12 20:24:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-17 05:25:56 -05:00
|
|
|
return warnings, errs
|
2015-06-12 20:24:03 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
return warnings, nil
|
2015-06-12 20:24:03 -04:00
|
|
|
}
|