From c845436e320dbf3d0873116ec3cd83f3a12fbddc Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 16 Mar 2020 15:46:08 +0100 Subject: [PATCH] KeyValues.CopyOn: make sure a receiving nil map is set too --- builder/alicloud/ecs/image_config.go | 2 +- builder/amazon/chroot/builder.go | 2 +- builder/amazon/common/ami_config.go | 4 ++-- builder/amazon/common/run_config.go | 2 +- builder/amazon/ebsvolume/block_device.go | 2 +- builder/hyperone/config.go | 4 ++-- builder/tencentcloud/cvm/run_config.go | 2 +- builder/triton/source_machine_config.go | 2 +- builder/triton/target_image_config.go | 2 +- hcl2template/types.kv.go | 9 ++++++--- 10 files changed, 17 insertions(+), 14 deletions(-) diff --git a/builder/alicloud/ecs/image_config.go b/builder/alicloud/ecs/image_config.go index b870e24ae..08fb86ebf 100644 --- a/builder/alicloud/ecs/image_config.go +++ b/builder/alicloud/ecs/image_config.go @@ -202,7 +202,7 @@ type AlicloudImageConfig struct { func (c *AlicloudImageConfig) Prepare(ctx *interpolate.Context) []error { var errs []error - errs = append(errs, c.AlicloudImageTag.CopyOn(c.AlicloudImageTags)...) + errs = append(errs, c.AlicloudImageTag.CopyOn(&c.AlicloudImageTags)...) if c.AlicloudImageName == "" { errs = append(errs, fmt.Errorf("image_name must be specified")) } else if len(c.AlicloudImageName) < 2 || len(c.AlicloudImageName) > 128 { diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 93f9f2981..3b11f29a0 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -259,7 +259,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) { var errs *packer.MultiError var warns []string - errs = packer.MultiErrorAppend(errs, b.config.RootVolumeTag.CopyOn(b.config.RootVolumeTags)...) + errs = packer.MultiErrorAppend(errs, b.config.RootVolumeTag.CopyOn(&b.config.RootVolumeTags)...) errs = packer.MultiErrorAppend(errs, b.config.SourceAmiFilter.Prepare()...) errs = packer.MultiErrorAppend(errs, b.config.AccessConfig.Prepare(&b.config.ctx)...) diff --git a/builder/amazon/common/ami_config.go b/builder/amazon/common/ami_config.go index 6b98ef4c0..ff0580710 100644 --- a/builder/amazon/common/ami_config.go +++ b/builder/amazon/common/ami_config.go @@ -151,8 +151,8 @@ func stringInSlice(s []string, searchstr string) bool { func (c *AMIConfig) Prepare(accessConfig *AccessConfig, ctx *interpolate.Context) []error { var errs []error - errs = append(errs, c.SnapshotTag.CopyOn(c.SnapshotTags)...) - errs = append(errs, c.AMITag.CopyOn(c.AMITags)...) + errs = append(errs, c.SnapshotTag.CopyOn(&c.SnapshotTags)...) + errs = append(errs, c.AMITag.CopyOn(&c.AMITags)...) if c.AMIName == "" { errs = append(errs, fmt.Errorf("ami_name must be specified")) diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 9ab6fe3bf..03027fd1b 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -430,7 +430,7 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { {c.RunTags, c.RunTag}, {c.SpotTags, c.SpotTag}, } { - errs = append(errs, s.kvs.CopyOn(s.tagMap)...) + errs = append(errs, s.kvs.CopyOn(&s.tagMap)...) } for _, preparer := range []interface{ Prepare() []error }{ diff --git a/builder/amazon/ebsvolume/block_device.go b/builder/amazon/ebsvolume/block_device.go index 594e37172..88e09da0f 100644 --- a/builder/amazon/ebsvolume/block_device.go +++ b/builder/amazon/ebsvolume/block_device.go @@ -37,7 +37,7 @@ func (bds BlockDevices) Prepare(ctx *interpolate.Context) (errs []error) { for _, block := range bds { - errs = append(errs, block.Tag.CopyOn(block.Tags)...) + errs = append(errs, block.Tag.CopyOn(&block.Tags)...) if err := block.Prepare(ctx); err != nil { errs = append(errs, err) diff --git a/builder/hyperone/config.go b/builder/hyperone/config.go index f8430cc91..4d40f9f04 100644 --- a/builder/hyperone/config.go +++ b/builder/hyperone/config.go @@ -267,8 +267,8 @@ func (c *Config) Prepare(raws ...interface{}) ([]string, error) { // Validation var errs *packer.MultiError - errs = packer.MultiErrorAppend(errs, c.ImageTag.CopyOn(c.ImageTags)...) - errs = packer.MultiErrorAppend(errs, c.VmTag.CopyOn(c.VmTags)...) + errs = packer.MultiErrorAppend(errs, c.ImageTag.CopyOn(&c.ImageTags)...) + errs = packer.MultiErrorAppend(errs, c.VmTag.CopyOn(&c.VmTags)...) if es := c.Comm.Prepare(&c.ctx); len(es) > 0 { errs = packer.MultiErrorAppend(errs, es...) diff --git a/builder/tencentcloud/cvm/run_config.go b/builder/tencentcloud/cvm/run_config.go index deb12725d..43e6f8afe 100644 --- a/builder/tencentcloud/cvm/run_config.go +++ b/builder/tencentcloud/cvm/run_config.go @@ -189,7 +189,7 @@ func (cf *TencentCloudRunConfig) Prepare(ctx *interpolate.Context) []error { cf.RunTags = make(map[string]string) } - errs = append(errs, cf.RunTag.CopyOn(cf.RunTags)...) + errs = append(errs, cf.RunTag.CopyOn(&cf.RunTags)...) return errs } diff --git a/builder/triton/source_machine_config.go b/builder/triton/source_machine_config.go index 945edb9fa..e8a583411 100644 --- a/builder/triton/source_machine_config.go +++ b/builder/triton/source_machine_config.go @@ -112,7 +112,7 @@ func (c *SourceMachineConfig) Prepare(ctx *interpolate.Context) []error { c.MachineTags = make(map[string]string) } - errs = append(errs, c.MachineTag.CopyOn(c.MachineTags)...) + errs = append(errs, c.MachineTag.CopyOn(&c.MachineTags)...) return errs } diff --git a/builder/triton/target_image_config.go b/builder/triton/target_image_config.go index 4c160d17e..90b6703ee 100644 --- a/builder/triton/target_image_config.go +++ b/builder/triton/target_image_config.go @@ -48,7 +48,7 @@ type TargetImageConfig struct { func (c *TargetImageConfig) Prepare(ctx *interpolate.Context) []error { var errs []error - errs = append(errs, c.ImageTag.CopyOn(c.ImageTags)...) + errs = append(errs, c.ImageTag.CopyOn(&c.ImageTags)...) if c.ImageName == "" { errs = append(errs, fmt.Errorf("An image_name must be specified")) diff --git a/hcl2template/types.kv.go b/hcl2template/types.kv.go index b9c40960d..25b74b069 100644 --- a/hcl2template/types.kv.go +++ b/hcl2template/types.kv.go @@ -9,9 +9,12 @@ type KeyValue struct { type KeyValues []KeyValue -func (kvs KeyValues) CopyOn(to map[string]string) []error { +func (kvs KeyValues) CopyOn(to *map[string]string) []error { + if *to == nil { + *to = map[string]string{} + } for _, kv := range kvs { - to[kv.Key] = kv.Value + (*to)[kv.Key] = kv.Value } return nil } @@ -22,7 +25,7 @@ type KVFilter struct { } func (kvf *KVFilter) Prepare() []error { - kvf.Filter.CopyOn(kvf.Filters) + kvf.Filter.CopyOn(&kvf.Filters) return nil }