builder/docker: config refactor

This commit is contained in:
Mitchell Hashimoto 2013-11-09 17:07:14 -08:00
parent 1cf89afe06
commit 0e581bfec3
3 changed files with 62 additions and 57 deletions

View File

@ -10,33 +10,16 @@ import (
const BuilderId = "packer.docker" const BuilderId = "packer.docker"
type Builder struct { type Builder struct {
config Config config *Config
runner multistep.Runner runner multistep.Runner
} }
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
md, err := common.DecodeConfig(&b.config, raws...) c, warnings, errs := NewConfig(raws...)
if err != nil { if errs != nil {
return nil, err
}
b.config.tpl, err = packer.NewConfigTemplate()
if err != nil {
return nil, err
}
// Accumulate any errors
errs := common.CheckUnusedConfig(md)
warnings := make([]string, 0)
// Validate the configuration
cwarns, cerrs := b.config.Prepare()
errs = packer.MultiErrorAppend(errs, cerrs...)
warnings = append(warnings, cwarns...)
if errs != nil && len(errs.Errors) > 0 {
return warnings, errs return warnings, errs
} }
b.config = c
return warnings, nil return warnings, nil
} }
@ -52,7 +35,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
// Setup the state bag and initial state for the steps // Setup the state bag and initial state for the steps
state := new(multistep.BasicStateBag) state := new(multistep.BasicStateBag)
state.Put("config", &b.config) state.Put("config", b.config)
state.Put("hook", hook) state.Put("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)

View File

@ -15,8 +15,19 @@ type Config struct {
tpl *packer.ConfigTemplate tpl *packer.ConfigTemplate
} }
func (c *Config) Prepare() ([]string, []error) { func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs := make([]error, 0) c := new(Config)
md, err := common.DecodeConfig(c, raws...)
if err != nil {
return nil, nil, err
}
c.tpl, err = packer.NewConfigTemplate()
if err != nil {
return nil, nil, err
}
errs := common.CheckUnusedConfig(md)
templates := map[string]*string{ templates := map[string]*string{
"export_path": &c.ExportPath, "export_path": &c.ExportPath,
@ -27,18 +38,24 @@ func (c *Config) Prepare() ([]string, []error) {
var err error var err error
*ptr, err = c.tpl.Process(*ptr, nil) *ptr, err = c.tpl.Process(*ptr, nil)
if err != nil { if err != nil {
errs = append( errs = packer.MultiErrorAppend(
errs, fmt.Errorf("Error processing %s: %s", n, err)) errs, fmt.Errorf("Error processing %s: %s", n, err))
} }
} }
if c.ExportPath == "" { if c.ExportPath == "" {
errs = append(errs, fmt.Errorf("export_path must be specified")) errs = packer.MultiErrorAppend(errs,
fmt.Errorf("export_path must be specified"))
} }
if c.Image == "" { if c.Image == "" {
errs = append(errs, fmt.Errorf("image must be specified")) errs = packer.MultiErrorAppend(errs,
fmt.Errorf("image must be specified"))
} }
return nil, errs if errs != nil && len(errs.Errors) > 0 {
return nil, nil, errs
}
return c, nil, nil
} }

View File

@ -1,67 +1,72 @@
package docker package docker
import ( import (
"github.com/mitchellh/packer/packer"
"testing" "testing"
) )
func testConfigStruct(t *testing.T) *Config { func testConfig() map[string]interface{} {
tpl, err := packer.NewConfigTemplate() return map[string]interface{}{
if err != nil { "export_path": "foo",
t.Fatalf("err: %s", err) "image": "bar",
}
} }
return &Config{ func testConfigStruct(t *testing.T) *Config {
ExportPath: "foo", c, warns, errs := NewConfig(testConfig())
Image: "bar", if len(warns) > 0 {
tpl: tpl, t.Fatalf("bad: %#v", len(warns))
} }
if errs != nil {
t.Fatalf("bad: %#v", errs)
}
return c
} }
func TestConfigPrepare_exportPath(t *testing.T) { func TestConfigPrepare_exportPath(t *testing.T) {
c := testConfigStruct(t) raw := testConfig()
// No export path // No export path
c.ExportPath = "" delete(raw, "export_path")
warns, errs := c.Prepare() _, warns, errs := NewConfig(raw)
if len(warns) > 0 { if len(warns) > 0 {
t.Fatalf("bad: %#v", warns) t.Fatalf("bad: %#v", warns)
} }
if len(errs) <= 0 { if errs == nil {
t.Fatalf("bad: %#v", errs) t.Fatal("should error")
} }
// Good export path // Good export path
c.ExportPath = "path" raw["export_path"] = "good"
warns, errs = c.Prepare() _, warns, errs = NewConfig(raw)
if len(warns) > 0 { if len(warns) > 0 {
t.Fatalf("bad: %#v", warns) t.Fatalf("bad: %#v", warns)
} }
if len(errs) > 0 { if errs != nil {
t.Fatalf("bad: %#v", errs) t.Fatalf("bad: %s", errs)
} }
} }
func TestConfigPrepare_image(t *testing.T) { func TestConfigPrepare_image(t *testing.T) {
c := testConfigStruct(t) raw := testConfig()
// No image // No image
c.Image = "" delete(raw, "image")
warns, errs := c.Prepare() _, warns, errs := NewConfig(raw)
if len(warns) > 0 { if len(warns) > 0 {
t.Fatalf("bad: %#v", warns) t.Fatalf("bad: %#v", warns)
} }
if len(errs) <= 0 { if errs == nil {
t.Fatalf("bad: %#v", errs) t.Fatal("should error")
} }
// Good image // Good image
c.Image = "path" raw["image"] = "path"
warns, errs = c.Prepare() _, warns, errs = NewConfig(raw)
if len(warns) > 0 { if len(warns) > 0 {
t.Fatalf("bad: %#v", warns) t.Fatalf("bad: %#v", warns)
} }
if len(errs) > 0 { if errs != nil {
t.Fatalf("bad: %#v", errs) t.Fatalf("bad: %s", errs)
} }
} }