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
8ed313e7
Commit
8ed313e7
authored
May 10, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
packer: Add concept of hooks to Environment
parent
5ac06e11
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
126 additions
and
20 deletions
+126
-20
packer.go
packer.go
+2
-2
packer/build.go
packer/build.go
+1
-0
packer/environment.go
packer/environment.go
+51
-13
packer/environment_test.go
packer/environment_test.go
+63
-5
packer/hook_test.go
packer/hook_test.go
+9
-0
No files found.
packer.go
View file @
8ed313e7
...
...
@@ -84,9 +84,9 @@ func main() {
}
envConfig
:=
packer
.
DefaultEnvironmentConfig
()
envConfig
.
BuilderFunc
=
config
.
LoadBuilder
envConfig
.
Commands
=
config
.
CommandNames
()
envConfig
.
CommandFunc
=
config
.
LoadCommand
envConfig
.
Components
.
Builder
=
config
.
LoadBuilder
envConfig
.
Components
.
Command
=
config
.
LoadCommand
env
,
err
:=
packer
.
NewEnvironment
(
envConfig
)
if
err
!=
nil
{
...
...
packer/build.go
View file @
8ed313e7
...
...
@@ -17,6 +17,7 @@ type Build interface {
type
coreBuild
struct
{
name
string
builder
Builder
hooks
map
[
string
]
Hook
rawConfig
interface
{}
prepareCalled
bool
...
...
packer/environment.go
View file @
8ed313e7
...
...
@@ -16,31 +16,42 @@ type BuilderFunc func(name string) (Builder, error)
// The function type used to lookup Command implementations.
type
CommandFunc
func
(
name
string
)
(
Command
,
error
)
// The function type used to lookup Hook implementations.
type
HookFunc
func
(
name
string
)
(
Hook
,
error
)
// ComponentFinder is a struct that contains the various function
// pointers necessary to look up components of Packer such as builders,
// commands, etc.
type
ComponentFinder
struct
{
Builder
BuilderFunc
Command
CommandFunc
Hook
HookFunc
}
// The environment interface provides access to the configuration and
// state of a single Packer run.
//
// It allows for things such as executing CLI commands, getting the
// list of available builders, and more.
type
Environment
interface
{
Builder
(
name
string
)
(
Builder
,
error
)
Cli
(
args
[]
string
)
(
int
,
error
)
Builder
(
string
)
(
Builder
,
error
)
Cli
([]
string
)
(
int
,
error
)
Hook
(
string
)
(
Hook
,
error
)
Ui
()
Ui
}
// An implementation of an Environment that represents the Packer core
// environment.
type
coreEnvironment
struct
{
builderFunc
BuilderFunc
commands
[]
string
com
mandFunc
CommandFunc
com
ponents
ComponentFinder
ui
Ui
}
// This struct configures new environments.
type
EnvironmentConfig
struct
{
BuilderFunc
BuilderFunc
CommandFunc
CommandFunc
Commands
[]
string
Components
ComponentFinder
Ui
Ui
}
...
...
@@ -48,8 +59,6 @@ type EnvironmentConfig struct {
// be used to create a new enviroment with NewEnvironment with sane defaults.
func
DefaultEnvironmentConfig
()
*
EnvironmentConfig
{
config
:=
&
EnvironmentConfig
{}
config
.
BuilderFunc
=
func
(
string
)
(
Builder
,
error
)
{
return
nil
,
nil
}
config
.
CommandFunc
=
func
(
string
)
(
Command
,
error
)
{
return
nil
,
nil
}
config
.
Commands
=
make
([]
string
,
0
)
config
.
Ui
=
&
ReaderWriterUi
{
os
.
Stdin
,
os
.
Stdout
}
return
config
...
...
@@ -63,11 +72,25 @@ func NewEnvironment(config *EnvironmentConfig) (resultEnv Environment, err error
}
env
:=
&
coreEnvironment
{}
env
.
builderFunc
=
config
.
BuilderFunc
env
.
commandFunc
=
config
.
CommandFunc
env
.
commands
=
config
.
Commands
env
.
components
=
config
.
Components
env
.
ui
=
config
.
Ui
// We want to make sure the components have valid function pointers.
// If a function pointer was not given, we assume that the function
// will just return a nil component.
if
env
.
components
.
Builder
==
nil
{
env
.
components
.
Builder
=
func
(
string
)
(
Builder
,
error
)
{
return
nil
,
nil
}
}
if
env
.
components
.
Command
==
nil
{
env
.
components
.
Command
=
func
(
string
)
(
Command
,
error
)
{
return
nil
,
nil
}
}
if
env
.
components
.
Hook
==
nil
{
env
.
components
.
Hook
=
func
(
string
)
(
Hook
,
error
)
{
return
nil
,
nil
}
}
resultEnv
=
env
return
}
...
...
@@ -75,7 +98,7 @@ func NewEnvironment(config *EnvironmentConfig) (resultEnv Environment, err error
// Returns a builder of the given name that is registered with this
// environment.
func
(
e
*
coreEnvironment
)
Builder
(
name
string
)
(
b
Builder
,
err
error
)
{
b
,
err
=
e
.
builderFunc
(
name
)
b
,
err
=
e
.
components
.
Builder
(
name
)
if
err
!=
nil
{
return
}
...
...
@@ -87,6 +110,21 @@ func (e *coreEnvironment) Builder(name string) (b Builder, err error) {
return
}
// Returns a hook of the given name that is registered with this
// environment.
func
(
e
*
coreEnvironment
)
Hook
(
name
string
)
(
h
Hook
,
err
error
)
{
h
,
err
=
e
.
components
.
Hook
(
name
)
if
err
!=
nil
{
return
}
if
h
==
nil
{
err
=
fmt
.
Errorf
(
"No hook returned for name: %s"
,
name
)
}
return
}
// Executes a command as if it was typed on the command-line interface.
// The return value is the exit code of the command.
func
(
e
*
coreEnvironment
)
Cli
(
args
[]
string
)
(
result
int
,
err
error
)
{
...
...
@@ -113,7 +151,7 @@ func (e *coreEnvironment) Cli(args []string) (result int, err error) {
}
if
command
==
nil
{
command
,
err
=
e
.
com
mandFunc
(
args
[
0
])
command
,
err
=
e
.
com
ponents
.
Command
(
args
[
0
])
if
err
!=
nil
{
return
}
...
...
@@ -152,7 +190,7 @@ func (e *coreEnvironment) printHelp() {
for
_
,
key
:=
range
e
.
commands
{
var
synopsis
string
command
,
err
:=
e
.
com
mandFunc
(
key
)
command
,
err
:=
e
.
com
ponents
.
Command
(
key
)
if
err
!=
nil
{
synopsis
=
fmt
.
Sprintf
(
"Error loading command: %s"
,
err
.
Error
())
}
else
if
command
==
nil
{
...
...
packer/environment_test.go
View file @
8ed313e7
...
...
@@ -52,6 +52,23 @@ func TestNewEnvironment_NoConfig(t *testing.T) {
assert
.
NotNil
(
err
,
"should be an error"
)
}
func
TestEnvironment_NilComponents
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
config
:=
DefaultEnvironmentConfig
()
config
.
Components
=
*
new
(
ComponentFinder
)
env
,
err
:=
NewEnvironment
(
config
)
assert
.
Nil
(
err
,
"should not have an error"
)
// All of these should not cause panics... so we don't assert
// anything but if there is a panic in the test then yeah, something
// went wrong.
env
.
Builder
(
"foo"
)
env
.
Cli
([]
string
{
"foo"
})
env
.
Hook
(
"foo"
)
}
func
TestEnvironment_Builder
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
...
...
@@ -60,7 +77,7 @@ func TestEnvironment_Builder(t *testing.T) {
builders
[
"foo"
]
=
builder
config
:=
DefaultEnvironmentConfig
()
config
.
BuilderFunc
=
func
(
n
string
)
(
Builder
,
error
)
{
return
builders
[
n
],
nil
}
config
.
Components
.
Builder
=
func
(
n
string
)
(
Builder
,
error
)
{
return
builders
[
n
],
nil
}
env
,
_
:=
NewEnvironment
(
config
)
returnedBuilder
,
err
:=
env
.
Builder
(
"foo"
)
...
...
@@ -72,7 +89,7 @@ func TestEnvironment_Builder_NilError(t *testing.T) {
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
config
:=
DefaultEnvironmentConfig
()
config
.
BuilderFunc
=
func
(
n
string
)
(
Builder
,
error
)
{
return
nil
,
nil
}
config
.
Components
.
Builder
=
func
(
n
string
)
(
Builder
,
error
)
{
return
nil
,
nil
}
env
,
_
:=
NewEnvironment
(
config
)
returnedBuilder
,
err
:=
env
.
Builder
(
"foo"
)
...
...
@@ -84,7 +101,7 @@ func TestEnvironment_Builder_Error(t *testing.T) {
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
config
:=
DefaultEnvironmentConfig
()
config
.
BuilderFunc
=
func
(
n
string
)
(
Builder
,
error
)
{
return
nil
,
errors
.
New
(
"foo"
)
}
config
.
Components
.
Builder
=
func
(
n
string
)
(
Builder
,
error
)
{
return
nil
,
errors
.
New
(
"foo"
)
}
env
,
_
:=
NewEnvironment
(
config
)
returnedBuilder
,
err
:=
env
.
Builder
(
"foo"
)
...
...
@@ -97,7 +114,7 @@ func TestEnvironment_Cli_Error(t *testing.T) {
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
config
:=
DefaultEnvironmentConfig
()
config
.
Com
mandFunc
=
func
(
n
string
)
(
Command
,
error
)
{
return
nil
,
errors
.
New
(
"foo"
)
}
config
.
Com
ponents
.
Command
=
func
(
n
string
)
(
Command
,
error
)
{
return
nil
,
errors
.
New
(
"foo"
)
}
env
,
_
:=
NewEnvironment
(
config
)
_
,
err
:=
env
.
Cli
([]
string
{
"foo"
})
...
...
@@ -114,7 +131,7 @@ func TestEnvironment_Cli_CallsRun(t *testing.T) {
config
:=
&
EnvironmentConfig
{}
config
.
Commands
=
[]
string
{
"foo"
}
config
.
Com
mandFunc
=
func
(
n
string
)
(
Command
,
error
)
{
return
commands
[
n
],
nil
}
config
.
Com
ponents
.
Command
=
func
(
n
string
)
(
Command
,
error
)
{
return
commands
[
n
],
nil
}
env
,
_
:=
NewEnvironment
(
config
)
exitCode
,
err
:=
env
.
Cli
([]
string
{
"foo"
,
"bar"
,
"baz"
})
...
...
@@ -179,6 +196,47 @@ func TestEnvironment_DefaultCli_Version(t *testing.T) {
}
}
func
TestEnvironment_Hook
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
hook
:=
&
TestHook
{}
hooks
:=
make
(
map
[
string
]
Hook
)
hooks
[
"foo"
]
=
hook
config
:=
DefaultEnvironmentConfig
()
config
.
Components
.
Hook
=
func
(
n
string
)
(
Hook
,
error
)
{
return
hooks
[
n
],
nil
}
env
,
_
:=
NewEnvironment
(
config
)
returned
,
err
:=
env
.
Hook
(
"foo"
)
assert
.
Nil
(
err
,
"should be no error"
)
assert
.
Equal
(
returned
,
hook
,
"should return correct hook"
)
}
func
TestEnvironment_Hook_NilError
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
config
:=
DefaultEnvironmentConfig
()
config
.
Components
.
Hook
=
func
(
n
string
)
(
Hook
,
error
)
{
return
nil
,
nil
}
env
,
_
:=
NewEnvironment
(
config
)
returned
,
err
:=
env
.
Hook
(
"foo"
)
assert
.
NotNil
(
err
,
"should be an error"
)
assert
.
Nil
(
returned
,
"should be no hook"
)
}
func
TestEnvironment_Hook_Error
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
config
:=
DefaultEnvironmentConfig
()
config
.
Components
.
Hook
=
func
(
n
string
)
(
Hook
,
error
)
{
return
nil
,
errors
.
New
(
"foo"
)
}
env
,
_
:=
NewEnvironment
(
config
)
returned
,
err
:=
env
.
Hook
(
"foo"
)
assert
.
NotNil
(
err
,
"should be an error"
)
assert
.
Equal
(
err
.
Error
(),
"foo"
,
"should be correct error"
)
assert
.
Nil
(
returned
,
"should be no hook"
)
}
func
TestEnvironment_SettingUi
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
...
...
packer/hook_test.go
0 → 100644
View file @
8ed313e7
package
packer
type
TestHook
struct
{
runCalled
bool
}
func
(
t
*
TestHook
)
Run
(
string
,
interface
{})
{
t
.
runCalled
=
true
}
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