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
5f8330ec
Commit
5f8330ec
authored
May 08, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parse/load configs
parent
8ffbc2ef
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
19 deletions
+60
-19
config.go
config.go
+21
-17
config_test.go
config_test.go
+32
-0
packer.go
packer.go
+7
-2
No files found.
config.go
View file @
5f8330ec
package
main
import
(
"
fmt
"
"
github.com/BurntSushi/toml
"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer/plugin"
"log"
"os/exec"
)
// This is the default, built-in configuration that ships with
// Packer.
const
defaultConfig
=
`
[commands]
build = "packer-command-build"
`
type
config
struct
{
build
s
map
[
string
]
string
c
ommands
map
[
string
]
string
Builder
s
map
[
string
]
string
C
ommands
map
[
string
]
string
}
func
defaultConfig
()
(
result
*
config
)
{
commands
:=
[]
string
{
"build"
}
// Parses a configuration file and returns a proper configuration
// struct.
func
parseConfig
(
data
string
)
(
result
*
config
,
err
error
)
{
result
=
new
(
config
)
result
.
builds
=
make
(
map
[
string
]
string
)
result
.
commands
=
make
(
map
[
string
]
string
)
for
_
,
name
:=
range
commands
{
result
.
commands
[
name
]
=
fmt
.
Sprintf
(
"packer-command-%s"
,
name
)
}
_
,
err
=
toml
.
Decode
(
data
,
&
result
)
return
}
func
(
c
*
config
)
Commands
()
(
result
[]
string
)
{
result
=
make
([]
string
,
0
,
len
(
c
.
commands
))
for
name
,
_
:=
range
c
.
commands
{
// Returns an array of defined command names.
func
(
c
*
config
)
CommandNames
()
(
result
[]
string
)
{
result
=
make
([]
string
,
0
,
len
(
c
.
Commands
))
for
name
,
_
:=
range
c
.
Commands
{
result
=
append
(
result
,
name
)
}
return
}
// This is a proper packer.CommandFunc that can be used to load packer.Command
// implementations from the defined plugins.
func
(
c
*
config
)
LoadCommand
(
name
string
)
(
packer
.
Command
,
error
)
{
log
.
Printf
(
"Loading command: %s
\n
"
,
name
)
commandBin
,
ok
:=
c
.
c
ommands
[
name
]
commandBin
,
ok
:=
c
.
C
ommands
[
name
]
if
!
ok
{
log
.
Printf
(
"Command not found: %s
\n
"
,
name
)
return
nil
,
nil
...
...
config_test.go
0 → 100644
View file @
5f8330ec
package
main
import
(
"cgl.tideland.biz/asserts"
"testing"
)
func
TestConfig_ParseConfig_Bad
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
data
:=
`
[commands]
foo = bar
`
_
,
err
:=
parseConfig
(
data
)
assert
.
NotNil
(
err
,
"should have an error"
)
}
func
TestConfig_ParseConfig_Good
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
data
:=
`
[commands]
foo = "bar"
`
c
,
err
:=
parseConfig
(
data
)
assert
.
Nil
(
err
,
"should not have an error"
)
assert
.
Equal
(
c
.
CommandNames
(),
[]
string
{
"foo"
},
"should have correct command names"
)
assert
.
Equal
(
c
.
Commands
[
"foo"
],
"bar"
,
"should have the command"
)
}
packer.go
View file @
5f8330ec
...
...
@@ -21,9 +21,14 @@ func main() {
defer
plugin
.
CleanupClients
()
config
:=
defaultConfig
()
config
,
err
:=
parseConfig
(
defaultConfig
)
if
err
!=
nil
{
fmt
.
Fprintf
(
os
.
Stderr
,
"Error loading global Packer configuration:
\n\n
%s
\n
"
,
err
)
os
.
Exit
(
1
)
}
envConfig
:=
packer
.
DefaultEnvironmentConfig
()
envConfig
.
Commands
=
config
.
Commands
()
envConfig
.
Commands
=
config
.
Command
Name
s
()
envConfig
.
CommandFunc
=
config
.
LoadCommand
env
,
err
:=
packer
.
NewEnvironment
(
envConfig
)
...
...
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