github.com/hashicorp/packer@v1.14.3/builder/file/config.go (about)

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