From 7d80e37c144b83312dc5021c4cb742d5ef39074d Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Tue, 21 Nov 2017 21:59:27 +0100 Subject: [PATCH] Add new `packer_version` function. Signed-off-by: Krzysztof Wilczynski --- template/interpolate/funcs.go | 26 +++++++++++++------- template/interpolate/funcs_test.go | 21 ++++++++++++++++ website/source/docs/templates/engine.html.md | 1 + 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/template/interpolate/funcs.go b/template/interpolate/funcs.go index ee5e21ccc..924c13d73 100644 --- a/template/interpolate/funcs.go +++ b/template/interpolate/funcs.go @@ -11,6 +11,7 @@ import ( "time" "github.com/hashicorp/packer/common/uuid" + "github.com/hashicorp/packer/version" ) // InitTime is the UTC time when this package was initialized. It is @@ -24,15 +25,16 @@ func init() { // Funcs are the interpolation funcs that are available within interpolations. var FuncGens = map[string]FuncGenerator{ - "build_name": funcGenBuildName, - "build_type": funcGenBuildType, - "env": funcGenEnv, - "isotime": funcGenIsotime, - "pwd": funcGenPwd, - "template_dir": funcGenTemplateDir, - "timestamp": funcGenTimestamp, - "uuid": funcGenUuid, - "user": funcGenUser, + "build_name": funcGenBuildName, + "build_type": funcGenBuildType, + "env": funcGenEnv, + "isotime": funcGenIsotime, + "pwd": funcGenPwd, + "template_dir": funcGenTemplateDir, + "timestamp": funcGenTimestamp, + "uuid": funcGenUuid, + "user": funcGenUser, + "packer_version": funcGenPackerVersion, "upper": funcGenPrimitive(strings.ToUpper), "lower": funcGenPrimitive(strings.ToLower), @@ -152,3 +154,9 @@ func funcGenUuid(ctx *Context) interface{} { return uuid.TimeOrderedUUID() } } + +func funcGenPackerVersion(ctx *Context) interface{} { + return func() string { + return version.FormattedVersion() + } +} diff --git a/template/interpolate/funcs_test.go b/template/interpolate/funcs_test.go index ab8ee36a4..eca121025 100644 --- a/template/interpolate/funcs_test.go +++ b/template/interpolate/funcs_test.go @@ -4,8 +4,11 @@ import ( "os" "path/filepath" "strconv" + "strings" "testing" "time" + + "github.com/hashicorp/packer/version" ) func TestFuncBuildName(t *testing.T) { @@ -257,3 +260,21 @@ func TestFuncUser(t *testing.T) { } } } + +func TestFuncPackerVersion(t *testing.T) { + template := `{{packer_version}}` + + ctx := &Context{} + i := &I{Value: template} + + result, err := i.Render(ctx) + if err != nil { + t.Fatalf("Input: %s\n\nerr: %s", template, err) + } + + // Only match the X.Y.Z portion of the whole version string. + if !strings.HasPrefix(result, version.Version) { + t.Fatalf("Expected input to include: %s\n\nGot: %s", + version.Version, result) + } +} diff --git a/website/source/docs/templates/engine.html.md b/website/source/docs/templates/engine.html.md index 3b7c60b24..065e6001b 100644 --- a/website/source/docs/templates/engine.html.md +++ b/website/source/docs/templates/engine.html.md @@ -43,6 +43,7 @@ Here is a full list of the available functions for reference. - `uuid` - Returns a random UUID. - `upper` - Uppercases the string. - `user` - Specifies a user variable. +- `packer_version` - Returns Packer version. #### Specific to Amazon builders: