github.com/sneal/packer@v0.5.2/builder/docker/config.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/packer/common"
     6  	"github.com/mitchellh/packer/packer"
     7  )
     8  
     9  type Config struct {
    10  	common.PackerConfig `mapstructure:",squash"`
    11  
    12  	ExportPath string `mapstructure:"export_path"`
    13  	Image      string
    14  	Pull       bool
    15  	RunCommand []string `mapstructure:"run_command"`
    16  
    17  	tpl *packer.ConfigTemplate
    18  }
    19  
    20  func NewConfig(raws ...interface{}) (*Config, []string, error) {
    21  	c := new(Config)
    22  	md, err := common.DecodeConfig(c, raws...)
    23  	if err != nil {
    24  		return nil, nil, err
    25  	}
    26  
    27  	c.tpl, err = packer.NewConfigTemplate()
    28  	if err != nil {
    29  		return nil, nil, err
    30  	}
    31  
    32  	c.tpl.UserVars = c.PackerUserVars
    33  
    34  	// Defaults
    35  	if len(c.RunCommand) == 0 {
    36  		c.RunCommand = []string{
    37  			"run",
    38  			"-d", "-i", "-t",
    39  			"-v", "{{.Volumes}}",
    40  			"{{.Image}}",
    41  			"/bin/bash",
    42  		}
    43  	}
    44  
    45  	// Default Pull if it wasn't set
    46  	hasPull := false
    47  	for _, k := range md.Keys {
    48  		if k == "Pull" {
    49  			hasPull = true
    50  			break
    51  		}
    52  	}
    53  
    54  	if !hasPull {
    55  		c.Pull = true
    56  	}
    57  
    58  	errs := common.CheckUnusedConfig(md)
    59  
    60  	templates := map[string]*string{
    61  		"export_path": &c.ExportPath,
    62  		"image":       &c.Image,
    63  	}
    64  
    65  	for n, ptr := range templates {
    66  		var err error
    67  		*ptr, err = c.tpl.Process(*ptr, nil)
    68  		if err != nil {
    69  			errs = packer.MultiErrorAppend(
    70  				errs, fmt.Errorf("Error processing %s: %s", n, err))
    71  		}
    72  	}
    73  
    74  	if c.ExportPath == "" {
    75  		errs = packer.MultiErrorAppend(errs,
    76  			fmt.Errorf("export_path must be specified"))
    77  	}
    78  
    79  	if c.Image == "" {
    80  		errs = packer.MultiErrorAppend(errs,
    81  			fmt.Errorf("image must be specified"))
    82  	}
    83  
    84  	if errs != nil && len(errs.Errors) > 0 {
    85  		return nil, nil, errs
    86  	}
    87  
    88  	return c, nil, nil
    89  }