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
839784b0
Commit
839784b0
authored
May 21, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template: parse post-processors
parent
fbda5b11
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
234 additions
and
7 deletions
+234
-7
template/parse.go
template/parse.go
+87
-5
template/parse_test.go
template/parse_test.go
+102
-0
template/template.go
template/template.go
+10
-2
template/test-fixtures/parse-pp-basic.json
template/test-fixtures/parse-pp-basic.json
+6
-0
template/test-fixtures/parse-pp-keep.json
template/test-fixtures/parse-pp-keep.json
+6
-0
template/test-fixtures/parse-pp-map.json
template/test-fixtures/parse-pp-map.json
+5
-0
template/test-fixtures/parse-pp-multi.json
template/test-fixtures/parse-pp-multi.json
+5
-0
template/test-fixtures/parse-pp-no-type.json
template/test-fixtures/parse-pp-no-type.json
+5
-0
template/test-fixtures/parse-pp-slice.json
template/test-fixtures/parse-pp-slice.json
+5
-0
template/test-fixtures/parse-pp-string.json
template/test-fixtures/parse-pp-string.json
+3
-0
No files found.
template/parse.go
View file @
839784b0
...
...
@@ -19,7 +19,7 @@ type rawTemplate struct {
Builders
[]
map
[
string
]
interface
{}
Push
map
[
string
]
interface
{}
PostProcesors
[]
interface
{}
`mapstructure:"post-processors"`
PostProces
s
ors
[]
interface
{}
`mapstructure:"post-processors"`
Provisioners
[]
map
[
string
]
interface
{}
Variables
map
[
string
]
interface
{}
}
...
...
@@ -94,6 +94,49 @@ func (r *rawTemplate) Template() (*Template, error) {
result
.
Builders
[
b
.
Name
]
=
&
b
}
// Gather all the post-processors
if
len
(
r
.
PostProcessors
)
>
0
{
result
.
PostProcessors
=
make
([][]
*
PostProcessor
,
0
,
len
(
r
.
PostProcessors
))
}
for
i
,
v
:=
range
r
.
PostProcessors
{
// Parse the configurations. We need to do this because post-processors
// can take three different formats.
configs
,
err
:=
r
.
parsePostProcessor
(
i
,
v
)
if
err
!=
nil
{
errs
=
multierror
.
Append
(
errs
,
err
)
continue
}
// Parse the PostProcessors out of the configs
pps
:=
make
([]
*
PostProcessor
,
0
,
len
(
configs
))
for
j
,
c
:=
range
configs
{
var
pp
PostProcessor
if
err
:=
r
.
decoder
(
&
pp
,
nil
)
.
Decode
(
c
);
err
!=
nil
{
errs
=
multierror
.
Append
(
errs
,
fmt
.
Errorf
(
"post-processor %d.%d: %s"
,
i
+
1
,
j
+
1
,
err
))
continue
}
// Type is required
if
pp
.
Type
==
""
{
errs
=
multierror
.
Append
(
errs
,
fmt
.
Errorf
(
"post-processor %d.%d: type is required"
,
i
+
1
,
j
+
1
))
continue
}
// Set the configuration
delete
(
c
,
"keep_input_artifact"
)
delete
(
c
,
"type"
)
if
len
(
c
)
>
0
{
pp
.
Config
=
c
}
pps
=
append
(
pps
,
&
pp
)
}
result
.
PostProcessors
=
append
(
result
.
PostProcessors
,
pps
)
}
// Gather all the provisioners
if
len
(
r
.
Provisioners
)
>
0
{
result
.
Provisioners
=
make
([]
*
Provisioner
,
0
,
len
(
r
.
Provisioners
))
...
...
@@ -152,6 +195,45 @@ func (r *rawTemplate) decoder(
return
d
}
func
(
r
*
rawTemplate
)
parsePostProcessor
(
i
int
,
raw
interface
{})
([]
map
[
string
]
interface
{},
error
)
{
switch
v
:=
raw
.
(
type
)
{
case
string
:
return
[]
map
[
string
]
interface
{}{
{
"type"
:
v
},
},
nil
case
map
[
string
]
interface
{}
:
return
[]
map
[
string
]
interface
{}{
v
},
nil
case
[]
interface
{}
:
var
err
error
result
:=
make
([]
map
[
string
]
interface
{},
len
(
v
))
for
j
,
innerRaw
:=
range
v
{
switch
innerV
:=
innerRaw
.
(
type
)
{
case
string
:
result
[
j
]
=
map
[
string
]
interface
{}{
"type"
:
innerV
}
case
map
[
string
]
interface
{}
:
result
[
j
]
=
innerV
case
[]
interface
{}
:
err
=
multierror
.
Append
(
err
,
fmt
.
Errorf
(
"post-processor %d.%d: sequence not allowed to be nested in a sequence"
,
i
+
1
,
j
+
1
))
default
:
err
=
multierror
.
Append
(
err
,
fmt
.
Errorf
(
"post-processor %d.%d: unknown format"
,
i
+
1
,
j
+
1
))
}
}
if
err
!=
nil
{
return
nil
,
err
}
return
result
,
nil
default
:
return
nil
,
fmt
.
Errorf
(
"post-processor %d: bad format"
,
i
+
1
)
}
}
// Parse takes the given io.Reader and parses a Template object out of it.
func
Parse
(
r
io
.
Reader
)
(
*
Template
,
error
)
{
// First, decode the object into an interface{}. We do this instead of
...
...
template/parse_test.go
View file @
839784b0
...
...
@@ -141,6 +141,108 @@ func TestParse(t *testing.T) {
},
false
,
},
{
"parse-pp-basic.json"
,
&
Template
{
PostProcessors
:
[][]
*
PostProcessor
{
[]
*
PostProcessor
{
&
PostProcessor
{
Type
:
"foo"
,
Config
:
map
[
string
]
interface
{}{
"foo"
:
"bar"
,
},
},
},
},
},
false
,
},
{
"parse-pp-keep.json"
,
&
Template
{
PostProcessors
:
[][]
*
PostProcessor
{
[]
*
PostProcessor
{
&
PostProcessor
{
Type
:
"foo"
,
KeepInputArtifact
:
true
,
},
},
},
},
false
,
},
{
"parse-pp-string.json"
,
&
Template
{
PostProcessors
:
[][]
*
PostProcessor
{
[]
*
PostProcessor
{
&
PostProcessor
{
Type
:
"foo"
,
},
},
},
},
false
,
},
{
"parse-pp-map.json"
,
&
Template
{
PostProcessors
:
[][]
*
PostProcessor
{
[]
*
PostProcessor
{
&
PostProcessor
{
Type
:
"foo"
,
},
},
},
},
false
,
},
{
"parse-pp-slice.json"
,
&
Template
{
PostProcessors
:
[][]
*
PostProcessor
{
[]
*
PostProcessor
{
&
PostProcessor
{
Type
:
"foo"
,
},
},
[]
*
PostProcessor
{
&
PostProcessor
{
Type
:
"bar"
,
},
},
},
},
false
,
},
{
"parse-pp-multi.json"
,
&
Template
{
PostProcessors
:
[][]
*
PostProcessor
{
[]
*
PostProcessor
{
&
PostProcessor
{
Type
:
"foo"
,
},
&
PostProcessor
{
Type
:
"bar"
,
},
},
},
},
false
,
},
{
"parse-pp-no-type.json"
,
nil
,
true
,
},
}
for
_
,
tc
:=
range
cases
{
...
...
template/template.go
View file @
839784b0
...
...
@@ -27,10 +27,10 @@ type Builder struct {
// PostProcessor represents a post-processor within the template.
type
PostProcessor
struct
{
OnlyExcept
OnlyExcept
`mapstructure:",squash"`
Type
string
KeepInputArtifact
bool
KeepInputArtifact
bool
`mapstructure:"keep_input_artifact"`
Config
map
[
string
]
interface
{}
}
...
...
@@ -79,3 +79,11 @@ func (b *Builder) GoString() string {
func
(
p
*
Provisioner
)
GoString
()
string
{
return
fmt
.
Sprintf
(
"*%#v"
,
*
p
)
}
func
(
p
*
PostProcessor
)
GoString
()
string
{
return
fmt
.
Sprintf
(
"*%#v"
,
*
p
)
}
func
(
v
*
Variable
)
GoString
()
string
{
return
fmt
.
Sprintf
(
"*%#v"
,
*
v
)
}
template/test-fixtures/parse-pp-basic.json
0 → 100644
View file @
839784b0
{
"post-processors"
:
[{
"type"
:
"foo"
,
"foo"
:
"bar"
}]
}
template/test-fixtures/parse-pp-keep.json
0 → 100644
View file @
839784b0
{
"post-processors"
:
[{
"type"
:
"foo"
,
"keep_input_artifact"
:
true
}]
}
template/test-fixtures/parse-pp-map.json
0 → 100644
View file @
839784b0
{
"post-processors"
:
[{
"type"
:
"foo"
}]
}
template/test-fixtures/parse-pp-multi.json
0 → 100644
View file @
839784b0
{
"post-processors"
:
[[{
"type"
:
"foo"
},
"bar"
]]
}
template/test-fixtures/parse-pp-no-type.json
0 → 100644
View file @
839784b0
{
"post-processors"
:
[{
"keep_input_artifact"
:
true
}]
}
template/test-fixtures/parse-pp-slice.json
0 → 100644
View file @
839784b0
{
"post-processors"
:
[{
"type"
:
"foo"
},
"bar"
]
}
template/test-fixtures/parse-pp-string.json
0 → 100644
View file @
839784b0
{
"post-processors"
:
[
"foo"
]
}
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