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
915c8ceb
Commit
915c8ceb
authored
Aug 09, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common/command: parse the "-var" flag
parent
229eab06
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
126 additions
and
2 deletions
+126
-2
common/command/build_flags.go
common/command/build_flags.go
+26
-0
common/command/build_flags_test.go
common/command/build_flags_test.go
+97
-0
common/command/template.go
common/command/template.go
+3
-2
No files found.
common/command/build_flags.go
View file @
915c8ceb
...
...
@@ -2,6 +2,8 @@ package command
import
(
"flag"
"fmt"
"strings"
)
// BuildOptionFlags sets the proper command line flags needed for
...
...
@@ -9,4 +11,28 @@ import (
func
BuildOptionFlags
(
fs
*
flag
.
FlagSet
,
f
*
BuildOptions
)
{
fs
.
Var
((
*
SliceValue
)(
&
f
.
Except
),
"except"
,
"build all builds except these"
)
fs
.
Var
((
*
SliceValue
)(
&
f
.
Only
),
"only"
,
"only build the given builds by name"
)
fs
.
Var
((
*
userVarValue
)(
&
f
.
UserVars
),
"var"
,
"specify a user variable"
)
}
// userVarValue is a flag.Value that parses out user variables in
// the form of 'key=value' and sets it on this map.
type
userVarValue
map
[
string
]
string
func
(
v
*
userVarValue
)
String
()
string
{
return
""
}
func
(
v
*
userVarValue
)
Set
(
raw
string
)
error
{
idx
:=
strings
.
Index
(
raw
,
"="
)
if
idx
==
-
1
{
return
fmt
.
Errorf
(
"No '=' value in arg: %s"
,
raw
)
}
if
*
v
==
nil
{
*
v
=
make
(
map
[
string
]
string
)
}
key
,
value
:=
raw
[
0
:
idx
],
raw
[
idx
+
1
:
]
(
*
v
)[
key
]
=
value
return
nil
}
common/command/build_flags_test.go
0 → 100644
View file @
915c8ceb
package
command
import
(
"flag"
"reflect"
"testing"
)
func
TestBuildOptionFlags
(
t
*
testing
.
T
)
{
opts
:=
new
(
BuildOptions
)
fs
:=
flag
.
NewFlagSet
(
"test"
,
flag
.
ContinueOnError
)
BuildOptionFlags
(
fs
,
opts
)
args
:=
[]
string
{
"-except=foo,bar,baz"
,
"-only=a,b"
,
"-var=foo=bar"
,
"-var"
,
"bar=baz"
,
"-var=foo=bang"
,
}
err
:=
fs
.
Parse
(
args
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
expected
:=
[]
string
{
"foo"
,
"bar"
,
"baz"
}
if
!
reflect
.
DeepEqual
(
opts
.
Except
,
expected
)
{
t
.
Fatalf
(
"bad: %#v"
,
opts
.
Except
)
}
expected
=
[]
string
{
"a"
,
"b"
}
if
!
reflect
.
DeepEqual
(
opts
.
Only
,
expected
)
{
t
.
Fatalf
(
"bad: %#v"
,
opts
.
Only
)
}
if
len
(
opts
.
UserVars
)
!=
2
{
t
.
Fatalf
(
"bad: %#v"
,
opts
.
UserVars
)
}
if
opts
.
UserVars
[
"foo"
]
!=
"bang"
{
t
.
Fatalf
(
"bad: %#v"
,
opts
.
UserVars
)
}
if
opts
.
UserVars
[
"bar"
]
!=
"baz"
{
t
.
Fatalf
(
"bad: %#v"
,
opts
.
UserVars
)
}
}
func
TestUserVarValue_implements
(
t
*
testing
.
T
)
{
var
raw
interface
{}
raw
=
new
(
userVarValue
)
if
_
,
ok
:=
raw
.
(
flag
.
Value
);
!
ok
{
t
.
Fatalf
(
"userVarValue should be a Value"
)
}
}
func
TestUserVarValueSet
(
t
*
testing
.
T
)
{
sv
:=
new
(
userVarValue
)
err
:=
sv
.
Set
(
"key=value"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
vars
:=
map
[
string
]
string
(
*
sv
)
if
vars
[
"key"
]
!=
"value"
{
t
.
Fatalf
(
"Bad: %#v"
,
vars
)
}
// Empty value
err
=
sv
.
Set
(
"key="
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
vars
=
map
[
string
]
string
(
*
sv
)
if
vars
[
"key"
]
!=
""
{
t
.
Fatalf
(
"Bad: %#v"
,
vars
)
}
// Equal in value
err
=
sv
.
Set
(
"key=foo=bar"
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
vars
=
map
[
string
]
string
(
*
sv
)
if
vars
[
"key"
]
!=
"foo=bar"
{
t
.
Fatalf
(
"Bad: %#v"
,
vars
)
}
// No equal
err
=
sv
.
Set
(
"key"
)
if
err
==
nil
{
t
.
Fatal
(
"should have error"
)
}
}
common/command/template.go
View file @
915c8ceb
...
...
@@ -10,8 +10,9 @@ import (
// BuildOptions is a set of options related to builds that can be set
// from the command line.
type
BuildOptions
struct
{
Except
[]
string
Only
[]
string
UserVars
map
[
string
]
string
Except
[]
string
Only
[]
string
}
// Validate validates the options
...
...
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