Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
2752e51e
Commit
2752e51e
authored
May 29, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template/interpolate: add template_path
parent
7ce76d28
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
6 deletions
+48
-6
template/interpolate/funcs.go
template/interpolate/funcs.go
+17
-6
template/interpolate/funcs_test.go
template/interpolate/funcs_test.go
+27
-0
template/interpolate/i.go
template/interpolate/i.go
+4
-0
No files found.
template/interpolate/funcs.go
View file @
2752e51e
...
@@ -26,6 +26,7 @@ var FuncGens = map[string]FuncGenerator{
...
@@ -26,6 +26,7 @@ var FuncGens = map[string]FuncGenerator{
"env"
:
funcGenEnv
,
"env"
:
funcGenEnv
,
"isotime"
:
funcGenIsotime
,
"isotime"
:
funcGenIsotime
,
"pwd"
:
funcGenPwd
,
"pwd"
:
funcGenPwd
,
"template_path"
:
funcGenTemplatePath
,
"timestamp"
:
funcGenTimestamp
,
"timestamp"
:
funcGenTimestamp
,
"uuid"
:
funcGenUuid
,
"uuid"
:
funcGenUuid
,
"user"
:
funcGenUser
,
"user"
:
funcGenUser
,
...
@@ -92,6 +93,16 @@ func funcGenPwd(ctx *Context) interface{} {
...
@@ -92,6 +93,16 @@ func funcGenPwd(ctx *Context) interface{} {
}
}
}
}
func
funcGenTemplatePath
(
ctx
*
Context
)
interface
{}
{
return
func
()
(
string
,
error
)
{
if
ctx
==
nil
||
ctx
.
TemplatePath
==
""
{
return
""
,
errors
.
New
(
"template path not available"
)
}
return
ctx
.
TemplatePath
,
nil
}
}
func
funcGenTimestamp
(
ctx
*
Context
)
interface
{}
{
func
funcGenTimestamp
(
ctx
*
Context
)
interface
{}
{
return
func
()
string
{
return
func
()
string
{
return
strconv
.
FormatInt
(
InitTime
.
Unix
(),
10
)
return
strconv
.
FormatInt
(
InitTime
.
Unix
(),
10
)
...
...
template/interpolate/funcs_test.go
View file @
2752e51e
...
@@ -116,6 +116,33 @@ func TestFuncPwd(t *testing.T) {
...
@@ -116,6 +116,33 @@ func TestFuncPwd(t *testing.T) {
}
}
}
}
func
TestFuncTemplatePath
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
Input
string
Output
string
}{
{
`{{template_path}}`
,
`foo`
,
},
}
ctx
:=
&
Context
{
TemplatePath
:
"foo"
,
}
for
_
,
tc
:=
range
cases
{
i
:=
&
I
{
Value
:
tc
.
Input
}
result
,
err
:=
i
.
Render
(
ctx
)
if
err
!=
nil
{
t
.
Fatalf
(
"Input: %s
\n\n
err: %s"
,
tc
.
Input
,
err
)
}
if
result
!=
tc
.
Output
{
t
.
Fatalf
(
"Input: %s
\n\n
Got: %s"
,
tc
.
Input
,
result
)
}
}
}
func
TestFuncTimestamp
(
t
*
testing
.
T
)
{
func
TestFuncTimestamp
(
t
*
testing
.
T
)
{
expected
:=
strconv
.
FormatInt
(
InitTime
.
Unix
(),
10
)
expected
:=
strconv
.
FormatInt
(
InitTime
.
Unix
(),
10
)
...
...
template/interpolate/i.go
View file @
2752e51e
...
@@ -14,6 +14,10 @@ type Context struct {
...
@@ -14,6 +14,10 @@ type Context struct {
// Funcs are extra functions available in the template
// Funcs are extra functions available in the template
Funcs
map
[
string
]
interface
{}
Funcs
map
[
string
]
interface
{}
// TemplatePath is the path to the template that this is being
// rendered within.
TemplatePath
string
// UserVariables is the mapping of user variables that the
// UserVariables is the mapping of user variables that the
// "user" function reads from.
// "user" function reads from.
UserVariables
map
[
string
]
string
UserVariables
map
[
string
]
string
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment