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
7659a914
Commit
7659a914
authored
May 15, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template/interpolate: timestamp
parent
b84ec8da
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
4 deletions
+49
-4
template/interpolate/funcs.go
template/interpolate/funcs.go
+21
-4
template/interpolate/funcs_test.go
template/interpolate/funcs_test.go
+28
-0
No files found.
template/interpolate/funcs.go
View file @
7659a914
...
...
@@ -4,15 +4,26 @@ import (
"errors"
"fmt"
"os"
"strconv"
"text/template"
"time"
)
// InitTime is the UTC time when this package was initialized. It is
// used as the timestamp for all configuration templates so that they
// match for a single build.
var
InitTime
time
.
Time
func
init
()
{
InitTime
=
time
.
Now
()
.
UTC
()
}
// Funcs are the interpolation funcs that are available within interpolations.
var
FuncGens
=
map
[
string
]
FuncGenerator
{
"env"
:
funcGenEnv
,
"isotime"
:
funcGenIsotime
,
"pwd"
:
funcGenPwd
,
"timestamp"
:
funcGenTimestamp
,
"user"
:
funcGenUser
,
}
...
...
@@ -63,6 +74,12 @@ func funcGenPwd(ctx *Context) interface{} {
}
}
func
funcGenTimestamp
(
ctx
*
Context
)
interface
{}
{
return
func
()
string
{
return
strconv
.
FormatInt
(
InitTime
.
Unix
(),
10
)
}
}
func
funcGenUser
(
ctx
*
Context
)
interface
{}
{
return
func
()
string
{
return
""
...
...
template/interpolate/funcs_test.go
View file @
7659a914
...
...
@@ -2,6 +2,7 @@ package interpolate
import
(
"os"
"strconv"
"testing"
"time"
)
...
...
@@ -114,3 +115,30 @@ func TestFuncPwd(t *testing.T) {
}
}
}
func
TestFuncTimestamp
(
t
*
testing
.
T
)
{
expected
:=
strconv
.
FormatInt
(
InitTime
.
Unix
(),
10
)
cases
:=
[]
struct
{
Input
string
Output
string
}{
{
`{{timestamp}}`
,
expected
,
},
}
ctx
:=
&
Context
{}
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
)
}
}
}
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