github.com/Hashicorp/packer@v1.3.2/builder/file/config.go (about)

     1  package file
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/common"
     7  	"github.com/hashicorp/packer/helper/config"
     8  	"github.com/hashicorp/packer/packer"
     9  	"github.com/hashicorp/packer/template/interpolate"
    10  )
    11  
    12  var ErrTargetRequired = fmt.Errorf("target required")
    13  var ErrContentSourceConflict = fmt.Errorf("Cannot specify source file AND content")
    14  
    15  type Config struct {
    16  	common.PackerConfig `mapstructure:",squash"`
    17  
    18  	Source  string `mapstructure:"source"`
    19  	Target  string `mapstructure:"target"`
    20  	Content string `mapstructure:"content"`
    21  }
    22  
    23  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    24  	c := new(Config)
    25  	warnings := []string{}
    26  
    27  	err := config.Decode(c, &config.DecodeOpts{
    28  		Interpolate: true,
    29  		InterpolateFilter: &interpolate.RenderFilter{
    30  			Exclude: []string{},
    31  		},
    32  	}, raws...)
    33  	if err != nil {
    34  		return nil, warnings, err
    35  	}
    36  
    37  	var errs *packer.MultiError
    38  
    39  	if c.Target == "" {
    40  		errs = packer.MultiErrorAppend(errs, ErrTargetRequired)
    41  	}
    42  
    43  	if c.Content == "" && c.Source == "" {
    44  		warnings = append(warnings, "Both source file and contents are blank; target will have no content")
    45  	}
    46  
    47  	if c.Content != "" && c.Source != "" {
    48  		errs = packer.MultiErrorAppend(errs, ErrContentSourceConflict)
    49  	}
    50  
    51  	if errs != nil && len(errs.Errors) > 0 {
    52  		return nil, warnings, errs
    53  	}
    54  
    55  	return c, warnings, nil
    56  }