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
1bcb52a0
Commit
1bcb52a0
authored
Jun 13, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
command/fix: validate resulting template [GH-2075]
parent
1e853f9f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
97 additions
and
0 deletions
+97
-0
command/fix.go
command/fix.go
+29
-0
command/fix_test.go
command/fix_test.go
+58
-0
command/test-fixtures/fix-invalid/template.json
command/test-fixtures/fix-invalid/template.json
+3
-0
command/test-fixtures/fix/template.json
command/test-fixtures/fix/template.json
+7
-0
No files found.
command/fix.go
View file @
1bcb52a0
...
...
@@ -9,6 +9,7 @@ import (
"strings"
"github.com/mitchellh/packer/fix"
"github.com/mitchellh/packer/template"
)
type
FixCommand
struct
{
...
...
@@ -16,7 +17,9 @@ type FixCommand struct {
}
func
(
c
*
FixCommand
)
Run
(
args
[]
string
)
int
{
var
flagValidate
bool
flags
:=
c
.
Meta
.
FlagSet
(
"fix"
,
FlagSetNone
)
flags
.
BoolVar
(
&
flagValidate
,
"validate"
,
true
,
""
)
flags
.
Usage
=
func
()
{
c
.
Ui
.
Say
(
c
.
Help
())
}
if
err
:=
flags
.
Parse
(
args
);
err
!=
nil
{
return
1
...
...
@@ -80,6 +83,28 @@ func (c *FixCommand) Run(args []string) int {
result
=
strings
.
Replace
(
result
,
`\u003c`
,
"<"
,
-
1
)
result
=
strings
.
Replace
(
result
,
`\u003e`
,
">"
,
-
1
)
c
.
Ui
.
Say
(
result
)
if
flagValidate
{
// Attemot to parse and validate the template
tpl
,
err
:=
template
.
Parse
(
strings
.
NewReader
(
result
))
if
err
!=
nil
{
c
.
Ui
.
Error
(
fmt
.
Sprintf
(
"Error! Fixed template fails to parse: %s
\n\n
"
+
"This is usually caused by an error in the input template.
\n
"
+
"Please fix the error and try again."
,
err
))
return
1
}
if
err
:=
tpl
.
Validate
();
err
!=
nil
{
c
.
Ui
.
Error
(
fmt
.
Sprintf
(
"Error! Fixed template failed to validate: %s
\n\n
"
+
"This is usually caused by an error in the input template.
\n
"
+
"Please fix the error and try again."
,
err
))
return
1
}
}
return
0
}
...
...
@@ -102,6 +127,10 @@ Fixes that are run:
pp-vagrant-override Replaces old-style provider overrides for the Vagrant
post-processor to new-style as of Packer 0.5.0.
virtualbox-rename Updates "virtualbox" builders to "virtualbox-iso"
Options:
-validate=true If true (default), validates the fixed template.
`
return
strings
.
TrimSpace
(
helpText
)
...
...
command/fix_test.go
0 → 100644
View file @
1bcb52a0
package
command
import
(
"path/filepath"
"testing"
)
func
TestFix_noArgs
(
t
*
testing
.
T
)
{
c
:=
&
PushCommand
{
Meta
:
testMeta
(
t
)}
code
:=
c
.
Run
(
nil
)
if
code
!=
1
{
t
.
Fatalf
(
"bad: %#v"
,
code
)
}
}
func
TestFix_multiArgs
(
t
*
testing
.
T
)
{
c
:=
&
PushCommand
{
Meta
:
testMeta
(
t
)}
code
:=
c
.
Run
([]
string
{
"one"
,
"two"
})
if
code
!=
1
{
t
.
Fatalf
(
"bad: %#v"
,
code
)
}
}
func
TestFix
(
t
*
testing
.
T
)
{
c
:=
&
FixCommand
{
Meta
:
testMeta
(
t
),
}
args
:=
[]
string
{
filepath
.
Join
(
testFixture
(
"fix"
),
"template.json"
)}
if
code
:=
c
.
Run
(
args
);
code
!=
0
{
fatalCommand
(
t
,
c
.
Meta
)
}
}
func
TestFix_invalidTemplate
(
t
*
testing
.
T
)
{
c
:=
&
FixCommand
{
Meta
:
testMeta
(
t
),
}
args
:=
[]
string
{
filepath
.
Join
(
testFixture
(
"fix-invalid"
),
"template.json"
)}
if
code
:=
c
.
Run
(
args
);
code
!=
1
{
fatalCommand
(
t
,
c
.
Meta
)
}
}
func
TestFix_invalidTemplateDisableValidation
(
t
*
testing
.
T
)
{
c
:=
&
FixCommand
{
Meta
:
testMeta
(
t
),
}
args
:=
[]
string
{
"-validate=false"
,
filepath
.
Join
(
testFixture
(
"fix-invalid"
),
"template.json"
),
}
if
code
:=
c
.
Run
(
args
);
code
!=
0
{
fatalCommand
(
t
,
c
.
Meta
)
}
}
command/test-fixtures/fix-invalid/template.json
0 → 100644
View file @
1bcb52a0
{
"hello"
:
"world"
}
command/test-fixtures/fix/template.json
0 → 100644
View file @
1bcb52a0
{
"builders"
:
[{
"type"
:
"dummy"
}],
"push"
:
{
"name"
:
"foo/bar"
}
}
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