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
ded13a8b
Commit
ded13a8b
authored
May 23, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Core, and template validate
parent
28dc1c2a
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
167 additions
and
0 deletions
+167
-0
packer/core.go
packer/core.go
+77
-0
packer/core_test.go
packer/core_test.go
+60
-0
packer/packer_test.go
packer/packer_test.go
+11
-0
packer/test-fixtures/validate-dup-builder.json
packer/test-fixtures/validate-dup-builder.json
+10
-0
packer/test-fixtures/validate-req-variable.json
packer/test-fixtures/validate-req-variable.json
+9
-0
No files found.
packer/core.go
0 → 100644
View file @
ded13a8b
package
packer
import
(
"fmt"
"os"
"github.com/hashicorp/go-multierror"
"github.com/mitchellh/packer/template"
)
// Core is the main executor of Packer. If Packer is being used as a
// library, this is the struct you'll want to instantiate to get anything done.
type
Core
struct
{
cache
Cache
components
ComponentFinder
ui
Ui
template
*
template
.
Template
variables
map
[
string
]
string
}
// CoreConfig is the structure for initializing a new Core. Once a CoreConfig
// is used to initialize a Core, it shouldn't be re-used or modified again.
type
CoreConfig
struct
{
Cache
Cache
Components
ComponentFinder
Ui
Ui
Template
*
template
.
Template
Variables
map
[
string
]
string
}
// NewCore creates a new Core.
func
NewCore
(
c
*
CoreConfig
)
(
*
Core
,
error
)
{
if
c
.
Ui
==
nil
{
c
.
Ui
=
&
BasicUi
{
Reader
:
os
.
Stdin
,
Writer
:
os
.
Stdout
,
ErrorWriter
:
os
.
Stdout
,
}
}
return
&
Core
{
cache
:
c
.
Cache
,
components
:
c
.
Components
,
ui
:
c
.
Ui
,
template
:
c
.
Template
,
variables
:
c
.
Variables
,
},
nil
}
// Validate does a full validation of the template.
//
// This will automatically call template.Validate() in addition to doing
// richer semantic checks around variables and so on.
func
(
c
*
Core
)
Validate
()
error
{
// First validate the template in general, we can't do anything else
// unless the template itself is valid.
if
err
:=
c
.
template
.
Validate
();
err
!=
nil
{
return
err
}
// Validate variables are set
var
err
error
for
n
,
v
:=
range
c
.
template
.
Variables
{
if
v
.
Required
{
if
_
,
ok
:=
c
.
variables
[
n
];
!
ok
{
err
=
multierror
.
Append
(
err
,
fmt
.
Errorf
(
"required variable not set: %s"
,
n
))
}
}
}
// TODO: validate all builders exist
// TODO: ^^ provisioner
// TODO: ^^ post-processor
return
err
}
packer/core_test.go
0 → 100644
View file @
ded13a8b
package
packer
import
(
"os"
"testing"
"github.com/mitchellh/packer/template"
)
func
TestCoreValidate
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
File
string
Vars
map
[
string
]
string
Err
bool
}{
{
"validate-dup-builder.json"
,
nil
,
true
,
},
// Required variable not set
{
"validate-req-variable.json"
,
nil
,
true
,
},
{
"validate-req-variable.json"
,
map
[
string
]
string
{
"foo"
:
"bar"
},
false
,
},
}
for
_
,
tc
:=
range
cases
{
f
,
err
:=
os
.
Open
(
fixtureDir
(
tc
.
File
))
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
tpl
,
err
:=
template
.
Parse
(
f
)
f
.
Close
()
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s
\n\n
%s"
,
tc
.
File
,
err
)
}
core
,
err
:=
NewCore
(
&
CoreConfig
{
Template
:
tpl
,
Variables
:
tc
.
Vars
,
})
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s
\n\n
%s"
,
tc
.
File
,
err
)
}
if
err
:=
core
.
Validate
();
(
err
!=
nil
)
!=
tc
.
Err
{
t
.
Fatalf
(
"err: %s
\n\n
%s"
,
tc
.
File
,
err
)
}
}
}
packer/packer_test.go
0 → 100644
View file @
ded13a8b
package
packer
import
(
"path/filepath"
)
const
FixtureDir
=
"./test-fixtures"
func
fixtureDir
(
n
string
)
string
{
return
filepath
.
Join
(
FixtureDir
,
n
)
}
packer/test-fixtures/validate-dup-builder.json
0 → 100644
View file @
ded13a8b
{
"builders"
:
[
{
"type"
:
"foo"
}
],
"provisioners"
:
[{
"type"
:
"foo"
,
"only"
:
[
"bar"
]
}]
}
packer/test-fixtures/validate-req-variable.json
0 → 100644
View file @
ded13a8b
{
"variables"
:
{
"foo"
:
null
},
"builders"
:
[{
"type"
:
"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