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
c22a764c
Commit
c22a764c
authored
Oct 15, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Oct 20, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands: Formatted code
parent
de86c91b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
67 deletions
+68
-67
command.go
command.go
+57
-57
command_test.go
command_test.go
+2
-2
request.go
request.go
+9
-8
No files found.
command.go
View file @
c22a764c
package
commands
import
(
"errors"
"fmt"
"strings"
"errors"
)
type
Command
struct
{
...
...
@@ -42,29 +42,29 @@ func (c *Command) Register(id string, sub *Command) error {
func
(
c
*
Command
)
Call
(
req
*
Request
)
*
Response
{
res
:=
&
Response
{
req
:
req
}
cmds
,
err
:=
c
.
Resolve
(
req
.
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
return
res
}
cmd
:=
cmds
[
len
(
cmds
)
-
1
]
cmds
,
err
:=
c
.
Resolve
(
req
.
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
return
res
}
cmd
:=
cmds
[
len
(
cmds
)
-
1
]
if
(
cmd
.
f
==
nil
)
{
res
.
SetError
(
NotCallableError
,
Client
)
return
res
}
if
cmd
.
f
==
nil
{
res
.
SetError
(
NotCallableError
,
Client
)
return
res
}
options
,
err
:=
c
.
GetOptions
(
req
.
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
return
res
}
options
,
err
:=
c
.
GetOptions
(
req
.
path
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
return
res
}
err
=
req
.
convertOptions
(
options
)
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
return
res
}
if
err
!=
nil
{
res
.
SetError
(
err
,
Client
)
return
res
}
cmd
.
f
(
req
,
res
)
...
...
@@ -73,54 +73,54 @@ func (c *Command) Call(req *Request) *Response {
// Resolve gets the subcommands at the given path
func
(
c
*
Command
)
Resolve
(
path
[]
string
)
([]
*
Command
,
error
)
{
cmds
:=
make
([]
*
Command
,
len
(
path
)
+
1
)
cmds
[
0
]
=
c
cmds
:=
make
([]
*
Command
,
len
(
path
)
+
1
)
cmds
[
0
]
=
c
cmd
:=
c
for
i
,
name
:=
range
path
{
cmd
=
cmd
.
Sub
(
name
)
cmd
:=
c
for
i
,
name
:=
range
path
{
cmd
=
cmd
.
Sub
(
name
)
if
cmd
==
nil
{
pathS
:=
strings
.
Join
(
path
[
0
:
i
],
"/"
)
return
nil
,
fmt
.
Errorf
(
"Undefined command: '%s'"
,
pathS
)
}
if
cmd
==
nil
{
pathS
:=
strings
.
Join
(
path
[
0
:
i
],
"/"
)
return
nil
,
fmt
.
Errorf
(
"Undefined command: '%s'"
,
pathS
)
}
cmds
[
i
+
1
]
=
cmd
}
cmds
[
i
+
1
]
=
cmd
}
return
cmds
,
nil
return
cmds
,
nil
}
func
(
c
*
Command
)
Get
(
path
[]
string
)
(
*
Command
,
error
)
{
cmds
,
err
:=
c
.
Resolve
(
path
)
if
err
!=
nil
{
return
nil
,
err
}
return
cmds
[
len
(
cmds
)
-
1
],
nil
cmds
,
err
:=
c
.
Resolve
(
path
)
if
err
!=
nil
{
return
nil
,
err
}
return
cmds
[
len
(
cmds
)
-
1
],
nil
}
// GetOptions gets the options in the given path of commands
func
(
c
*
Command
)
GetOptions
(
path
[]
string
)
(
map
[
string
]
Option
,
error
)
{
options
:=
make
([]
Option
,
len
(
c
.
Options
))
copy
(
options
,
c
.
Options
)
options
=
append
(
options
,
globalOptions
...
)
cmds
,
err
:=
c
.
Resolve
(
path
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
cmd
:=
range
cmds
{
options
=
append
(
options
,
cmd
.
Options
...
)
}
optionsMap
:=
make
(
map
[
string
]
Option
)
for
_
,
opt
:=
range
options
{
for
_
,
name
:=
range
opt
.
Names
{
optionsMap
[
name
]
=
opt
}
}
return
optionsMap
,
nil
options
:=
make
([]
Option
,
len
(
c
.
Options
))
copy
(
options
,
c
.
Options
)
options
=
append
(
options
,
globalOptions
...
)
cmds
,
err
:=
c
.
Resolve
(
path
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
cmd
:=
range
cmds
{
options
=
append
(
options
,
cmd
.
Options
...
)
}
optionsMap
:=
make
(
map
[
string
]
Option
)
for
_
,
opt
:=
range
options
{
for
_
,
name
:=
range
opt
.
Names
{
optionsMap
[
name
]
=
opt
}
}
return
optionsMap
,
nil
}
// Sub returns the subcommand with the given id
...
...
command_test.go
View file @
c22a764c
...
...
@@ -149,11 +149,11 @@ func TestResolving(t *testing.T) {
cmdA
.
Register
(
"b"
,
cmdB
)
cmdB
.
Register
(
"c"
,
cmdC
)
cmds
,
err
:=
cmd
.
Resolve
([]
string
{
"a"
,
"b"
,
"c"
})
cmds
,
err
:=
cmd
.
Resolve
([]
string
{
"a"
,
"b"
,
"c"
})
if
err
!=
nil
{
t
.
Error
(
err
)
}
if
len
(
cmds
)
!=
4
||
cmds
[
0
]
!=
cmd
||
cmds
[
1
]
!=
cmdA
||
cmds
[
2
]
!=
cmdB
||
cmds
[
3
]
!=
cmdC
{
t
.
Error
(
"Returned command path is different than expected"
,
cmds
)
}
}
\ No newline at end of file
}
request.go
View file @
c22a764c
...
...
@@ -8,7 +8,7 @@ import (
// Request represents a call to a command from a consumer
type
Request
struct
{
path
[]
string
path
[]
string
options
map
[
string
]
interface
{}
arguments
[]
string
}
...
...
@@ -33,21 +33,22 @@ func (r *Request) Arguments() []string {
return
r
.
arguments
}
type
converter
func
(
string
)(
interface
{},
error
)
type
converter
func
(
string
)
(
interface
{},
error
)
var
converters
map
[
reflect
.
Kind
]
converter
=
map
[
reflect
.
Kind
]
converter
{
Bool
:
func
(
v
string
)(
interface
{},
error
)
{
Bool
:
func
(
v
string
)
(
interface
{},
error
)
{
if
v
==
""
{
return
true
,
nil
}
return
strconv
.
ParseBool
(
v
)
},
Int
:
func
(
v
string
)(
interface
{},
error
)
{
Int
:
func
(
v
string
)
(
interface
{},
error
)
{
return
strconv
.
ParseInt
(
v
,
0
,
32
)
},
Uint
:
func
(
v
string
)(
interface
{},
error
)
{
Uint
:
func
(
v
string
)
(
interface
{},
error
)
{
return
strconv
.
ParseInt
(
v
,
0
,
32
)
},
Float
:
func
(
v
string
)(
interface
{},
error
)
{
Float
:
func
(
v
string
)
(
interface
{},
error
)
{
return
strconv
.
ParseFloat
(
v
,
64
)
},
}
...
...
@@ -88,7 +89,7 @@ func (r *Request) convertOptions(options map[string]Option) error {
k
,
name
)
}
converted
[
name
]
=
value
converted
[
name
]
=
value
}
}
...
...
@@ -102,7 +103,7 @@ func NewEmptyRequest() *Request {
func
NewRequest
(
path
[]
string
,
opts
map
[
string
]
interface
{},
args
[]
string
)
*
Request
{
if
path
==
nil
{
path
=
make
([]
string
,
0
)
path
=
make
([]
string
,
0
)
}
if
opts
==
nil
{
opts
=
make
(
map
[
string
]
interface
{})
...
...
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