mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 10:49:24 +08:00
Implement ipfs key rm
License: MIT Signed-off-by: Michael Muré <batolettre@gmail.com>
This commit is contained in:
@ -35,6 +35,7 @@ var KeyCmd = &cmds.Command{
|
|||||||
Subcommands: map[string]*cmds.Command{
|
Subcommands: map[string]*cmds.Command{
|
||||||
"gen": KeyGenCmd,
|
"gen": KeyGenCmd,
|
||||||
"list": KeyListCmd,
|
"list": KeyListCmd,
|
||||||
|
"rm": KeyRmCmd,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,6 +203,61 @@ var KeyListCmd = &cmds.Command{
|
|||||||
Type: KeyOutputList{},
|
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) {
|
func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) {
|
||||||
withId, _, _ := res.Request().Option("l").Bool()
|
withId, _, _ := res.Request().Option("l").Bool()
|
||||||
|
|
||||||
|
@ -36,6 +36,14 @@ test_key_cmd() {
|
|||||||
PeerID="$(ipfs config Identity.PeerID)"
|
PeerID="$(ipfs config Identity.PeerID)"
|
||||||
ipfs key list -l | grep "$PeerID self"
|
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
|
test_key_cmd
|
||||||
|
Reference in New Issue
Block a user