Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
7228c8fe
提交
7228c8fe
authored
11月 05, 2014
作者:
Matt Bell
提交者:
Juan Batiz-Benet
11月 14, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
core/commands2: Added 'config' command
上级
cb72868a
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
189 行增加
和
0 行删除
+189
-0
config.go
core/commands2/config.go
+188
-0
root.go
core/commands2/root.go
+1
-0
没有找到文件。
core/commands2/config.go
0 → 100644
浏览文件 @
7228c8fe
package
commands
import
(
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
cmds
"github.com/jbenet/go-ipfs/commands"
config
"github.com/jbenet/go-ipfs/config"
)
type
ConfigField
struct
{
Key
string
Value
interface
{}
}
var
configCmd
=
&
cmds
.
Command
{
Arguments
:
[]
cmds
.
Argument
{
cmds
.
Argument
{
"key"
,
cmds
.
ArgString
,
true
,
false
},
cmds
.
Argument
{
"value"
,
cmds
.
ArgString
,
false
,
false
},
},
Help
:
`ipfs config <key> [value] - Get/Set ipfs config values.
ipfs config <key> - Get value of <key>
ipfs config <key> <value> - Set value of <key> to <value>
ipfs config show - Show config file
ipfs config edit - Edit config file in $EDITOR
Examples:
Get the value of the 'datastore.path' key:
ipfs config datastore.path
Set the value of the 'datastore.path' key:
ipfs config datastore.path ~/.go-ipfs/datastore
`
,
Run
:
func
(
res
cmds
.
Response
,
req
cmds
.
Request
)
{
args
:=
req
.
Arguments
()
key
,
ok
:=
args
[
0
]
.
(
string
)
if
!
ok
{
res
.
SetError
(
errors
.
New
(
"cast error"
),
cmds
.
ErrNormal
)
return
}
filename
,
err
:=
config
.
Filename
(
req
.
Context
()
.
ConfigRoot
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
var
value
string
if
len
(
args
)
==
2
{
var
ok
bool
value
,
ok
=
args
[
1
]
.
(
string
)
if
!
ok
{
res
.
SetError
(
errors
.
New
(
"cast error"
),
cmds
.
ErrNormal
)
return
}
field
,
err
:=
setConfig
(
filename
,
key
,
value
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
res
.
SetOutput
(
field
)
return
}
else
{
field
,
err
:=
getConfig
(
filename
,
key
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
res
.
SetOutput
(
field
)
return
}
},
Marshallers
:
map
[
cmds
.
EncodingType
]
cmds
.
Marshaller
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
([]
byte
,
error
)
{
v
:=
res
.
Output
()
.
(
*
ConfigField
)
s
:=
""
if
len
(
res
.
Request
()
.
Arguments
())
==
2
{
s
+=
fmt
.
Sprintf
(
"'%s' set to: "
,
v
.
Key
)
}
marshalled
,
err
:=
json
.
Marshal
(
v
.
Value
)
if
err
!=
nil
{
return
nil
,
err
}
s
+=
fmt
.
Sprintf
(
"%s
\n
"
,
marshalled
)
return
[]
byte
(
s
),
nil
},
},
Type
:
&
ConfigField
{},
Subcommands
:
map
[
string
]
*
cmds
.
Command
{
"show"
:
configShowCmd
,
"edit"
:
configEditCmd
,
},
}
var
configShowCmd
=
&
cmds
.
Command
{
Run
:
func
(
res
cmds
.
Response
,
req
cmds
.
Request
)
{
filename
,
err
:=
config
.
Filename
(
req
.
Context
()
.
ConfigRoot
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
reader
,
err
:=
showConfig
(
filename
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
res
.
SetOutput
(
reader
)
},
}
var
configEditCmd
=
&
cmds
.
Command
{
Run
:
func
(
res
cmds
.
Response
,
req
cmds
.
Request
)
{
filename
,
err
:=
config
.
Filename
(
req
.
Context
()
.
ConfigRoot
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
err
=
editConfig
(
filename
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmds
.
ErrNormal
)
return
}
},
}
func
getConfig
(
filename
string
,
key
string
)
(
*
ConfigField
,
error
)
{
value
,
err
:=
config
.
ReadConfigKey
(
filename
,
key
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Failed to get config value: %s"
,
err
)
}
return
&
ConfigField
{
Key
:
key
,
Value
:
value
,
},
nil
}
func
setConfig
(
filename
string
,
key
,
value
string
)
(
*
ConfigField
,
error
)
{
err
:=
config
.
WriteConfigKey
(
filename
,
key
,
value
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Failed to set config value: %s"
,
err
)
}
return
getConfig
(
filename
,
key
)
}
func
showConfig
(
filename
string
)
(
io
.
Reader
,
error
)
{
// MAYBE_TODO: maybe we should omit privkey so we don't accidentally leak it?
file
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
return
nil
,
err
}
//defer file.Close()
return
file
,
nil
}
func
editConfig
(
filename
string
)
error
{
editor
:=
os
.
Getenv
(
"EDITOR"
)
if
editor
==
""
{
return
errors
.
New
(
"ENV variable $EDITOR not set"
)
}
cmd
:=
exec
.
Command
(
"sh"
,
"-c"
,
editor
+
" "
+
filename
)
cmd
.
Stdin
,
cmd
.
Stdout
,
cmd
.
Stderr
=
os
.
Stdin
,
os
.
Stdout
,
os
.
Stderr
return
cmd
.
Run
()
}
core/commands2/root.go
浏览文件 @
7228c8fe
...
...
@@ -66,6 +66,7 @@ var rootSubcommands = map[string]*cmds.Command{
"pin"
:
pinCmd
,
"unpin"
:
unpinCmd
,
"version"
:
versionCmd
,
"config"
:
configCmd
,
// test subcommands
// TODO: remove these when we don't need them anymore
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论