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
f60867f3
Commit
f60867f3
authored
Oct 20, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
f -> run, Function type.
parent
fa7d1704
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
12 deletions
+18
-12
command.go
command.go
+12
-6
command_test.go
command_test.go
+6
-6
No files found.
command.go
View file @
f60867f3
...
...
@@ -6,11 +6,17 @@ import (
"strings"
)
// Command is an object that defines a command.
// Function is the type of function that Commands use.
// It reads from the Request, and writes results to the Response.
type
Function
func
(
*
Request
,
*
Response
)
// Command is a runnable command, with input arguments and options (flags).
// It can also have subcommands, to group units of work into sets.
type
Command
struct
{
Help
string
Options
[]
Option
f
func
(
*
Request
,
*
Response
)
Help
string
Options
[]
Option
run
Function
subcommands
map
[
string
]
*
Command
}
...
...
@@ -51,7 +57,7 @@ func (c *Command) Call(req *Request) *Response {
}
cmd
:=
cmds
[
len
(
cmds
)
-
1
]
if
cmd
.
f
==
nil
{
if
cmd
.
run
==
nil
{
res
.
SetError
(
ErrNotCallable
,
ErrClient
)
return
res
}
...
...
@@ -68,7 +74,7 @@ func (c *Command) Call(req *Request) *Response {
return
res
}
cmd
.
f
(
req
,
res
)
cmd
.
run
(
req
,
res
)
return
res
}
...
...
command_test.go
View file @
f60867f3
...
...
@@ -8,7 +8,7 @@ func TestOptionValidation(t *testing.T) {
Option
{[]
string
{
"b"
,
"beep"
},
Int
},
Option
{[]
string
{
"B"
,
"boop"
},
String
},
},
f
:
func
(
req
*
Request
,
res
*
Response
)
{},
run
:
func
(
req
*
Request
,
res
*
Response
)
{},
}
req
:=
NewEmptyRequest
()
...
...
@@ -84,35 +84,35 @@ func TestRegistration(t *testing.T) {
Options
:
[]
Option
{
Option
{[]
string
{
"beep"
},
Int
},
},
f
:
func
(
req
*
Request
,
res
*
Response
)
{},
run
:
func
(
req
*
Request
,
res
*
Response
)
{},
},
&
Command
{
Options
:
[]
Option
{
Option
{[]
string
{
"boop"
},
Int
},
},
f
:
func
(
req
*
Request
,
res
*
Response
)
{},
run
:
func
(
req
*
Request
,
res
*
Response
)
{},
},
&
Command
{
Options
:
[]
Option
{
Option
{[]
string
{
"boop"
},
String
},
},
f
:
func
(
req
*
Request
,
res
*
Response
)
{},
run
:
func
(
req
*
Request
,
res
*
Response
)
{},
},
&
Command
{
Options
:
[]
Option
{
Option
{[]
string
{
"bop"
},
String
},
},
f
:
func
(
req
*
Request
,
res
*
Response
)
{},
run
:
func
(
req
*
Request
,
res
*
Response
)
{},
},
&
Command
{
Options
:
[]
Option
{
Option
{[]
string
{
"enc"
},
String
},
},
f
:
func
(
req
*
Request
,
res
*
Response
)
{},
run
:
func
(
req
*
Request
,
res
*
Response
)
{},
},
}
...
...
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