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
f9c59c71
Commit
f9c59c71
authored
May 22, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Better error handling for template parsing
parent
615f993d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
9 deletions
+50
-9
packer/template.go
packer/template.go
+13
-9
packer/template_test.go
packer/template_test.go
+37
-0
No files found.
packer/template.go
View file @
f9c59c71
...
@@ -48,11 +48,12 @@ func ParseTemplate(data []byte) (t *Template, err error) {
...
@@ -48,11 +48,12 @@ func ParseTemplate(data []byte) (t *Template, err error) {
t
.
Builders
=
make
(
map
[
string
]
rawBuilderConfig
)
t
.
Builders
=
make
(
map
[
string
]
rawBuilderConfig
)
t
.
Hooks
=
rawTpl
.
Hooks
t
.
Hooks
=
rawTpl
.
Hooks
for
_
,
v
:=
range
rawTpl
.
Builders
{
errors
:=
make
([]
error
,
0
)
for
i
,
v
:=
range
rawTpl
.
Builders
{
rawType
,
ok
:=
v
[
"type"
]
rawType
,
ok
:=
v
[
"type"
]
if
!
ok
{
if
!
ok
{
// TODO: Missing type error
errors
=
append
(
errors
,
fmt
.
Errorf
(
"missing 'type' for builder %d"
,
i
+
1
))
return
continue
}
}
// Attempt to get the name of the builder. If the "name" key
// Attempt to get the name of the builder. If the "name" key
...
@@ -67,17 +68,20 @@ func ParseTemplate(data []byte) (t *Template, err error) {
...
@@ -67,17 +68,20 @@ func ParseTemplate(data []byte) (t *Template, err error) {
name
:=
rawName
.
(
string
)
name
:=
rawName
.
(
string
)
typeName
:=
rawType
.
(
string
)
typeName
:=
rawType
.
(
string
)
// Check if we already have a builder with this name and record
// Check if we already have a builder with this name and error if so
// an error.
if
_
,
ok
:=
t
.
Builders
[
name
];
ok
{
_
,
ok
=
t
.
Builders
[
name
]
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder with name '%s' already exists"
,
name
))
if
ok
{
continue
// TODO: We already have a builder with this name
return
}
}
t
.
Builders
[
name
]
=
rawBuilderConfig
{
typeName
,
v
}
t
.
Builders
[
name
]
=
rawBuilderConfig
{
typeName
,
v
}
}
}
if
len
(
errors
)
>
0
{
err
=
&
MultiError
{
errors
}
return
}
return
return
}
}
...
...
packer/template_test.go
View file @
f9c59c71
...
@@ -40,6 +40,20 @@ func TestParseTemplate_Invalid(t *testing.T) {
...
@@ -40,6 +40,20 @@ func TestParseTemplate_Invalid(t *testing.T) {
assert
.
Nil
(
result
,
"should have no result"
)
assert
.
Nil
(
result
,
"should have no result"
)
}
}
func
TestParseTemplate_BuilderWithoutType
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
data
:=
`
{
"name": "my-image",
"builders": [{}]
}
`
_
,
err
:=
ParseTemplate
([]
byte
(
data
))
assert
.
NotNil
(
err
,
"should have error"
)
}
func
TestParseTemplate_BuilderWithoutName
(
t
*
testing
.
T
)
{
func
TestParseTemplate_BuilderWithoutName
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
...
@@ -89,6 +103,29 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
...
@@ -89,6 +103,29 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
assert
.
Equal
(
builder
.
builderName
,
"amazon-ebs"
,
"builder should be amazon-ebs"
)
assert
.
Equal
(
builder
.
builderName
,
"amazon-ebs"
,
"builder should be amazon-ebs"
)
}
}
func
TestParseTemplate_BuilderWithConflictingName
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
data
:=
`
{
"name": "my-image",
"builders": [
{
"name": "bob",
"type": "amazon-ebs"
},
{
"name": "bob",
"type": "foo",
}
]
}
`
_
,
err
:=
ParseTemplate
([]
byte
(
data
))
assert
.
NotNil
(
err
,
"should have error"
)
}
func
TestParseTemplate_Hooks
(
t
*
testing
.
T
)
{
func
TestParseTemplate_Hooks
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
...
...
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