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
520503e1
Commit
520503e1
authored
May 02, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Turn Environment into an interface
parent
fefd2ae2
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
35 additions
and
26 deletions
+35
-26
packer/build_command.go
packer/build_command.go
+1
-1
packer/command.go
packer/command.go
+8
-1
packer/environment.go
packer/environment.go
+22
-15
packer/environment_test.go
packer/environment_test.go
+3
-8
packer/version.go
packer/version.go
+1
-1
No files found.
packer/build_command.go
View file @
520503e1
...
...
@@ -6,7 +6,7 @@ import (
type
buildCommand
byte
func
(
buildCommand
)
Run
(
env
*
Environment
,
args
[]
string
)
int
{
func
(
buildCommand
)
Run
(
env
Environment
,
args
[]
string
)
int
{
if
len
(
args
)
!=
1
{
// TODO: Error message
return
1
...
...
packer/command.go
View file @
520503e1
package
packer
import
"net/rpc"
// A command is a runnable sub-command of the `packer` application.
// When `packer` is called with the proper subcommand, this will be
// called.
...
...
@@ -14,7 +16,12 @@ package packer
// Synopsis should return a one-line, short synopsis of the command.
// This should be less than 50 characters ideally.
type
Command
interface
{
Run
(
env
*
Environment
,
args
[]
string
)
int
Run
(
env
Environment
,
args
[]
string
)
int
Synopsis
()
string
}
// An RPCCommand is an implementation of the Command interface where the
// command is actually executed over an RPC connection.
type
RPCCommand
struct
{
client
*
rpc
.
Client
}
packer/environment.go
View file @
520503e1
...
...
@@ -9,14 +9,20 @@ import (
"strings"
)
// The environment
struct contains all the state necessary for a single
//
instance of Packer
.
// The environment
interface provides access to the configuration and
//
state of a single Packer run
.
//
// It is *not* a singleton, but generally a single environment is created
// when Packer starts running to represent that Packer run. Technically,
// if you're building a custom Packer binary, you could instantiate multiple
// environments and run them in parallel.
type
Environment
struct
{
// It allows for things such as executing CLI commands, getting the
// list of available builders, and more.
type
Environment
interface
{
BuilderFactory
()
BuilderFactory
Cli
(
args
[]
string
)
int
Ui
()
Ui
}
// An implementation of an Environment that represents the Packer core
// environment.
type
coreEnvironment
struct
{
builderFactory
BuilderFactory
command
map
[
string
]
Command
ui
Ui
...
...
@@ -40,13 +46,13 @@ func DefaultEnvironmentConfig() *EnvironmentConfig {
}
// This creates a new environment
func
NewEnvironment
(
config
*
EnvironmentConfig
)
(
env
*
Environment
,
err
error
)
{
func
NewEnvironment
(
config
*
EnvironmentConfig
)
(
resultEnv
Environment
,
err
error
)
{
if
config
==
nil
{
err
=
errors
.
New
(
"config must be given to initialize environment"
)
return
}
env
=
&
Environment
{}
env
:=
&
core
Environment
{}
env
.
builderFactory
=
config
.
BuilderFactory
env
.
command
=
make
(
map
[
string
]
Command
)
env
.
ui
=
config
.
Ui
...
...
@@ -60,19 +66,20 @@ func NewEnvironment(config *EnvironmentConfig) (env *Environment, err error) {
env
.
command
[
"version"
]
=
new
(
versionCommand
)
}
resultEnv
=
env
return
}
// Returns the BuilderFactory associated with this Environment.
func
(
e
*
Environment
)
BuilderFactory
()
BuilderFactory
{
func
(
e
*
core
Environment
)
BuilderFactory
()
BuilderFactory
{
return
e
.
builderFactory
}
// 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
*
Environment
)
Cli
(
args
[]
string
)
int
{
func
(
e
*
core
Environment
)
Cli
(
args
[]
string
)
int
{
if
len
(
args
)
==
0
||
args
[
0
]
==
"--help"
||
args
[
0
]
==
"-h"
{
e
.
P
rintHelp
()
e
.
p
rintHelp
()
return
1
}
...
...
@@ -89,7 +96,7 @@ func (e *Environment) Cli(args []string) int {
// If we still don't have a command, show the help.
if
command
==
nil
{
e
.
P
rintHelp
()
e
.
p
rintHelp
()
return
1
}
}
...
...
@@ -98,7 +105,7 @@ func (e *Environment) Cli(args []string) int {
}
// Prints the CLI help to the UI.
func
(
e
*
Environment
)
P
rintHelp
()
{
func
(
e
*
coreEnvironment
)
p
rintHelp
()
{
// Created a sorted slice of the map keys and record the longest
// command name so we can better format the output later.
commandKeys
:=
make
([]
string
,
len
(
e
.
command
))
...
...
@@ -131,6 +138,6 @@ func (e *Environment) PrintHelp() {
// Returns the UI for the environment. The UI is the interface that should
// be used for all communication with the outside world.
func
(
e
*
Environment
)
Ui
()
Ui
{
func
(
e
*
core
Environment
)
Ui
()
Ui
{
return
e
.
ui
}
packer/environment_test.go
View file @
520503e1
...
...
@@ -11,10 +11,10 @@ import (
type
TestCommand
struct
{
runArgs
[]
string
runCalled
bool
runEnv
*
Environment
runEnv
Environment
}
func
(
tc
*
TestCommand
)
Run
(
env
*
Environment
,
args
[]
string
)
int
{
func
(
tc
*
TestCommand
)
Run
(
env
Environment
,
args
[]
string
)
int
{
tc
.
runCalled
=
true
tc
.
runArgs
=
args
tc
.
runEnv
=
env
...
...
@@ -25,7 +25,7 @@ func (tc *TestCommand) Synopsis() string {
return
""
}
func
testEnvironment
()
*
Environment
{
func
testEnvironment
()
Environment
{
config
:=
&
EnvironmentConfig
{}
config
.
Ui
=
&
ReaderWriterUi
{
new
(
bytes
.
Buffer
),
...
...
@@ -140,11 +140,6 @@ func TestEnvironment_DefaultCli_Version(t *testing.T) {
assert
.
Equal
(
defaultEnv
.
Cli
([]
string
{
"bad"
,
"version"
}),
1
,
"version should NOT work anywhere"
)
}
func
TestEnvironment_PrintHelp
(
t
*
testing
.
T
)
{
// Just call the function and verify that no panics occur
testEnvironment
()
.
PrintHelp
()
}
func
TestEnvironment_SettingUi
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
...
...
packer/version.go
View file @
520503e1
...
...
@@ -6,7 +6,7 @@ const Version = "0.1.0.dev"
type
versionCommand
byte
// Implement the Command interface by simply showing the version
func
(
versionCommand
)
Run
(
env
*
Environment
,
args
[]
string
)
int
{
func
(
versionCommand
)
Run
(
env
Environment
,
args
[]
string
)
int
{
env
.
Ui
()
.
Say
(
"Packer v%v
\n
"
,
Version
)
return
0
}
...
...
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