Add new `packer_version` function.

Signed-off-by: Krzysztof Wilczynski <kw@linux.com>
This commit is contained in:
Krzysztof Wilczynski 2017-11-21 21:59:27 +01:00
parent baacb7e173
commit 7d80e37c14
No known key found for this signature in database
GPG Key ID: 7C64768D3DE334E7
3 changed files with 39 additions and 9 deletions

View File

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

View File

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

View File

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