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"
|
|
|
|
|
2020-11-12 17:44:02 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/common"
|
2020-11-19 15:07:02 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2020-11-18 13:34:59 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/template/config"
|
2020-11-11 13:21:37 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/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
|
|
|
}
|
|
|
|
|
2020-11-19 15:07:02 -05:00
|
|
|
var errs *packersdk.MultiError
|
2015-06-12 20:24:03 -04:00
|
|
|
|
2015-06-12 21:18:38 -04:00
|
|
|
if c.Target == "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, ErrTargetRequired)
|
2015-06-12 21:18:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 != "" {
|
2020-11-19 15:07:02 -05:00
|
|
|
errs = packersdk.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
|
|
|
}
|