提交 3aa6d78e 作者: Michael Muré

Implement ipfs key rename

License: MIT
Signed-off-by: 's avatarMichael Muré <batolettre@gmail.com>
上级 6f815b15
......@@ -33,9 +33,10 @@ var KeyCmd = &cmds.Command{
`,
},
Subcommands: map[string]*cmds.Command{
"gen": keyGenCmd,
"list": keyListCmd,
"rm": keyRmCmd,
"gen": keyGenCmd,
"list": keyListCmd,
"rename": keyRenameCmd,
"rm": keyRmCmd,
},
}
......@@ -48,6 +49,13 @@ type KeyOutputList struct {
Keys []KeyOutput
}
type KeyRenameOutput struct {
Was string
Now string
Id string
Overwrite bool
}
var keyGenCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Create a new keypair",
......@@ -203,6 +211,103 @@ var keyListCmd = &cmds.Command{
Type: KeyOutputList{},
}
var keyRenameCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Rename a keypair",
},
Arguments: []cmds.Argument{
cmds.StringArg("name", true, false, "name of key to rename"),
cmds.StringArg("newName", true, false, "new name of the key"),
},
Options: []cmds.Option{
cmds.BoolOption("force", "f", "Allow to overwrite an existing key."),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
ks := n.Repo.Keystore()
name := req.Arguments()[0]
newName := req.Arguments()[1]
if name == "self" {
res.SetError(fmt.Errorf("cannot rename key with name 'self'"), cmds.ErrNormal)
return
}
if newName == "self" {
res.SetError(fmt.Errorf("cannot overwrite key with name 'self'"), cmds.ErrNormal)
return
}
oldKey, err := ks.Get(name)
if err != nil {
res.SetError(fmt.Errorf("no key named %s was found", name), cmds.ErrNormal)
return
}
pubKey := oldKey.GetPublic()
pid, err := peer.IDFromPublicKey(pubKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
overwrite := false
force, _, _ := res.Request().Option("f").Bool()
if force && ks.Has(newName) {
overwrite = true
err := ks.Delete(newName)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}
err = ks.Put(newName, oldKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
err = ks.Delete(name)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&KeyRenameOutput{
Was: name,
Now: newName,
Id: pid.Pretty(),
Overwrite: overwrite,
})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
k, ok := res.Output().(*KeyRenameOutput)
if !ok {
return nil, fmt.Errorf("expected a KeyRenameOutput as command result")
}
buf := new(bytes.Buffer)
if k.Overwrite {
fmt.Fprintf(buf, "Key %s renamed to %s with overwriting\n", k.Id, k.Now)
} else {
fmt.Fprintf(buf, "Key %s renamed to %s\n", k.Id, k.Now)
}
return buf, nil
},
},
Type: KeyRenameOutput{},
}
var keyRmCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove a keypair",
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论