Commit 4c8b8d4f authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer: add `pwd` function with working directory [GH-762]

parent 476486ef
......@@ -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
......
......@@ -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)
}
......
......@@ -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 {
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment