refactor core version pkg imports out of json interpolation/decode pathway.

This commit is contained in:
Megan Marsh 2020-11-09 13:20:36 -08:00
parent bc85854a53
commit cd59d938b2
5 changed files with 25 additions and 18 deletions

View File

@ -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"`

View File

@ -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
}

View File

@ -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,

View File

@ -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
}
}

View File

@ -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.