From cd59d938b2f441fc27c9f5c8724325cfca97b231 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Mon, 9 Nov 2020 13:20:36 -0800 Subject: [PATCH] refactor core version pkg imports out of json interpolation/decode pathway. --- common/packer_config.go | 2 +- helper/config/decode.go | 23 +++++++++++++---------- packer/build.go | 2 +- template/interpolate/funcs.go | 9 ++++++--- template/interpolate/i.go | 7 ++++--- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/common/packer_config.go b/common/packer_config.go index 3e2c87225..028e58f17 100644 --- a/common/packer_config.go +++ b/common/packer_config.go @@ -6,7 +6,7 @@ package common type PackerConfig struct { PackerBuildName string `mapstructure:"packer_build_name"` PackerBuilderType string `mapstructure:"packer_builder_type"` - PackerCoreVersion bool `mapstructure:"packer_core_version"` + PackerCoreVersion string `mapstructure:"packer_core_version"` PackerDebug bool `mapstructure:"packer_debug"` PackerForce bool `mapstructure:"packer_force"` PackerOnError string `mapstructure:"packer_on_error"` diff --git a/helper/config/decode.go b/helper/config/decode.go index e351d20d0..0bd1b0950 100644 --- a/helper/config/decode.go +++ b/helper/config/decode.go @@ -113,6 +113,7 @@ func Decode(target interface{}, config *DecodeOpts, raws ...interface{}) error { } else { config.InterpolateContext.BuildName = ctx.BuildName config.InterpolateContext.BuildType = ctx.BuildType + config.InterpolateContext.CorePackerVersionString = ctx.CorePackerVersionString config.InterpolateContext.TemplatePath = ctx.TemplatePath config.InterpolateContext.UserVariables = ctx.UserVariables if config.InterpolateContext.Data == nil { @@ -259,11 +260,12 @@ func DetectContextData(raws ...interface{}) (map[interface{}]interface{}, []inte // detecting things like user variables from the raw configuration params. func DetectContext(raws ...interface{}) (*interpolate.Context, error) { var s struct { - BuildName string `mapstructure:"packer_build_name"` - BuildType string `mapstructure:"packer_builder_type"` - TemplatePath string `mapstructure:"packer_template_path"` - Vars map[string]string `mapstructure:"packer_user_variables"` - SensitiveVars []string `mapstructure:"packer_sensitive_variables"` + BuildName string `mapstructure:"packer_build_name"` + BuildType string `mapstructure:"packer_builder_type"` + CorePackerVersionString string `mapstructure:"packer_core_version"` + TemplatePath string `mapstructure:"packer_template_path"` + Vars map[string]string `mapstructure:"packer_user_variables"` + SensitiveVars []string `mapstructure:"packer_sensitive_variables"` } for _, r := range raws { @@ -274,11 +276,12 @@ func DetectContext(raws ...interface{}) (*interpolate.Context, error) { } return &interpolate.Context{ - BuildName: s.BuildName, - BuildType: s.BuildType, - TemplatePath: s.TemplatePath, - UserVariables: s.Vars, - SensitiveVariables: s.SensitiveVars, + BuildName: s.BuildName, + BuildType: s.BuildType, + CorePackerVersionString: s.CorePackerVersionString, + TemplatePath: s.TemplatePath, + UserVariables: s.Vars, + SensitiveVariables: s.SensitiveVars, }, nil } diff --git a/packer/build.go b/packer/build.go index e054d2b55..f495aa5e1 100644 --- a/packer/build.go +++ b/packer/build.go @@ -166,7 +166,7 @@ func (b *CoreBuild) Prepare() (warn []string, err error) { packerConfig := map[string]interface{}{ BuildNameConfigKey: b.Type, BuilderTypeConfigKey: b.BuilderType, - CoreVersionConfigKey: version.Version, + CoreVersionConfigKey: version.FormattedVersion(), DebugConfigKey: b.debug, ForceConfigKey: b.force, OnErrorConfigKey: b.onError, diff --git a/template/interpolate/funcs.go b/template/interpolate/funcs.go index d94be75d8..471343151 100644 --- a/template/interpolate/funcs.go +++ b/template/interpolate/funcs.go @@ -13,7 +13,6 @@ import ( "github.com/hashicorp/packer/common/packerbuilderdata" commontpl "github.com/hashicorp/packer/common/template" "github.com/hashicorp/packer/common/uuid" - "github.com/hashicorp/packer/version" strftime "github.com/jehiah/go-strftime" ) @@ -242,8 +241,12 @@ func funcGenUuid(ctx *Context) interface{} { } func funcGenPackerVersion(ctx *Context) interface{} { - return func() string { - return version.FormattedVersion() + return func() (string, error) { + if ctx == nil || ctx.CorePackerVersionString == "" { + return "", errors.New("packer_version not available") + } + + return ctx.CorePackerVersionString, nil } } diff --git a/template/interpolate/i.go b/template/interpolate/i.go index 852f7f116..8ad4e50df 100644 --- a/template/interpolate/i.go +++ b/template/interpolate/i.go @@ -35,9 +35,10 @@ type Context struct { // // TemplatePath is the path to the template that this is being // rendered within. - BuildName string - BuildType string - TemplatePath string + BuildName string + BuildType string + CorePackerVersionString string + TemplatePath string } // NewContext returns an initialized empty context.