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
...
@@ -3,6 +3,7 @@ package packer
import
(
import
(
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"github.com/mitchellh/mapstructure"
)
)
// The rawTemplate struct represents the structure of a template read
// The rawTemplate struct represents the structure of a template read
...
@@ -31,15 +32,19 @@ type Template struct {
...
@@ -31,15 +32,19 @@ type Template struct {
// raw configuration. If requested, this is used to compile into a full
// raw configuration. If requested, this is used to compile into a full
// builder configuration at some point.
// builder configuration at some point.
type
rawBuilderConfig
struct
{
type
rawBuilderConfig
struct
{
builderType
string
Name
string
rawConfig
interface
{}
Type
string
rawConfig
interface
{}
}
}
// rawProvisionerConfig represents a raw, unprocessed provisioner configuration.
// rawProvisionerConfig represents a raw, unprocessed provisioner configuration.
// It contains the type of the provisioner as well as the raw configuration
// It contains the type of the provisioner as well as the raw configuration
// that is handed to the provisioner for it to process.
// that is handed to the provisioner for it to process.
type
rawProvisionerConfig
struct
{
type
rawProvisionerConfig
struct
{
pType
string
Type
string
Override
map
[
string
]
interface
{}
rawConfig
interface
{}
rawConfig
interface
{}
}
}
...
@@ -65,8 +70,20 @@ func ParseTemplate(data []byte) (t *Template, err error) {
...
@@ -65,8 +70,20 @@ func ParseTemplate(data []byte) (t *Template, err error) {
// Gather all the builders
// Gather all the builders
for
i
,
v
:=
range
rawTpl
.
Builders
{
for
i
,
v
:=
range
rawTpl
.
Builders
{
rawType
,
ok
:=
v
[
"type"
]
var
raw
rawBuilderConfig
if
!
ok
{
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
))
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder %d: missing 'type'"
,
i
+
1
))
continue
continue
}
}
...
@@ -74,54 +91,42 @@ func ParseTemplate(data []byte) (t *Template, err error) {
...
@@ -74,54 +91,42 @@ func ParseTemplate(data []byte) (t *Template, err error) {
// Attempt to get the name of the builder. If the "name" key
// Attempt to get the name of the builder. If the "name" key
// missing, use the "type" field, which is guaranteed to exist
// missing, use the "type" field, which is guaranteed to exist
// at this point.
// at this point.
rawName
,
ok
:=
v
[
"name"
]
if
raw
.
Name
==
""
{
if
!
ok
{
raw
.
Name
=
raw
.
Type
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
}
}
// Check if we already have a builder with this name and error if so
// Check if we already have a builder with this name and error if so
if
_
,
ok
:=
t
.
Builders
[
n
ame
];
ok
{
if
_
,
ok
:=
t
.
Builders
[
raw
.
N
ame
];
ok
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder with name '%s' already exists"
,
n
ame
))
errors
=
append
(
errors
,
fmt
.
Errorf
(
"builder with name '%s' already exists"
,
raw
.
N
ame
))
continue
continue
}
}
t
.
Builders
[
name
]
=
rawBuilderConfig
{
raw
.
rawConfig
=
v
typeName
,
v
,
t
.
Builders
[
raw
.
Name
]
=
raw
}
}
}
// Gather all the provisioners
// Gather all the provisioners
for
i
,
v
:=
range
rawTpl
.
Provisioners
{
for
i
,
v
:=
range
rawTpl
.
Provisioners
{
rawType
,
ok
:=
v
[
"type"
]
raw
:=
&
t
.
Provisioners
[
i
]
if
!
ok
{
if
err
:=
mapstructure
.
Decode
(
v
,
raw
);
err
!=
nil
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"provisioner %d: missing 'type'"
,
i
+
1
))
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
continue
}
}
typeName
,
ok
:=
rawType
.
(
string
)
if
raw
.
Type
==
""
{
if
!
ok
{
errors
=
append
(
errors
,
fmt
.
Errorf
(
"provisioner %d: missing 'type'"
,
i
+
1
))
errors
=
append
(
errors
,
fmt
.
Errorf
(
"provisioner %d: type must be a string"
,
i
+
1
))
continue
continue
}
}
t
.
Provisioners
[
i
]
=
rawProvisionerConfig
{
raw
.
rawConfig
=
v
typeName
,
v
,
}
}
}
// If there were errors, we put it into a MultiError and return
// 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
...
@@ -168,13 +173,13 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
panic
(
"no provisioner function"
)
panic
(
"no provisioner function"
)
}
}
builder
,
err
:=
components
.
Builder
(
builderConfig
.
builder
Type
)
builder
,
err
:=
components
.
Builder
(
builderConfig
.
Type
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
if
builder
==
nil
{
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
return
}
}
...
@@ -205,13 +210,13 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
...
@@ -205,13 +210,13 @@ func (t *Template) Build(name string, components *ComponentFinder) (b Build, err
provisioners
:=
make
([]
coreBuildProvisioner
,
0
,
len
(
t
.
Provisioners
))
provisioners
:=
make
([]
coreBuildProvisioner
,
0
,
len
(
t
.
Provisioners
))
for
_
,
rawProvisioner
:=
range
t
.
Provisioners
{
for
_
,
rawProvisioner
:=
range
t
.
Provisioners
{
var
provisioner
Provisioner
var
provisioner
Provisioner
provisioner
,
err
=
components
.
Provisioner
(
rawProvisioner
.
p
Type
)
provisioner
,
err
=
components
.
Provisioner
(
rawProvisioner
.
Type
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
}
}
if
provisioner
==
nil
{
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
return
}
}
...
...
packer/template_test.go
View file @
5638cecd
...
@@ -91,7 +91,7 @@ func TestParseTemplate_BuilderWithoutName(t *testing.T) {
...
@@ -91,7 +91,7 @@ func TestParseTemplate_BuilderWithoutName(t *testing.T) {
builder
,
ok
:=
result
.
Builders
[
"amazon-ebs"
]
builder
,
ok
:=
result
.
Builders
[
"amazon-ebs"
]
assert
.
True
(
ok
,
"should have amazon-ebs builder"
)
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
)
{
func
TestParseTemplate_BuilderWithName
(
t
*
testing
.
T
)
{
...
@@ -116,7 +116,7 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
...
@@ -116,7 +116,7 @@ func TestParseTemplate_BuilderWithName(t *testing.T) {
builder
,
ok
:=
result
.
Builders
[
"bob"
]
builder
,
ok
:=
result
.
Builders
[
"bob"
]
assert
.
True
(
ok
,
"should have bob builder"
)
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
)
{
func
TestParseTemplate_BuilderWithConflictingName
(
t
*
testing
.
T
)
{
...
@@ -213,7 +213,8 @@ func TestParseTemplate_Provisioners(t *testing.T) {
...
@@ -213,7 +213,8 @@ func TestParseTemplate_Provisioners(t *testing.T) {
assert
.
Nil
(
err
,
"should not error"
)
assert
.
Nil
(
err
,
"should not error"
)
assert
.
NotNil
(
result
,
"template should not be nil"
)
assert
.
NotNil
(
result
,
"template should not be nil"
)
assert
.
Length
(
result
.
Provisioners
,
1
,
"should have one provisioner"
)
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
)
{
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