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
5638cecd
Commit
5638cecd
authored
Jun 07, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Use mapstructure for templates, save lots of lines
parent
4a8278d4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
44 deletions
+50
-44
packer/template.go
packer/template.go
+46
-41
packer/template_test.go
packer/template_test.go
+4
-3
No files found.
packer/template.go
View file @
5638cecd
...
...
@@ -3,6 +3,7 @@ package packer
import
(
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
)
// The rawTemplate struct represents the structure of a template read
...
...
@@ -31,15 +32,19 @@ type Template struct {
// raw configuration. If requested, this is used to compile into a full
// builder configuration at some point.
type
rawBuilderConfig
struct
{
builderType
string
rawConfig
interface
{}
Name
string
Type
string
rawConfig
interface
{}
}
// rawProvisionerConfig represents a raw, unprocessed provisioner configuration.
// It contains the type of the provisioner as well as the raw configuration
// that is handed to the provisioner for it to process.
type
rawProvisionerConfig
struct
{
pType
string
Type
string
Override
map
[
string
]
interface
{}
rawConfig
interface
{}
}
...
...
@@ -65,8 +70,20 @@ func ParseTemplate(data []byte) (t *Template, err error) {
// Gather all the builders
for
i
,
v
:=
range
rawTpl
.
Builders
{
rawType
,
ok
:=
v
[
"type"
]
if
!
ok
{
var
raw
rawBuilderConfig
if
err
:=
mapstructure
.
Decode
(
v
,
&
raw
);
err
!=
nil
{
if
merr
,
ok
:=
err
.
(
*
mapstructure
.
Error
);
ok
{
for
_
,
err
:=
range
merr
.
Errors
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder %d: %s"
,
i
+
1
,
err
))
}
}
else
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder %d: %s"
,
i
+
1
,
err
))
}
continue
}
if
raw
.
Type
==
""
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder %d: missing 'type'"
,
i
+
1
))
continue
}
...
...
@@ -74,54 +91,42 @@ func ParseTemplate(data []byte) (t *Template, err error) {
// Attempt to get the name of the builder. If the "name" key
// missing, use the "type" field, which is guaranteed to exist
// at this point.
rawName
,
ok
:=
v
[
"name"
]
if
!
ok
{
rawName
=
v
[
"type"
]
}
// Attempt to convert the name/type to strings, but error if we can't
name
,
ok
:=
rawName
.
(
string
)
if
!
ok
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder %d: name must be a string"
,
i
+
1
))
continue
}
typeName
,
ok
:=
rawType
.
(
string
)
if
!
ok
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder %d: type must be a string"
,
i
+
1
))
continue
if
raw
.
Name
==
""
{
raw
.
Name
=
raw
.
Type
}
// Check if we already have a builder with this name and error if so
if
_
,
ok
:=
t
.
Builders
[
n
ame
];
ok
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder with name '%s' already exists"
,
n
ame
))
if
_
,
ok
:=
t
.
Builders
[
raw
.
N
ame
];
ok
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder with name '%s' already exists"
,
raw
.
N
ame
))
continue
}
t
.
Builders
[
name
]
=
rawBuilderConfig
{
typeName
,
v
,
}
raw
.
rawConfig
=
v
t
.
Builders
[
raw
.
Name
]
=
raw
}
// Gather all the provisioners
for
i
,
v
:=
range
rawTpl
.
Provisioners
{
rawType
,
ok
:=
v
[
"type"
]
if
!
ok
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"provisioner %d: missing 'type'"
,
i
+
1
))
raw
:=
&
t
.
Provisioners
[
i
]
if
err
:=
mapstructure
.
Decode
(
v
,
raw
);
err
!=
nil
{
if
merr
,
ok
:=
err
.
(
*
mapstructure
.
Error
);
ok
{
for
_
,
err
:=
range
merr
.
Errors
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"provisioner %d: %s"
,
i
+
1
,
err
))
}
}
else
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"provisioner %d: %s"
,
i
+
1
,
err
))
}
continue
}
typeName
,
ok
:=
rawType
.
(
string
)
if
!
ok
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"provisioner %d: type must be a string"
,
i
+
1
))
if
raw
.
Type
==
""
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"provisioner %d: missing 'type'"
,
i
+
1
))
continue
}
t
.
Provisioners
[
i
]
=
rawProvisionerConfig
{
typeName
,
v
,
}
raw
.
rawConfig
=
v
}
// If there were errors, we put it into a MultiError and return
...
...
@@ -168,13 +173,13 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
panic
(
"no provisioner function"
)
}
builder
,
err
:=
components
.
Builder
(
builderConfig
.
builder
Type
)
builder
,
err
:=
components
.
Builder
(
builderConfig
.
Type
)
if
err
!=
nil
{
return
}
if
builder
==
nil
{
err
=
fmt
.
Errorf
(
"Builder type not found: %s"
,
builderConfig
.
builder
Type
)
err
=
fmt
.
Errorf
(
"Builder type not found: %s"
,
builderConfig
.
Type
)
return
}
...
...
@@ -205,13 +210,13 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
provisioners
:=
make
([]
coreBuildProvisioner
,
0
,
len
(
t
.
Provisioners
))
for
_
,
rawProvisioner
:=
range
t
.
Provisioners
{
var
provisioner
Provisioner
provisioner
,
err
=
components
.
Provisioner
(
rawProvisioner
.
p
Type
)
provisioner
,
err
=
components
.
Provisioner
(
rawProvisioner
.
Type
)
if
err
!=
nil
{
return
}
if
provisioner
==
nil
{
err
=
fmt
.
Errorf
(
"Provisioner type not found: %s"
,
rawProvisioner
.
p
Type
)
err
=
fmt
.
Errorf
(
"Provisioner type not found: %s"
,
rawProvisioner
.
Type
)
return
}
...
...
packer/template_test.go
View file @
5638cecd
...
...
@@ -91,7 +91,7 @@ func TestParseTemplate_BuilderWithoutName(t *testing.T) {
builder
,
ok
:=
result
.
Builders
[
"amazon-ebs"
]
assert
.
True
(
ok
,
"should have amazon-ebs builder"
)
assert
.
Equal
(
builder
.
builder
Type
,
"amazon-ebs"
,
"builder should be amazon-ebs"
)
assert
.
Equal
(
builder
.
Type
,
"amazon-ebs"
,
"builder should be amazon-ebs"
)
}
func
TestParseTemplate_BuilderWithName
(
t
*
testing
.
T
)
{
...
...
@@ -116,7 +116,7 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
builder
,
ok
:=
result
.
Builders
[
"bob"
]
assert
.
True
(
ok
,
"should have bob builder"
)
assert
.
Equal
(
builder
.
builder
Type
,
"amazon-ebs"
,
"builder should be amazon-ebs"
)
assert
.
Equal
(
builder
.
Type
,
"amazon-ebs"
,
"builder should be amazon-ebs"
)
}
func
TestParseTemplate_BuilderWithConflictingName
(
t
*
testing
.
T
)
{
...
...
@@ -213,7 +213,8 @@ func TestParseTemplate_Provisioners(t *testing.T) {
assert
.
Nil
(
err
,
"should not error"
)
assert
.
NotNil
(
result
,
"template should not be nil"
)
assert
.
Length
(
result
.
Provisioners
,
1
,
"should have one provisioner"
)
assert
.
Equal
(
result
.
Provisioners
[
0
]
.
pType
,
"shell"
,
"provisioner should be shell"
)
assert
.
Equal
(
result
.
Provisioners
[
0
]
.
Type
,
"shell"
,
"provisioner should be shell"
)
assert
.
NotNil
(
result
.
Provisioners
[
0
]
.
rawConfig
,
"should have raw config"
)
}
func
TestTemplate_BuildNames
(
t
*
testing
.
T
)
{
...
...
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