Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
5f3945c9
提交
5f3945c9
authored
3月 04, 2016
作者:
Jeromy Johnson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2443 from ipfs/feat/default-option-vals
add default option value support to commands lib
上级
39c101cd
767ee13e
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
33 行增加
和
27 行删除
+33
-27
option.go
commands/option.go
+28
-8
option_test.go
commands/option_test.go
+0
-7
request.go
commands/request.go
+1
-2
pin.go
core/commands/pin.go
+4
-10
没有找到文件。
commands/option.go
浏览文件 @
5f3945c9
package
commands
import
(
"fmt"
"reflect"
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
...
...
@@ -18,15 +19,18 @@ const (
// Option is used to specify a field that will be provided by a consumer
type
Option
interface
{
Names
()
[]
string
// a list of unique names matched with user-provided flags
Type
()
reflect
.
Kind
// value must be this type
Description
()
string
// a short string that describes this option
Names
()
[]
string
// a list of unique names matched with user-provided flags
Type
()
reflect
.
Kind
// value must be this type
Description
()
string
// a short string that describes this option
Default
(
interface
{})
Option
// sets the default value of the option
DefaultVal
()
interface
{}
}
type
option
struct
{
names
[]
string
kind
reflect
.
Kind
description
string
defaultVal
interface
{}
}
func
(
o
*
option
)
Names
()
[]
string
{
...
...
@@ -38,6 +42,13 @@ func (o *option) Type() reflect.Kind {
}
func
(
o
*
option
)
Description
()
string
{
if
o
.
description
[
len
(
o
.
description
)
-
1
]
!=
'.'
{
o
.
description
+=
"."
}
if
o
.
defaultVal
!=
nil
{
return
fmt
.
Sprintf
(
"%s Default: %v."
,
o
.
description
,
o
.
defaultVal
)
}
return
o
.
description
}
...
...
@@ -58,6 +69,15 @@ func NewOption(kind reflect.Kind, names ...string) Option {
}
}
func
(
o
*
option
)
Default
(
v
interface
{})
Option
{
o
.
defaultVal
=
v
return
o
}
func
(
o
*
option
)
DefaultVal
()
interface
{}
{
return
o
.
defaultVal
}
// TODO handle description separately. this will take care of the panic case in
// NewOption
...
...
@@ -98,7 +118,7 @@ func (ov OptionValue) Definition() Option {
// value accessor methods, gets the value as a certain type
func
(
ov
OptionValue
)
Bool
()
(
value
bool
,
found
bool
,
err
error
)
{
if
!
ov
.
found
{
if
!
ov
.
found
&&
ov
.
value
==
nil
{
return
false
,
false
,
nil
}
val
,
ok
:=
ov
.
value
.
(
bool
)
...
...
@@ -109,7 +129,7 @@ func (ov OptionValue) Bool() (value bool, found bool, err error) {
}
func
(
ov
OptionValue
)
Int
()
(
value
int
,
found
bool
,
err
error
)
{
if
!
ov
.
found
{
if
!
ov
.
found
&&
ov
.
value
==
nil
{
return
0
,
false
,
nil
}
val
,
ok
:=
ov
.
value
.
(
int
)
...
...
@@ -120,7 +140,7 @@ func (ov OptionValue) Int() (value int, found bool, err error) {
}
func
(
ov
OptionValue
)
Uint
()
(
value
uint
,
found
bool
,
err
error
)
{
if
!
ov
.
found
{
if
!
ov
.
found
&&
ov
.
value
==
nil
{
return
0
,
false
,
nil
}
val
,
ok
:=
ov
.
value
.
(
uint
)
...
...
@@ -131,7 +151,7 @@ func (ov OptionValue) Uint() (value uint, found bool, err error) {
}
func
(
ov
OptionValue
)
Float
()
(
value
float64
,
found
bool
,
err
error
)
{
if
!
ov
.
found
{
if
!
ov
.
found
&&
ov
.
value
==
nil
{
return
0
,
false
,
nil
}
val
,
ok
:=
ov
.
value
.
(
float64
)
...
...
@@ -142,7 +162,7 @@ func (ov OptionValue) Float() (value float64, found bool, err error) {
}
func
(
ov
OptionValue
)
String
()
(
value
string
,
found
bool
,
err
error
)
{
if
!
ov
.
found
{
if
!
ov
.
found
&&
ov
.
value
==
nil
{
return
""
,
false
,
nil
}
val
,
ok
:=
ov
.
value
.
(
string
)
...
...
commands/option_test.go
浏览文件 @
5f3945c9
...
...
@@ -9,13 +9,6 @@ func TestOptionValueExtractBoolNotFound(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
"Found was false. Err should have been nil"
)
}
t
.
Log
(
"ensure that no error is returned when value is not found (even if value exists)"
)
optval
=
&
OptionValue
{
value
:
"wrong type: a string"
,
found
:
false
}
_
,
_
,
err
=
optval
.
Bool
()
if
err
!=
nil
{
t
.
Fatal
(
"Found was false. Err should have been nil"
)
}
}
func
TestOptionValueExtractWrongType
(
t
*
testing
.
T
)
{
...
...
commands/request.go
浏览文件 @
5f3945c9
...
...
@@ -118,8 +118,7 @@ func (r *request) Option(name string) *OptionValue {
}
}
// MAYBE_TODO: use default value instead of nil
return
&
OptionValue
{
nil
,
false
,
option
}
return
&
OptionValue
{
option
.
DefaultVal
(),
false
,
option
}
}
// Options returns a copy of the option map
...
...
core/commands/pin.go
浏览文件 @
5f3945c9
...
...
@@ -41,7 +41,7 @@ var addPinCmd = &cmds.Command{
cmds
.
StringArg
(
"ipfs-path"
,
true
,
true
,
"Path to object(s) to be pinned."
)
.
EnableStdin
(),
},
Options
:
[]
cmds
.
Option
{
cmds
.
BoolOption
(
"recursive"
,
"r"
,
"Recursively pin the object linked to by the specified object(s)."
),
cmds
.
BoolOption
(
"recursive"
,
"r"
,
"Recursively pin the object linked to by the specified object(s)."
)
.
Default
(
true
)
,
},
Type
:
PinOutput
{},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
...
...
@@ -54,14 +54,11 @@ var addPinCmd = &cmds.Command{
defer
n
.
Blockstore
.
PinLock
()
.
Unlock
()
// set recursive flag
recursive
,
found
,
err
:=
req
.
Option
(
"recursive"
)
.
Bool
()
recursive
,
_
,
err
:=
req
.
Option
(
"recursive"
)
.
Bool
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
if
!
found
{
recursive
=
true
}
added
,
err
:=
corerepo
.
Pin
(
n
,
req
.
Context
(),
req
.
Arguments
(),
recursive
)
if
err
!=
nil
{
...
...
@@ -108,7 +105,7 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
cmds
.
StringArg
(
"ipfs-path"
,
true
,
true
,
"Path to object(s) to be unpinned."
)
.
EnableStdin
(),
},
Options
:
[]
cmds
.
Option
{
cmds
.
BoolOption
(
"recursive"
,
"r"
,
"Recursively unpin the object linked to by the specified object(s)."
),
cmds
.
BoolOption
(
"recursive"
,
"r"
,
"Recursively unpin the object linked to by the specified object(s)."
)
.
Default
(
true
)
,
},
Type
:
PinOutput
{},
Run
:
func
(
req
cmds
.
Request
,
res
cmds
.
Response
)
{
...
...
@@ -119,14 +116,11 @@ collected if needed. (By default, recursively. Use -r=false for direct pins)
}
// set recursive flag
recursive
,
found
,
err
:=
req
.
Option
(
"recursive"
)
.
Bool
()
recursive
,
_
,
err
:=
req
.
Option
(
"recursive"
)
.
Bool
()
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
if
!
found
{
recursive
=
true
// default
}
removed
,
err
:=
corerepo
.
Unpin
(
n
,
req
.
Context
(),
req
.
Arguments
(),
recursive
)
if
err
!=
nil
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论