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-cmds
Commits
58597eb0
Commit
58597eb0
authored
Oct 10, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Oct 20, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands: Updated Command to use Response for output rather than (interface{}, error)
parent
56f43338
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
9 deletions
+16
-9
command.go
command.go
+16
-9
No files found.
command.go
View file @
58597eb0
...
...
@@ -9,7 +9,7 @@ import (
type
Command
struct
{
Help
string
Options
[]
Option
f
func
(
*
Request
)
(
interface
{},
error
)
f
func
(
*
Request
,
*
Response
)
subcommands
map
[
string
]
*
Command
}
...
...
@@ -37,11 +37,12 @@ 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
)
(
interface
{},
error
)
{
func
(
c
*
Command
)
Call
(
path
[]
string
,
req
*
Request
)
*
Response
{
options
:=
make
([]
Option
,
len
(
c
.
Options
))
copy
(
options
,
c
.
Options
)
options
=
append
(
options
,
globalOptions
...
)
cmd
:=
c
res
:=
&
Response
{
req
:
req
}
if
path
!=
nil
{
for
i
,
id
:=
range
path
{
...
...
@@ -49,7 +50,8 @@ func (c *Command) Call(path []string, req *Request) (interface{}, error) {
if
cmd
==
nil
{
pathS
:=
strings
.
Join
(
path
[
0
:
i
],
"/"
)
return
nil
,
fmt
.
Errorf
(
"Undefined command: '%s'"
,
pathS
)
res
.
SetError
(
fmt
.
Errorf
(
"Undefined command: '%s'"
,
pathS
),
Client
)
return
res
}
options
=
append
(
options
,
cmd
.
Options
...
)
...
...
@@ -67,24 +69,29 @@ func (c *Command) Call(path []string, req *Request) (interface{}, error) {
opt
,
ok
:=
optionsMap
[
k
]
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"Unrecognized command option: '%s'"
,
k
)
res
.
SetError
(
fmt
.
Errorf
(
"Unrecognized command option: '%s'"
,
k
),
Client
)
return
res
}
for
_
,
name
:=
range
opt
.
Names
{
if
_
,
ok
=
req
.
options
[
name
];
name
!=
k
&&
ok
{
return
nil
,
fmt
.
Errorf
(
"Duplicate command options were provided ('%s' and '%s')"
,
k
,
name
)
res
.
SetError
(
fmt
.
Errorf
(
"Duplicate command options were provided ('%s' and '%s')"
,
k
,
name
),
Client
)
return
res
}
}
kind
:=
reflect
.
TypeOf
(
v
)
.
Kind
()
if
kind
!=
opt
.
Type
{
return
nil
,
fmt
.
Errorf
(
"Option '%s' should be type '%s', but got type '%s'"
,
k
,
opt
.
Type
.
String
(),
kind
.
String
())
res
.
SetError
(
fmt
.
Errorf
(
"Option '%s' should be type '%s', but got type '%s'"
,
k
,
opt
.
Type
.
String
(),
kind
.
String
()),
Client
)
return
res
}
}
return
cmd
.
f
(
req
)
cmd
.
f
(
req
,
res
)
return
res
}
// Sub returns the subcommand with the given id
...
...
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