Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
d3bba25f
Unverified
提交
d3bba25f
authored
10月 24, 2018
作者:
Steven Allen
提交者:
GitHub
10月 24, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5455 from chenminjian/feat/flags/profile-apply
feat: add dry-run flag for config profile apply command
上级
dcecab68
64cdabc8
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
149 行增加
和
11 行删除
+149
-11
config.go
core/commands/config.go
+91
-11
package.json
package.json
+6
-0
t0021-config.sh
test/sharness/t0021-config.sh
+52
-0
没有找到文件。
core/commands/config.go
浏览文件 @
d3bba25f
...
...
@@ -16,10 +16,17 @@ import (
repo
"github.com/ipfs/go-ipfs/repo"
fsrepo
"github.com/ipfs/go-ipfs/repo/fsrepo"
"gx/ipfs/QmP2i47tnU23ijdshrZtuvrSkQPtf9HhsMb9fwGVe8owj2/jsondiff"
config
"gx/ipfs/QmSoYrBMibm2T3LupaLuez7LPGnyrJwdRxvTfPUyCp691u/go-ipfs-config"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
// ConfigUpdateOutput is config profile apply command's output
type
ConfigUpdateOutput
struct
{
OldCfg
map
[
string
]
interface
{}
NewCfg
map
[
string
]
interface
{}
}
type
ConfigField
struct
{
Key
string
Value
interface
{}
...
...
@@ -333,6 +340,9 @@ var configProfileApplyCmd = &cmds.Command{
Helptext
:
cmdkit
.
HelpText
{
Tagline
:
"Apply profile to config."
,
},
Options
:
[]
cmdkit
.
Option
{
cmdkit
.
BoolOption
(
"dry-run"
,
"print difference between the current config and the config that would be generated"
),
},
Arguments
:
[]
cmdkit
.
Argument
{
cmdkit
.
StringArg
(
"profile"
,
true
,
false
,
"The profile to apply to the config."
),
},
...
...
@@ -343,13 +353,53 @@ var configProfileApplyCmd = &cmds.Command{
return
}
err
:=
transformConfig
(
req
.
InvocContext
()
.
ConfigRoot
,
req
.
Arguments
()[
0
],
profile
.
Transform
)
dryRun
,
_
,
_
:=
req
.
Option
(
"dry-run"
)
.
Bool
()
oldCfg
,
newCfg
,
err
:=
transformConfig
(
req
.
InvocContext
()
.
ConfigRoot
,
req
.
Arguments
()[
0
],
profile
.
Transform
,
dryRun
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
res
.
SetOutput
(
nil
)
oldCfgMap
,
err
:=
scrubPrivKey
(
oldCfg
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
newCfgMap
,
err
:=
scrubPrivKey
(
newCfg
)
if
err
!=
nil
{
res
.
SetError
(
err
,
cmdkit
.
ErrNormal
)
return
}
res
.
SetOutput
(
&
ConfigUpdateOutput
{
OldCfg
:
oldCfgMap
,
NewCfg
:
newCfgMap
,
})
},
Marshalers
:
cmds
.
MarshalerMap
{
cmds
.
Text
:
func
(
res
cmds
.
Response
)
(
io
.
Reader
,
error
)
{
if
res
.
Error
()
!=
nil
{
return
nil
,
res
.
Error
()
}
v
,
err
:=
unwrapOutput
(
res
.
Output
())
if
err
!=
nil
{
return
nil
,
err
}
apply
,
ok
:=
v
.
(
*
ConfigUpdateOutput
)
if
!
ok
{
return
nil
,
e
.
TypeErr
(
apply
,
v
)
}
diff
:=
jsondiff
.
Compare
(
apply
.
OldCfg
,
apply
.
NewCfg
)
buf
:=
jsondiff
.
Format
(
diff
)
return
strings
.
NewReader
(
string
(
buf
)),
nil
},
},
Type
:
ConfigUpdateOutput
{},
}
func
buildProfileHelp
()
string
{
...
...
@@ -367,29 +417,59 @@ func buildProfileHelp() string {
return
out
}
func
transformConfig
(
configRoot
string
,
configName
string
,
transformer
config
.
Transformer
)
error
{
// scrubPrivKey scrubs private key for security reasons.
func
scrubPrivKey
(
cfg
*
config
.
Config
)
(
map
[
string
]
interface
{},
error
)
{
cfgMap
,
err
:=
config
.
ToMap
(
cfg
)
if
err
!=
nil
{
return
nil
,
err
}
err
=
scrubValue
(
cfgMap
,
[]
string
{
config
.
IdentityTag
,
config
.
PrivKeyTag
})
if
err
!=
nil
{
return
nil
,
err
}
return
cfgMap
,
nil
}
// transformConfig returns old config and new config instead of difference between they,
// because apply command can provide stable API through this way.
// If dryRun is true, repo's config should not be updated and persisted
// to storage. Otherwise, repo's config should be updated and persisted
// to storage.
func
transformConfig
(
configRoot
string
,
configName
string
,
transformer
config
.
Transformer
,
dryRun
bool
)
(
*
config
.
Config
,
*
config
.
Config
,
error
)
{
r
,
err
:=
fsrepo
.
Open
(
configRoot
)
if
err
!=
nil
{
return
err
return
nil
,
nil
,
err
}
defer
r
.
Close
()
cfg
,
err
:=
r
.
Config
()
if
err
!=
nil
{
return
err
return
nil
,
nil
,
err
}
err
=
transformer
(
cfg
)
// make a copy to avoid updating repo's config unintentionally
oldCfg
:=
*
cfg
newCfg
:=
oldCfg
err
=
transformer
(
&
newCfg
)
if
err
!=
nil
{
return
err
return
nil
,
nil
,
err
}
_
,
err
=
r
.
BackupConfig
(
"pre-"
+
configName
+
"-"
)
if
err
!=
nil
{
return
err
if
!
dryRun
{
_
,
err
=
r
.
BackupConfig
(
"pre-"
+
configName
+
"-"
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
err
=
r
.
SetConfig
(
&
newCfg
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
}
return
r
.
SetConfig
(
cfg
)
return
&
oldCfg
,
&
newCfg
,
nil
}
func
getConfig
(
r
repo
.
Repo
,
key
string
)
(
*
ConfigField
,
error
)
{
...
...
package.json
浏览文件 @
d3bba25f
...
...
@@ -553,6 +553,12 @@
"version"
:
"0.2.4"
},
{
"author"
:
"elgris"
,
"hash"
:
"QmP2i47tnU23ijdshrZtuvrSkQPtf9HhsMb9fwGVe8owj2"
,
"name"
:
"jsondiff"
,
"version"
:
"0.0.0"
},
{
"author"
:
"marten-seemann"
,
"hash"
:
"QmNtLLJMc7TDMSaNuJVFipxLji9NL56QyMnBBPk2X58Mno"
,
"name"
:
"go-libp2p-quic-transport"
,
...
...
test/sharness/t0021-config.sh
浏览文件 @
d3bba25f
...
...
@@ -75,6 +75,17 @@ test_profile_apply_revert() {
'
}
test_profile_apply_dry_run_not_alter
()
{
profile
=
$1
test_expect_success
"'ipfs config profile apply
${
profile
}
--dry-run' doesn't alter config"
'
cat "$IPFS_PATH/config" >expected &&
ipfs config profile apply '
${
profile
}
' --dry-run &&
cat "$IPFS_PATH/config" >actual &&
test_cmp expected actual
'
}
test_config_cmd
()
{
test_config_cmd_set
"beep"
"boop"
test_config_cmd_set
"beep1"
"boop2"
...
...
@@ -220,6 +231,47 @@ test_config_cmd() {
# need to do this in reverse as the test profile is already applied in sharness
test_profile_apply_revert default-networking
test
test_profile_apply_dry_run_not_alter server
test_profile_apply_dry_run_not_alter local-discovery
test_profile_apply_dry_run_not_alter
test
test_expect_success
"'ipfs config profile apply local-discovery --dry-run' looks good with different profile info"
'
ipfs config profile apply local-discovery --dry-run > diff_info &&
test `grep "DisableNatPortMap" diff_info | wc -l` = 2
'
test_expect_success
"'ipfs config profile apply server --dry-run' looks good with same profile info"
'
ipfs config profile apply server --dry-run > diff_info &&
test `grep "DisableNatPortMap" diff_info | wc -l` = 1
'
test_expect_success
"'ipfs config profile apply server' looks good with same profile info"
'
ipfs config profile apply server > diff_info &&
test `grep "DisableNatPortMap" diff_info | wc -l` = 1
'
test_expect_success
"'ipfs config profile apply local-discovery' looks good with different profile info"
'
ipfs config profile apply local-discovery > diff_info &&
test `grep "DisableNatPortMap" diff_info | wc -l` = 2
'
test_expect_success
"'ipfs config profile apply test' looks good with different profile info"
'
ipfs config profile apply test > diff_info &&
test `grep "DisableNatPortMap" diff_info | wc -l` = 2
'
test_expect_success
"'ipfs config profile apply test --dry-run' doesn't include privkey"
'
ipfs config profile apply test --dry-run > show_config &&
test_expect_code 1 grep PrivKey show_config
'
test_expect_success
"'ipfs config profile apply test' doesn't include privkey"
'
ipfs config profile apply test > show_config &&
test_expect_code 1 grep PrivKey show_config
'
# won't work as it changes datastore definition, which makes ipfs not launch
# without converting first
# test_profile_apply_revert badgerds
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论