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
11644251
Commit
11644251
authored
Apr 20, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Require configuration to create Environment
parent
12049e3d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
34 deletions
+57
-34
packer.go
packer.go
+7
-1
packer/environment.go
packer/environment.go
+22
-18
packer/environment_test.go
packer/environment_test.go
+28
-15
No files found.
packer.go
View file @
11644251
...
...
@@ -3,10 +3,16 @@ package main
import
(
"github.com/mitchellh/packer/packer"
"fmt"
"os"
)
func
main
()
{
env
:=
packer
.
NewEnvironment
(
nil
)
env
,
err
:=
packer
.
NewEnvironment
(
nil
)
if
err
!=
nil
{
fmt
.
Fprintf
(
os
.
Stderr
,
"Packer initialization error:
\n\n
%s
\n
"
,
err
)
os
.
Exit
(
1
)
}
os
.
Exit
(
env
.
Cli
(
os
.
Args
[
1
:
]))
}
packer/environment.go
View file @
11644251
...
...
@@ -2,6 +2,7 @@
package
packer
import
(
"errors"
"fmt"
"os"
"sort"
...
...
@@ -46,33 +47,36 @@ type EnvironmentConfig struct {
Ui
Ui
}
// This creates a new environment
func
NewEnvironment
(
config
*
EnvironmentConfig
)
*
Environment
{
env
:=
&
Environment
{}
env
.
command
=
make
(
map
[
string
]
Command
)
if
config
!=
nil
{
for
k
,
v
:=
range
config
.
Command
{
env
.
command
[
k
]
=
v
}
// DefaultEnvironmentConfig returns a default EnvironmentConfig that can
// be used to create a new enviroment with NewEnvironment with sane defaults.
func
DefaultEnvironmentConfig
()
*
EnvironmentConfig
{
config
:=
&
EnvironmentConfig
{}
config
.
Ui
=
&
ReaderWriterUi
{
os
.
Stdin
,
os
.
Stdout
}
return
config
}
env
.
builderFactory
=
config
.
BuilderFactory
env
.
ui
=
config
.
Ui
// This creates a new environment
func
NewEnvironment
(
config
*
EnvironmentConfig
)
(
env
*
Environment
,
err
error
)
{
if
config
==
nil
{
err
=
errors
.
New
(
"config must be given to initialize environment"
)
return
}
if
_
,
ok
:=
env
.
command
[
"build"
];
!
ok
{
env
.
command
[
"build"
]
=
new
(
buildCommand
)
env
=
&
Environment
{}
env
.
builderFactory
=
config
.
BuilderFactory
env
.
command
=
make
(
map
[
string
]
Command
)
env
.
ui
=
config
.
Ui
for
k
,
v
:=
range
config
.
Command
{
env
.
command
[
k
]
=
v
}
// TODO: Should "version" be allowed to be overriden?
if
_
,
ok
:=
env
.
command
[
"version"
];
!
ok
{
env
.
command
[
"version"
]
=
new
(
versionCommand
)
}
if
env
.
ui
==
nil
{
env
.
ui
=
&
ReaderWriterUi
{
os
.
Stdin
,
os
.
Stdout
}
}
return
env
return
}
// Executes a command as if it was typed on the command-line interface.
...
...
packer/environment_test.go
View file @
11644251
...
...
@@ -32,7 +32,32 @@ func testEnvironment() *Environment {
new
(
bytes
.
Buffer
),
}
return
NewEnvironment
(
config
)
env
,
err
:=
NewEnvironment
(
config
)
if
err
!=
nil
{
panic
(
err
)
}
return
env
}
func
TestEnvironment_DefaultConfig_Ui
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
config
:=
DefaultEnvironmentConfig
()
assert
.
NotNil
(
config
.
Ui
,
"default UI should not be nil"
)
rwUi
,
ok
:=
config
.
Ui
.
(
*
ReaderWriterUi
)
assert
.
True
(
ok
,
"default UI should be ReaderWriterUi"
)
assert
.
Equal
(
rwUi
.
Writer
,
os
.
Stdout
,
"default UI should go to stdout"
)
assert
.
Equal
(
rwUi
.
Reader
,
os
.
Stdin
,
"default UI should read from stdin"
)
}
func
TestNewEnvironment_NoConfig
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
env
,
err
:=
NewEnvironment
(
nil
)
assert
.
Nil
(
env
,
"env should be nil"
)
assert
.
NotNil
(
err
,
"should be an error"
)
}
func
TestEnvironment_Cli_CallsRun
(
t
*
testing
.
T
)
{
...
...
@@ -44,7 +69,7 @@ func TestEnvironment_Cli_CallsRun(t *testing.T) {
config
.
Command
=
make
(
map
[
string
]
Command
)
config
.
Command
[
"foo"
]
=
command
env
:=
NewEnvironment
(
config
)
env
,
_
:=
NewEnvironment
(
config
)
assert
.
Equal
(
env
.
Cli
([]
string
{
"foo"
,
"bar"
,
"baz"
}),
0
,
"runs foo command"
)
assert
.
True
(
command
.
runCalled
,
"run should've been called"
)
assert
.
Equal
(
command
.
runEnv
,
env
,
"should've ran with env"
)
...
...
@@ -99,18 +124,6 @@ func TestEnvironment_DefaultCli_Version(t *testing.T) {
assert
.
Equal
(
defaultEnv
.
Cli
([]
string
{
"bad"
,
"version"
}),
1
,
"version should NOT work anywhere"
)
}
func
TestEnvironment_DefaultUi
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
defaultEnv
:=
NewEnvironment
(
nil
)
assert
.
NotNil
(
defaultEnv
.
Ui
(),
"default UI should not be nil"
)
rwUi
,
ok
:=
defaultEnv
.
Ui
()
.
(
*
ReaderWriterUi
)
assert
.
True
(
ok
,
"default UI should be ReaderWriterUi"
)
assert
.
Equal
(
rwUi
.
Writer
,
os
.
Stdout
,
"default UI should go to stdout"
)
assert
.
Equal
(
rwUi
.
Reader
,
os
.
Stdin
,
"default UI should read from stdin"
)
}
func
TestEnvironment_PrintHelp
(
t
*
testing
.
T
)
{
// Just call the function and verify that no panics occur
testEnvironment
()
.
PrintHelp
()
...
...
@@ -124,7 +137,7 @@ func TestEnvironment_SettingUi(t *testing.T) {
config
:=
&
EnvironmentConfig
{}
config
.
Ui
=
ui
env
:=
NewEnvironment
(
config
)
env
,
_
:=
NewEnvironment
(
config
)
assert
.
Equal
(
env
.
Ui
(),
ui
,
"UIs should be equal"
)
}
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