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
cd5cecfe
Commit
cd5cecfe
authored
May 08, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
app: support merging configs
parent
5e17fbda
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
0 deletions
+52
-0
config.go
config.go
+27
-0
config_test.go
config_test.go
+25
-0
No files found.
config.go
View file @
cd5cecfe
...
...
@@ -20,6 +20,33 @@ type config struct {
Commands
map
[
string
]
string
}
// Merge the configurations. Anything in the "new" configuration takes
// precedence over the "old" configuration.
func
mergeConfig
(
a
,
b
*
config
)
*
config
{
configs
:=
[]
*
config
{
a
,
b
}
result
:=
newConfig
()
for
_
,
config
:=
range
configs
{
for
k
,
v
:=
range
config
.
Builders
{
result
.
Builders
[
k
]
=
v
}
for
k
,
v
:=
range
config
.
Commands
{
result
.
Commands
[
k
]
=
v
}
}
return
result
}
// Creates and initializes a new config struct.
func
newConfig
()
*
config
{
result
:=
new
(
config
)
result
.
Builders
=
make
(
map
[
string
]
string
)
result
.
Commands
=
make
(
map
[
string
]
string
)
return
result
}
// Parses a configuration file and returns a proper configuration
// struct.
func
parseConfig
(
data
string
)
(
result
*
config
,
err
error
)
{
...
...
config_test.go
View file @
cd5cecfe
...
...
@@ -5,6 +5,31 @@ import (
"testing"
)
func
TestConfig_MergeConfig
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
true
)
aString
:=
`
[commands]
a = "1"
b = "1"
`
bString
:=
`
[commands]
a = "1"
b = "2"
c = "3"
`
a
,
_
:=
parseConfig
(
aString
)
b
,
_
:=
parseConfig
(
bString
)
result
:=
mergeConfig
(
a
,
b
)
assert
.
Equal
(
result
.
Commands
[
"a"
],
"1"
,
"a should be 1"
)
assert
.
Equal
(
result
.
Commands
[
"b"
],
"2"
,
"a should be 2"
)
assert
.
Equal
(
result
.
Commands
[
"c"
],
"3"
,
"a should be 3"
)
}
func
TestConfig_ParseConfig_Bad
(
t
*
testing
.
T
)
{
assert
:=
asserts
.
NewTestingAsserts
(
t
,
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