github.com/mitchellh/packer@v1.3.2/builder/null/config.go (about)

     1  package null
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/packer/common"
     7  	"github.com/hashicorp/packer/helper/communicator"
     8  	"github.com/hashicorp/packer/helper/config"
     9  	"github.com/hashicorp/packer/packer"
    10  	"github.com/hashicorp/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  
    35  	if c.CommConfig.Type != "none" {
    36  		if c.CommConfig.Host() == "" {
    37  			errs = packer.MultiErrorAppend(errs,
    38  				fmt.Errorf("a Host must be specified, please reference your communicator documentation"))
    39  		}
    40  
    41  		if c.CommConfig.User() == "" {
    42  			errs = packer.MultiErrorAppend(errs,
    43  				fmt.Errorf("a Username must be specified, please reference your communicator documentation"))
    44  		}
    45  
    46  		if !c.CommConfig.SSHAgentAuth && c.CommConfig.Password() == "" && c.CommConfig.SSHPrivateKeyFile == "" {
    47  			errs = packer.MultiErrorAppend(errs,
    48  				fmt.Errorf("one authentication method must be specified, please reference your communicator documentation"))
    49  		}
    50  
    51  		if (c.CommConfig.SSHAgentAuth &&
    52  			(c.CommConfig.SSHPassword != "" || c.CommConfig.SSHPrivateKeyFile != "")) ||
    53  			(c.CommConfig.SSHPassword != "" && c.CommConfig.SSHPrivateKeyFile != "") {
    54  			errs = packer.MultiErrorAppend(errs,
    55  				fmt.Errorf("only one of ssh_agent_auth, ssh_password, and ssh_private_key_file must be specified"))
    56  
    57  		}
    58  	}
    59  
    60  	if errs != nil && len(errs.Errors) > 0 {
    61  		return nil, nil, errs
    62  	}
    63  
    64  	return &c, nil, nil
    65  }