Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
793a8de9
Commit
793a8de9
authored
Oct 14, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Oct 20, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands: Refactored to make Request contain command path
parent
86bc450b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
9 deletions
+16
-9
commands/command.go
commands/command.go
+3
-2
commands/command_test.go
commands/command_test.go
+7
-7
commands/request.go
commands/request.go
+6
-0
No files found.
commands/command.go
View file @
793a8de9
...
...
@@ -37,11 +37,11 @@ func (c *Command) Register(id string, sub *Command) error {
}
// Call invokes the command at the given subcommand path
func
(
c
*
Command
)
Call
(
path
[]
string
,
req
*
Request
)
*
Response
{
func
(
c
*
Command
)
Call
(
req
*
Request
)
*
Response
{
cmd
:=
c
res
:=
&
Response
{
req
:
req
}
options
,
err
:=
cmd
.
GetOptions
(
path
)
options
,
err
:=
cmd
.
GetOptions
(
req
.
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
return
res
...
...
@@ -82,6 +82,7 @@ func (c *Command) GetOptions(path []string) (map[string]Option, error) {
copy
(
options
,
c
.
Options
)
options
=
append
(
options
,
globalOptions
...
)
// a nil path means this command, not a subcommand (same as an empty path)
if
path
!=
nil
{
for
i
,
id
:=
range
path
{
cmd
:=
c
.
Sub
(
id
)
...
...
commands/command_test.go
View file @
793a8de9
...
...
@@ -13,7 +13,7 @@ func TestOptionValidation(t *testing.T) {
req
:=
NewRequest
()
req
.
options
[
"foo"
]
=
5
res
:=
cmd
.
Call
(
nil
,
req
)
res
:=
cmd
.
Call
(
req
)
if
res
.
Error
==
nil
{
t
.
Error
(
"Should have failed (unrecognized command)"
)
}
...
...
@@ -21,21 +21,21 @@ func TestOptionValidation(t *testing.T) {
req
=
NewRequest
()
req
.
options
[
"beep"
]
=
5
req
.
options
[
"b"
]
=
10
res
=
cmd
.
Call
(
nil
,
req
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
==
nil
{
t
.
Error
(
"Should have failed (duplicate options)"
)
}
req
=
NewRequest
()
req
.
options
[
"beep"
]
=
"foo"
res
=
cmd
.
Call
(
nil
,
req
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
==
nil
{
t
.
Error
(
"Should have failed (incorrect type)"
)
}
req
=
NewRequest
()
req
.
options
[
"beep"
]
=
5
res
=
cmd
.
Call
(
nil
,
req
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
!=
nil
{
t
.
Error
(
"Should have passed"
)
}
...
...
@@ -43,7 +43,7 @@ func TestOptionValidation(t *testing.T) {
req
=
NewRequest
()
req
.
options
[
"beep"
]
=
5
req
.
options
[
"boop"
]
=
"test"
res
=
cmd
.
Call
(
nil
,
req
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
!=
nil
{
t
.
Error
(
"Should have passed"
)
}
...
...
@@ -51,14 +51,14 @@ func TestOptionValidation(t *testing.T) {
req
=
NewRequest
()
req
.
options
[
"b"
]
=
5
req
.
options
[
"B"
]
=
"test"
res
=
cmd
.
Call
(
nil
,
req
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
!=
nil
{
t
.
Error
(
"Should have passed"
)
}
req
=
NewRequest
()
req
.
options
[
"enc"
]
=
"json"
res
=
cmd
.
Call
(
nil
,
req
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
!=
nil
{
t
.
Error
(
"Should have passed"
)
}
...
...
commands/request.go
View file @
793a8de9
...
...
@@ -2,10 +2,15 @@ package commands
// Request represents a call to a command from a consumer
type
Request
struct
{
path
[]
string
options
map
[
string
]
interface
{}
arguments
[]
string
}
func
(
r
*
Request
)
Path
()
[]
string
{
return
r
.
path
}
func
(
r
*
Request
)
Option
(
name
string
)
interface
{}
{
return
r
.
options
[
name
]
}
...
...
@@ -24,6 +29,7 @@ func (r *Request) Arguments() []string {
func
NewRequest
()
*
Request
{
return
&
Request
{
make
([]
string
,
0
),
make
(
map
[
string
]
interface
{}),
make
([]
string
,
0
),
}
...
...
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