Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
81f29257
提交
81f29257
authored
10月 28, 2014
作者:
Matt Bell
提交者:
Juan Batiz-Benet
11月 04, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commands: Gave Requests a reference to the command they are being called on
上级
507192ef
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
30 行增加
和
14 行删除
+30
-14
parse.go
commands/cli/parse.go
+12
-8
parse.go
commands/http/parse.go
+9
-3
request.go
commands/request.go
+9
-3
没有找到文件。
commands/cli/parse.go
浏览文件 @
81f29257
...
...
@@ -4,23 +4,26 @@ import (
"fmt"
"strings"
"github.com/jbenet/go-ipfs/commands"
cmds
"github.com/jbenet/go-ipfs/commands"
)
// Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object.
func
Parse
(
input
[]
string
,
root
*
c
ommands
.
Command
)
(
comman
ds
.
Request
,
error
)
{
path
,
input
:=
parsePath
(
input
,
root
)
func
Parse
(
input
[]
string
,
root
*
c
mds
.
Command
)
(
cm
ds
.
Request
,
error
)
{
path
,
input
,
cmd
:=
parsePath
(
input
,
root
)
opts
,
args
,
err
:=
parseOptions
(
input
)
if
err
!=
nil
{
return
nil
,
err
}
return
commands
.
NewRequest
(
path
,
opts
,
args
,
nil
),
nil
// TODO: figure out how to know when to read given file(s) as an input stream
// (instead of filename arg string)
return
cmds
.
NewRequest
(
path
,
opts
,
args
,
nil
,
cmd
),
nil
}
// parsePath gets the command path from the command line input
func
parsePath
(
input
[]
string
,
root
*
c
ommands
.
Command
)
([]
string
,
[]
string
)
{
func
parsePath
(
input
[]
string
,
root
*
c
mds
.
Command
)
([]
string
,
[]
string
,
*
cmds
.
Command
)
{
cmd
:=
root
i
:=
0
...
...
@@ -29,15 +32,16 @@ func parsePath(input []string, root *commands.Command) ([]string, []string) {
break
}
cmd
:=
cmd
.
Subcommand
(
blob
)
if
cmd
==
nil
{
sub
:=
cmd
.
Subcommand
(
blob
)
if
sub
==
nil
{
break
}
cmd
=
sub
i
++
}
return
input
[
:
i
],
input
[
i
:
]
return
input
[
:
i
],
input
[
i
:
]
,
cmd
}
// parseOptions parses the raw string values of the given options
...
...
commands/http/parse.go
浏览文件 @
81f29257
...
...
@@ -10,18 +10,24 @@ import (
// Parse parses the data in a http.Request and returns a command Request object
func
Parse
(
r
*
http
.
Request
)
(
cmds
.
Request
,
error
)
{
// TODO: take root cmd as a param, like the commands/cli Parse
path
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)[
3
:
]
args
:=
make
([]
string
,
0
)
if
cmd
,
err
:=
commands
.
Root
.
Get
(
path
[
:
len
(
path
)
-
1
]);
err
!=
nil
{
cmd
,
err
:=
commands
.
Root
.
Get
(
path
[
:
len
(
path
)
-
1
])
if
err
!=
nil
{
// 404 if there is no command at that path
return
nil
,
ErrNotFound
}
else
if
cmd
.
Subcommand
(
path
[
len
(
path
)
-
1
])
==
nil
{
}
else
if
sub
:=
cmd
.
Subcommand
(
path
[
len
(
path
)
-
1
]);
sub
==
nil
{
// if the last string in the path isn't a subcommand, use it as an argument
// e.g. /objects/Qabc12345 (we are passing "Qabc12345" to the "objects" command)
args
=
append
(
args
,
path
[
len
(
path
)
-
1
])
path
=
path
[
:
len
(
path
)
-
1
]
}
else
{
cmd
=
sub
}
opts
,
args2
:=
parseOptions
(
r
)
...
...
@@ -33,7 +39,7 @@ func Parse(r *http.Request) (cmds.Request, error) {
// (r.Body will be nil if there is no request body, like in GET requests)
in
:=
r
.
Body
return
cmds
.
NewRequest
(
path
,
opts
,
args
,
in
),
nil
return
cmds
.
NewRequest
(
path
,
opts
,
args
,
in
,
cmd
),
nil
}
func
parseOptions
(
r
*
http
.
Request
)
(
map
[
string
]
interface
{},
[]
string
)
{
...
...
commands/request.go
浏览文件 @
81f29257
...
...
@@ -29,6 +29,7 @@ type Request interface {
SetStream
(
io
.
Reader
)
Context
()
*
Context
SetContext
(
Context
)
Command
()
*
Command
ConvertOptions
(
options
map
[
string
]
Option
)
error
}
...
...
@@ -38,6 +39,7 @@ type request struct {
options
optMap
arguments
[]
string
in
io
.
Reader
cmd
*
Command
ctx
Context
}
...
...
@@ -89,6 +91,10 @@ func (r *request) SetContext(ctx Context) {
r
.
ctx
=
ctx
}
func
(
r
*
request
)
Command
()
*
Command
{
return
r
.
cmd
}
type
converter
func
(
string
)
(
interface
{},
error
)
var
converters
=
map
[
reflect
.
Kind
]
converter
{
...
...
@@ -155,11 +161,11 @@ func (r *request) ConvertOptions(options map[string]Option) error {
// NewEmptyRequest initializes an empty request
func
NewEmptyRequest
()
Request
{
return
NewRequest
(
nil
,
nil
,
nil
,
nil
)
return
NewRequest
(
nil
,
nil
,
nil
,
nil
,
nil
)
}
// NewRequest returns a request initialized with given arguments
func
NewRequest
(
path
[]
string
,
opts
optMap
,
args
[]
string
,
in
io
.
Reader
)
Request
{
func
NewRequest
(
path
[]
string
,
opts
optMap
,
args
[]
string
,
in
io
.
Reader
,
cmd
*
Command
)
Request
{
if
path
==
nil
{
path
=
make
([]
string
,
0
)
}
...
...
@@ -169,5 +175,5 @@ func NewRequest(path []string, opts optMap, args []string, in io.Reader) Request
if
args
==
nil
{
args
=
make
([]
string
,
0
)
}
return
&
request
{
path
,
opts
,
args
,
in
,
Context
{}}
return
&
request
{
path
,
opts
,
args
,
in
,
cmd
,
Context
{}}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论