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
349a83d6
Commit
349a83d6
authored
Aug 15, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: Remove Template, in packer now
parent
1e520b16
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
0 additions
and
148 deletions
+0
-148
common/template.go
common/template.go
+0
-81
common/template_test.go
common/template_test.go
+0
-67
No files found.
common/template.go
deleted
100644 → 0
View file @
1e520b16
package
common
import
(
"bytes"
"fmt"
"strconv"
"text/template"
"time"
)
// Template processes string data as a text/template with some common
// elements and functions available. Plugin creators should process as
// many fields as possible through this.
type
Template
struct
{
UserVars
map
[
string
]
string
root
*
template
.
Template
i
int
}
// NewTemplate creates a new template processor.
func
NewTemplate
()
(
*
Template
,
error
)
{
result
:=
&
Template
{
UserVars
:
make
(
map
[
string
]
string
),
}
result
.
root
=
template
.
New
(
"configTemplateRoot"
)
result
.
root
.
Funcs
(
template
.
FuncMap
{
"timestamp"
:
templateTimestamp
,
"user"
:
result
.
templateUser
,
})
return
result
,
nil
}
// Process processes a single string, compiling and executing the template.
func
(
t
*
Template
)
Process
(
s
string
,
data
interface
{})
(
string
,
error
)
{
tpl
,
err
:=
t
.
root
.
New
(
t
.
nextTemplateName
())
.
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
}
// Validate the template.
func
(
t
*
Template
)
Validate
(
s
string
)
error
{
root
,
err
:=
t
.
root
.
Clone
()
if
err
!=
nil
{
return
err
}
_
,
err
=
root
.
New
(
"template"
)
.
Parse
(
s
)
return
err
}
func
(
t
*
Template
)
nextTemplateName
()
string
{
name
:=
fmt
.
Sprintf
(
"tpl%d"
,
t
.
i
)
t
.
i
++
return
name
}
// User is the function exposed as "user" within the templates and
// looks up user variables.
func
(
t
*
Template
)
templateUser
(
n
string
)
(
string
,
error
)
{
result
,
ok
:=
t
.
UserVars
[
n
]
if
!
ok
{
return
""
,
fmt
.
Errorf
(
"uknown user var: %s"
,
n
)
}
return
result
,
nil
}
func
templateTimestamp
()
string
{
return
strconv
.
FormatInt
(
time
.
Now
()
.
UTC
()
.
Unix
(),
10
)
}
common/template_test.go
deleted
100644 → 0
View file @
1e520b16
package
common
import
(
"math"
"strconv"
"testing"
"time"
)
func
TestTemplateProcess_timestamp
(
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
)
}
}
func
TestTemplateProcess_user
(
t
*
testing
.
T
)
{
tpl
,
err
:=
NewTemplate
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
tpl
.
UserVars
[
"foo"
]
=
"bar"
result
,
err
:=
tpl
.
Process
(
`{{user "foo"}}`
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
result
!=
"bar"
{
t
.
Fatalf
(
"bad: %s"
,
result
)
}
}
func
TestTemplateValidate
(
t
*
testing
.
T
)
{
tpl
,
err
:=
NewTemplate
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
// Valid
err
=
tpl
.
Validate
(
`{{user "foo"}}`
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
// Invalid
err
=
tpl
.
Validate
(
`{{idontexist}}`
)
if
err
==
nil
{
t
.
Fatal
(
"should have error"
)
}
}
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