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
b84ec8da
Commit
b84ec8da
authored
May 15, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template/interpolate: isotime
parent
5d205ec1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
3 deletions
+40
-3
template/interpolate/funcs.go
template/interpolate/funcs.go
+20
-3
template/interpolate/funcs_test.go
template/interpolate/funcs_test.go
+20
-0
No files found.
template/interpolate/funcs.go
View file @
b84ec8da
...
...
@@ -2,15 +2,18 @@ package interpolate
import
(
"errors"
"fmt"
"os"
"text/template"
"time"
)
// Funcs are the interpolation funcs that are available within interpolations.
var
FuncGens
=
map
[
string
]
FuncGenerator
{
"env"
:
funcGenEnv
,
"pwd"
:
funcGenPwd
,
"user"
:
funcGenUser
,
"env"
:
funcGenEnv
,
"isotime"
:
funcGenIsotime
,
"pwd"
:
funcGenPwd
,
"user"
:
funcGenUser
,
}
// FuncGenerator is a function that given a context generates a template
...
...
@@ -40,6 +43,20 @@ func funcGenEnv(ctx *Context) interface{} {
}
}
func
funcGenIsotime
(
ctx
*
Context
)
interface
{}
{
return
func
(
format
...
string
)
(
string
,
error
)
{
if
len
(
format
)
==
0
{
return
time
.
Now
()
.
UTC
()
.
Format
(
time
.
RFC3339
),
nil
}
if
len
(
format
)
>
1
{
return
""
,
fmt
.
Errorf
(
"too many values, 1 needed: %v"
,
format
)
}
return
time
.
Now
()
.
UTC
()
.
Format
(
format
[
0
]),
nil
}
}
func
funcGenPwd
(
ctx
*
Context
)
interface
{}
{
return
func
()
(
string
,
error
)
{
return
os
.
Getwd
()
...
...
template/interpolate/funcs_test.go
View file @
b84ec8da
...
...
@@ -3,6 +3,7 @@ package interpolate
import
(
"os"
"testing"
"time"
)
func
TestFuncEnv
(
t
*
testing
.
T
)
{
...
...
@@ -65,6 +66,25 @@ func TestFuncEnv_disable(t *testing.T) {
}
}
func
TestFuncIsotime
(
t
*
testing
.
T
)
{
ctx
:=
&
Context
{}
i
:=
&
I
{
Value
:
"{{isotime}}"
}
result
,
err
:=
i
.
Render
(
ctx
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
val
,
err
:=
time
.
Parse
(
time
.
RFC3339
,
result
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
currentTime
:=
time
.
Now
()
.
UTC
()
if
currentTime
.
Sub
(
val
)
>
2
*
time
.
Second
{
t
.
Fatalf
(
"val: %d (current: %d)"
,
val
,
currentTime
)
}
}
func
TestFuncPwd
(
t
*
testing
.
T
)
{
wd
,
err
:=
os
.
Getwd
()
if
err
!=
nil
{
...
...
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