Commit 4d4d7b8e authored by Michael Muré's avatar Michael Muré

Implement ipfs key rm

License: MIT
Signed-off-by: default avatarMichael Muré <batolettre@gmail.com>
parent f44ff30e
......@@ -35,6 +35,7 @@ var KeyCmd = &cmds.Command{
Subcommands: map[string]*cmds.Command{
"gen": KeyGenCmd,
"list": KeyListCmd,
"rm": KeyRmCmd,
},
}
......@@ -202,6 +203,61 @@ var KeyListCmd = &cmds.Command{
Type: KeyOutputList{},
}
var KeyRmCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove a keypair",
},
Arguments: []cmds.Argument{
cmds.StringArg("name", true, false, "name of key to remove"),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
name := req.Arguments()[0]
if name == "self" {
res.SetError(fmt.Errorf("cannot remove key with name 'self'"), cmds.ErrNormal)
return
}
removed, err := n.Repo.Keystore().Get(name)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
err = n.Repo.Keystore().Delete(name)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
pubKey := removed.GetPublic()
pid, err := peer.IDFromPublicKey(pubKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
res.SetOutput(&KeyOutput{
Name: name,
Id: pid.Pretty(),
})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v := res.Output().(*KeyOutput)
s := fmt.Sprintf("Removed key %s with Id: %s\n", v.Name, v.Id)
return strings.NewReader(s), nil
},
},
Type: KeyOutput{},
}
func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) {
withId, _, _ := res.Request().Option("l").Bool()
......
......@@ -36,6 +36,14 @@ test_key_cmd() {
PeerID="$(ipfs config Identity.PeerID)"
ipfs key list -l | grep "$PeerID self"
'
test_expect_success "key rm remove a key" '
ipfs key rm foobarsa
echo bazed > list_exp &&
echo self >> list_exp
ipfs key list | sort > list_out &&
test_cmp list_exp list_out
'
}
test_key_cmd
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment