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
7883d937
Commit
7883d937
authored
Aug 08, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: validation and generating unique template names
parent
56728e65
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletion
+38
-1
common/template.go
common/template.go
+19
-1
common/template_test.go
common/template_test.go
+19
-0
No files found.
common/template.go
View file @
7883d937
...
...
@@ -8,12 +8,17 @@ import (
"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
{
UserData
map
[
string
]
string
root
*
template
.
Template
i
int
}
// NewTemplate creates a new template processor.
func
NewTemplate
()
(
*
Template
,
error
)
{
result
:=
&
Template
{
UserData
:
make
(
map
[
string
]
string
),
...
...
@@ -28,8 +33,9 @@ func NewTemplate() (*Template, error) {
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
(
"tpl"
)
.
Parse
(
s
)
tpl
,
err
:=
t
.
root
.
New
(
t
.
nextTemplateName
()
)
.
Parse
(
s
)
if
err
!=
nil
{
return
""
,
err
}
...
...
@@ -42,6 +48,18 @@ func (t *Template) Process(s string, data interface{}) (string, error) {
return
buf
.
String
(),
nil
}
// Validate the template.
func
(
t
*
Template
)
Validate
(
s
string
)
error
{
_
,
err
:=
t
.
root
.
New
(
t
.
nextTemplateName
())
.
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
)
{
...
...
common/template_test.go
View file @
7883d937
...
...
@@ -46,3 +46,22 @@ func TestTemplateProcess_user(t *testing.T) {
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