From 4d4d7b8e089d3f6ee532d75c16bec738dfd51110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Mon, 1 May 2017 15:14:30 +0900 Subject: [PATCH] Implement ipfs key rm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Michael Muré --- core/commands/keystore.go | 56 +++++++++++++++++++++++++++++++++ test/sharness/t0165-keystore.sh | 8 +++++ 2 files changed, 64 insertions(+) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index 7ab6cd914..b47faf02b 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -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() diff --git a/test/sharness/t0165-keystore.sh b/test/sharness/t0165-keystore.sh index ec9726a8a..78bc9d646 100755 --- a/test/sharness/t0165-keystore.sh +++ b/test/sharness/t0165-keystore.sh @@ -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