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
7f78a2c5
Commit
7f78a2c5
authored
May 26, 2015
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
helper/flag-kv: can parse JSON files
parent
dd0a7755
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
0 deletions
+96
-0
helper/flag-kv/flag_json.go
helper/flag-kv/flag_json.go
+34
-0
helper/flag-kv/flag_json_test.go
helper/flag-kv/flag_json_test.go
+59
-0
helper/flag-kv/test-fixtures/basic.json
helper/flag-kv/test-fixtures/basic.json
+3
-0
No files found.
helper/flag-kv/flag_json.go
0 → 100644
View file @
7f78a2c5
package
kvflag
import
(
"encoding/json"
"fmt"
"os"
)
// FlagJSON is a flag.Value implementation for parsing user variables
// from the command-line using JSON files.
type
FlagJSON
map
[
string
]
string
func
(
v
*
FlagJSON
)
String
()
string
{
return
""
}
func
(
v
*
FlagJSON
)
Set
(
raw
string
)
error
{
f
,
err
:=
os
.
Open
(
raw
)
if
err
!=
nil
{
return
err
}
defer
f
.
Close
()
if
*
v
==
nil
{
*
v
=
make
(
map
[
string
]
string
)
}
if
err
:=
json
.
NewDecoder
(
f
)
.
Decode
(
v
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Error reading variables in '%s': %s"
,
raw
,
err
)
}
return
nil
}
helper/flag-kv/flag_json_test.go
0 → 100644
View file @
7f78a2c5
package
kvflag
import
(
"flag"
"path/filepath"
"reflect"
"testing"
)
func
TestFlagJSON_impl
(
t
*
testing
.
T
)
{
var
_
flag
.
Value
=
new
(
FlagJSON
)
}
func
TestFlagJSON
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
Input
string
Initial
map
[
string
]
string
Output
map
[
string
]
string
Error
bool
}{
{
"basic.json"
,
nil
,
map
[
string
]
string
{
"key"
:
"value"
},
false
,
},
{
"basic.json"
,
map
[
string
]
string
{
"foo"
:
"bar"
},
map
[
string
]
string
{
"foo"
:
"bar"
,
"key"
:
"value"
},
false
,
},
{
"basic.json"
,
map
[
string
]
string
{
"key"
:
"bar"
},
map
[
string
]
string
{
"key"
:
"value"
},
false
,
},
}
for
_
,
tc
:=
range
cases
{
f
:=
new
(
FlagJSON
)
if
tc
.
Initial
!=
nil
{
f
=
(
*
FlagJSON
)(
&
tc
.
Initial
)
}
err
:=
f
.
Set
(
filepath
.
Join
(
"./test-fixtures"
,
tc
.
Input
))
if
(
err
!=
nil
)
!=
tc
.
Error
{
t
.
Fatalf
(
"bad error. Input: %#v
\n\n
%s"
,
tc
.
Input
,
err
)
}
actual
:=
map
[
string
]
string
(
*
f
)
if
!
reflect
.
DeepEqual
(
actual
,
tc
.
Output
)
{
t
.
Fatalf
(
"bad: %#v"
,
actual
)
}
}
}
helper/flag-kv/test-fixtures/basic.json
0 → 100644
View file @
7f78a2c5
{
"key"
:
"value"
}
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