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 { type PackerConfig struct {
PackerBuildName string `mapstructure:"packer_build_name"` PackerBuildName string `mapstructure:"packer_build_name"`
PackerBuilderType string `mapstructure:"packer_builder_type"` PackerBuilderType string `mapstructure:"packer_builder_type"`
PackerCoreVersion bool `mapstructure:"packer_core_version"` PackerCoreVersion string `mapstructure:"packer_core_version"`
PackerDebug bool `mapstructure:"packer_debug"` PackerDebug bool `mapstructure:"packer_debug"`
PackerForce bool `mapstructure:"packer_force"` PackerForce bool `mapstructure:"packer_force"`
PackerOnError string `mapstructure:"packer_on_error"` PackerOnError string `mapstructure:"packer_on_error"`

View File

@ -113,6 +113,7 @@ func Decode(target interface{}, config *DecodeOpts, raws ...interface{}) error {
} else { } else {
config.InterpolateContext.BuildName = ctx.BuildName config.InterpolateContext.BuildName = ctx.BuildName
config.InterpolateContext.BuildType = ctx.BuildType config.InterpolateContext.BuildType = ctx.BuildType
config.InterpolateContext.CorePackerVersionString = ctx.CorePackerVersionString
config.InterpolateContext.TemplatePath = ctx.TemplatePath config.InterpolateContext.TemplatePath = ctx.TemplatePath
config.InterpolateContext.UserVariables = ctx.UserVariables config.InterpolateContext.UserVariables = ctx.UserVariables
if config.InterpolateContext.Data == nil { 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. // detecting things like user variables from the raw configuration params.
func DetectContext(raws ...interface{}) (*interpolate.Context, error) { func DetectContext(raws ...interface{}) (*interpolate.Context, error) {
var s struct { var s struct {
BuildName string `mapstructure:"packer_build_name"` BuildName string `mapstructure:"packer_build_name"`
BuildType string `mapstructure:"packer_builder_type"` BuildType string `mapstructure:"packer_builder_type"`
TemplatePath string `mapstructure:"packer_template_path"` CorePackerVersionString string `mapstructure:"packer_core_version"`
Vars map[string]string `mapstructure:"packer_user_variables"` TemplatePath string `mapstructure:"packer_template_path"`
SensitiveVars []string `mapstructure:"packer_sensitive_variables"` Vars map[string]string `mapstructure:"packer_user_variables"`
SensitiveVars []string `mapstructure:"packer_sensitive_variables"`
} }
for _, r := range raws { for _, r := range raws {
@ -274,11 +276,12 @@ func DetectContext(raws ...interface{}) (*interpolate.Context, error) {
} }
return &interpolate.Context{ return &interpolate.Context{
BuildName: s.BuildName, BuildName: s.BuildName,
BuildType: s.BuildType, BuildType: s.BuildType,
TemplatePath: s.TemplatePath, CorePackerVersionString: s.CorePackerVersionString,
UserVariables: s.Vars, TemplatePath: s.TemplatePath,
SensitiveVariables: s.SensitiveVars, UserVariables: s.Vars,
SensitiveVariables: s.SensitiveVars,
}, nil }, nil
} }

View File

@ -166,7 +166,7 @@ func (b *CoreBuild) Prepare() (warn []string, err error) {
packerConfig := map[string]interface{}{ packerConfig := map[string]interface{}{
BuildNameConfigKey: b.Type, BuildNameConfigKey: b.Type,
BuilderTypeConfigKey: b.BuilderType, BuilderTypeConfigKey: b.BuilderType,
CoreVersionConfigKey: version.Version, CoreVersionConfigKey: version.FormattedVersion(),
DebugConfigKey: b.debug, DebugConfigKey: b.debug,
ForceConfigKey: b.force, ForceConfigKey: b.force,
OnErrorConfigKey: b.onError, OnErrorConfigKey: b.onError,

View File

@ -13,7 +13,6 @@ import (
"github.com/hashicorp/packer/common/packerbuilderdata" "github.com/hashicorp/packer/common/packerbuilderdata"
commontpl "github.com/hashicorp/packer/common/template" commontpl "github.com/hashicorp/packer/common/template"
"github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/version"
strftime "github.com/jehiah/go-strftime" strftime "github.com/jehiah/go-strftime"
) )
@ -242,8 +241,12 @@ func funcGenUuid(ctx *Context) interface{} {
} }
func funcGenPackerVersion(ctx *Context) interface{} { func funcGenPackerVersion(ctx *Context) interface{} {
return func() string { return func() (string, error) {
return version.FormattedVersion() 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 // TemplatePath is the path to the template that this is being
// rendered within. // rendered within.
BuildName string BuildName string
BuildType string BuildType string
TemplatePath string CorePackerVersionString string
TemplatePath string
} }
// NewContext returns an initialized empty context. // NewContext returns an initialized empty context.