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: ...@@ -41,6 +41,8 @@ IMPROVEMENTS:
dramatically increased. dramatically increased.
* core: Build names are now template processed so you can use things * core: Build names are now template processed so you can use things
like user variables in them. [GH-744] 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 * builder/amazon/all: Launched EC2 instances now have a name of
"Packer Builder" so that they are easily recognizable. [GH-642] "Packer Builder" so that they are easily recognizable. [GH-642]
* builder/amazon/all: Copying AMIs to multiple regions now happens * builder/amazon/all: Copying AMIs to multiple regions now happens
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/mitchellh/packer/common/uuid" "github.com/mitchellh/packer/common/uuid"
"os"
"strconv" "strconv"
"text/template" "text/template"
"time" "time"
...@@ -36,6 +37,7 @@ func NewConfigTemplate() (*ConfigTemplate, error) { ...@@ -36,6 +37,7 @@ func NewConfigTemplate() (*ConfigTemplate, error) {
result.root = template.New("configTemplateRoot") result.root = template.New("configTemplateRoot")
result.root.Funcs(template.FuncMap{ result.root.Funcs(template.FuncMap{
"pwd": templatePwd,
"isotime": templateISOTime, "isotime": templateISOTime,
"timestamp": templateTimestamp, "timestamp": templateTimestamp,
"user": result.templateUser, "user": result.templateUser,
...@@ -97,6 +99,10 @@ func templateISOTime() string { ...@@ -97,6 +99,10 @@ func templateISOTime() string {
return time.Now().UTC().Format(time.RFC3339) return time.Now().UTC().Format(time.RFC3339)
} }
func templatePwd() (string, error) {
return os.Getwd()
}
func templateTimestamp() string { func templateTimestamp() string {
return strconv.FormatInt(InitTime.Unix(), 10) return strconv.FormatInt(InitTime.Unix(), 10)
} }
......
...@@ -2,6 +2,7 @@ package packer ...@@ -2,6 +2,7 @@ package packer
import ( import (
"math" "math"
"os"
"strconv" "strconv"
"testing" "testing"
"time" "time"
...@@ -29,6 +30,27 @@ func TestConfigTemplateProcess_isotime(t *testing.T) { ...@@ -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) { func TestConfigTemplateProcess_timestamp(t *testing.T) {
tpl, err := NewConfigTemplate() tpl, err := NewConfigTemplate()
if err != nil { if err != nil {
......
...@@ -53,8 +53,10 @@ While some configuration settings have local variables specific to only that ...@@ -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_ configuration, a set of functions are available globally for use in _any string_
in Packer templates. These are listed below for reference. in Packer templates. These are listed below for reference.
* ``isotime`` - UTC time in RFC-3339 format. * `pwd` - The working directory while executing Packer.
* ``timestamp`` - The current Unix timestamp in UTC. * `isotime` - UTC time in RFC-3339 format.
* `timestamp` - The current Unix timestamp in UTC.
* `uuid` - Returns a random UUID.
## Amazon Specific Functions ## 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