Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
4f06c6fd
提交
4f06c6fd
authored
10月 15, 2014
作者:
Matt Bell
提交者:
Juan Batiz-Benet
10月 20, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commands: Formatted code
上级
d2176c05
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
68 行增加
和
68 行删除
+68
-68
command.go
commands/command.go
+57
-57
command_test.go
commands/command_test.go
+2
-3
request.go
commands/request.go
+9
-8
没有找到文件。
commands/command.go
浏览文件 @
4f06c6fd
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
...
...
commands/command_test.go
浏览文件 @
4f06c6fd
...
...
@@ -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
}
commands/request.go
浏览文件 @
4f06c6fd
...
...
@@ -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
{})
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论