diff --git a/CHANGELOG.md b/CHANGELOG.md index 6baae46b4..6337ee211 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,8 @@ IMPROVEMENTS: dramatically increased. * core: Build names are now template processed so you can use things like user variables in them. [GH-744] +* core: New "pwd" function available globally that returns the working + directory. [GH-762] * builder/amazon/all: Launched EC2 instances now have a name of "Packer Builder" so that they are easily recognizable. [GH-642] * builder/amazon/all: Copying AMIs to multiple regions now happens diff --git a/packer/config_template.go b/packer/config_template.go index 3df291005..c60afdd1a 100644 --- a/packer/config_template.go +++ b/packer/config_template.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "github.com/mitchellh/packer/common/uuid" + "os" "strconv" "text/template" "time" @@ -36,6 +37,7 @@ func NewConfigTemplate() (*ConfigTemplate, error) { result.root = template.New("configTemplateRoot") result.root.Funcs(template.FuncMap{ + "pwd": templatePwd, "isotime": templateISOTime, "timestamp": templateTimestamp, "user": result.templateUser, @@ -97,6 +99,10 @@ func templateISOTime() string { return time.Now().UTC().Format(time.RFC3339) } +func templatePwd() (string, error) { + return os.Getwd() +} + func templateTimestamp() string { return strconv.FormatInt(InitTime.Unix(), 10) } diff --git a/packer/config_template_test.go b/packer/config_template_test.go index dd1e01da8..f5e7394fb 100644 --- a/packer/config_template_test.go +++ b/packer/config_template_test.go @@ -2,6 +2,7 @@ package packer import ( "math" + "os" "strconv" "testing" "time" @@ -29,6 +30,27 @@ func TestConfigTemplateProcess_isotime(t *testing.T) { } } +func TestConfigTemplateProcess_pwd(t *testing.T) { + tpl, err := NewConfigTemplate() + if err != nil { + t.Fatalf("err: %s", err) + } + + pwd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + + result, err := tpl.Process(`{{pwd}}`, nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + if result != pwd { + t.Fatalf("err: %s", result) + } +} + func TestConfigTemplateProcess_timestamp(t *testing.T) { tpl, err := NewConfigTemplate() if err != nil { diff --git a/website/source/docs/templates/configuration-templates.html.markdown b/website/source/docs/templates/configuration-templates.html.markdown index 88d13f159..f2c42c2fb 100644 --- a/website/source/docs/templates/configuration-templates.html.markdown +++ b/website/source/docs/templates/configuration-templates.html.markdown @@ -53,8 +53,10 @@ While some configuration settings have local variables specific to only that configuration, a set of functions are available globally for use in _any string_ in Packer templates. These are listed below for reference. -* ``isotime`` - UTC time in RFC-3339 format. -* ``timestamp`` - The current Unix timestamp in UTC. +* `pwd` - The working directory while executing Packer. +* `isotime` - UTC time in RFC-3339 format. +* `timestamp` - The current Unix timestamp in UTC. +* `uuid` - Returns a random UUID. ## Amazon Specific Functions