github.com/rothwerx/packer@v0.9.0/builder/null/config.go (about)

     1  package null
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mitchellh/packer/common"
     7  	"github.com/mitchellh/packer/helper/communicator"
     8  	"github.com/mitchellh/packer/helper/config"
     9  	"github.com/mitchellh/packer/packer"
    10  	"github.com/mitchellh/packer/template/interpolate"
    11  )
    12  
    13  type Config struct {
    14  	common.PackerConfig `mapstructure:",squash"`
    15  
    16  	CommConfig communicator.Config `mapstructure:",squash"`
    17  }
    18  
    19  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    20  	var c Config
    21  
    22  	err := config.Decode(&c, &config.DecodeOpts{
    23  		Interpolate:       true,
    24  		InterpolateFilter: &interpolate.RenderFilter{},
    25  	}, raws...)
    26  	if err != nil {
    27  		return nil, nil, err
    28  	}
    29  
    30  	var errs *packer.MultiError
    31  	if es := c.CommConfig.Prepare(nil); len(es) > 0 {
    32  		errs = packer.MultiErrorAppend(errs, es...)
    33  	}
    34  	if c.CommConfig.SSHHost == "" {
    35  		errs = packer.MultiErrorAppend(errs,
    36  			fmt.Errorf("ssh_host must be specified"))
    37  	}
    38  
    39  	if c.CommConfig.SSHUsername == "" {
    40  		errs = packer.MultiErrorAppend(errs,
    41  			fmt.Errorf("ssh_username must be specified"))
    42  	}
    43  
    44  	if c.CommConfig.SSHPassword == "" && c.CommConfig.SSHPrivateKey == "" {
    45  		errs = packer.MultiErrorAppend(errs,
    46  			fmt.Errorf("one of ssh_password and ssh_private_key_file must be specified"))
    47  	}
    48  
    49  	if c.CommConfig.SSHPassword != "" && c.CommConfig.SSHPrivateKey != "" {
    50  		errs = packer.MultiErrorAppend(errs,
    51  			fmt.Errorf("only one of ssh_password and ssh_private_key_file must be specified"))
    52  	}
    53  
    54  	if errs != nil && len(errs.Errors) > 0 {
    55  		return nil, nil, errs
    56  	}
    57  
    58  	return &c, nil, nil
    59  }