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" "time"
"github.com/hashicorp/packer/common/uuid" "github.com/hashicorp/packer/common/uuid"
"github.com/hashicorp/packer/version"
) )
// InitTime is the UTC time when this package was initialized. It is // InitTime is the UTC time when this package was initialized. It is
@ -33,6 +34,7 @@ var FuncGens = map[string]FuncGenerator{
"timestamp": funcGenTimestamp, "timestamp": funcGenTimestamp,
"uuid": funcGenUuid, "uuid": funcGenUuid,
"user": funcGenUser, "user": funcGenUser,
"packer_version": funcGenPackerVersion,
"upper": funcGenPrimitive(strings.ToUpper), "upper": funcGenPrimitive(strings.ToUpper),
"lower": funcGenPrimitive(strings.ToLower), "lower": funcGenPrimitive(strings.ToLower),
@ -152,3 +154,9 @@ func funcGenUuid(ctx *Context) interface{} {
return uuid.TimeOrderedUUID() return uuid.TimeOrderedUUID()
} }
} }
func funcGenPackerVersion(ctx *Context) interface{} {
return func() string {
return version.FormattedVersion()
}
}

View File

@ -4,8 +4,11 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"testing" "testing"
"time" "time"
"github.com/hashicorp/packer/version"
) )
func TestFuncBuildName(t *testing.T) { 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. - `uuid` - Returns a random UUID.
- `upper` - Uppercases the string. - `upper` - Uppercases the string.
- `user` - Specifies a user variable. - `user` - Specifies a user variable.
- `packer_version` - Returns Packer version.
#### Specific to Amazon builders: #### Specific to Amazon builders: