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
53d0095c
Commit
53d0095c
authored
Aug 08, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: functions for template processing
parent
d5fb4d55
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
0 deletions
+73
-0
common/template.go
common/template.go
+43
-0
common/template_test.go
common/template_test.go
+30
-0
No files found.
common/template.go
0 → 100644
View file @
53d0095c
package
common
import
(
"bytes"
"strconv"
"text/template"
"time"
)
type
Template
struct
{
root
*
template
.
Template
}
func
NewTemplate
()
(
*
Template
,
error
)
{
root
:=
template
.
New
(
"configTemplateRoot"
)
root
.
Funcs
(
template
.
FuncMap
{
"timestamp"
:
templateTimestamp
,
})
result
:=
&
Template
{
root
:
root
,
}
return
result
,
nil
}
func
(
t
*
Template
)
Process
(
s
string
,
data
interface
{})
(
string
,
error
)
{
tpl
,
err
:=
t
.
root
.
New
(
"tpl"
)
.
Parse
(
s
)
if
err
!=
nil
{
return
""
,
err
}
buf
:=
new
(
bytes
.
Buffer
)
if
err
:=
tpl
.
Execute
(
buf
,
data
);
err
!=
nil
{
return
""
,
err
}
return
buf
.
String
(),
nil
}
func
templateTimestamp
()
string
{
return
strconv
.
FormatInt
(
time
.
Now
()
.
UTC
()
.
Unix
(),
10
)
}
common/template_test.go
0 → 100644
View file @
53d0095c
package
common
import
(
"math"
"strconv"
"testing"
"time"
)
func
TestTemplateProcess
(
t
*
testing
.
T
)
{
tpl
,
err
:=
NewTemplate
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
result
,
err
:=
tpl
.
Process
(
`{{timestamp}}`
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
val
,
err
:=
strconv
.
ParseInt
(
result
,
10
,
64
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
currentTime
:=
time
.
Now
()
.
UTC
()
.
Unix
()
if
math
.
Abs
(
float64
(
currentTime
-
val
))
>
10
{
t
.
Fatalf
(
"val: %d (current: %d)"
,
val
,
currentTime
)
}
}
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