Implement ipfs key rm
License: MIT Signed-off-by: Michael Muré <batolettre@gmail.com>
Michael Muré committed
May 1, 2017 at 15:14 UTC
4d4d7b8e089d3f6ee532d75c16bec738dfd51110
2 files changed
+64
core/commands/keystore.go
+56
@@ -35,6 +35,7 @@ var KeyCmd = &cmds.Command{
35
Subcommands: map[string]*cmds.Command{
36
"gen": KeyGenCmd,
37
"list": KeyListCmd,
38
+ "rm": KeyRmCmd,
39
},
40
}
41
@@ -202,6 +203,61 @@ var KeyListCmd = &cmds.Command{
203
Type: KeyOutputList{},
204
}
205
206
+var KeyRmCmd = &cmds.Command{
207
+ Helptext: cmds.HelpText{
208
+ Tagline: "Remove a keypair",
209
+ },
210
+ Arguments: []cmds.Argument{
211
+ cmds.StringArg("name", true, false, "name of key to remove"),
212
+ },
213
+ Run: func(req cmds.Request, res cmds.Response) {
214
+ n, err := req.InvocContext().GetNode()
215
+ if err != nil {
216
+ res.SetError(err, cmds.ErrNormal)
217
+ return
218
+ }
219
+
220
+ name := req.Arguments()[0]
221
+ if name == "self" {
222
+ res.SetError(fmt.Errorf("cannot remove key with name 'self'"), cmds.ErrNormal)
223
+ return
224
+ }
225
+
226
+ removed, err := n.Repo.Keystore().Get(name)
227
+ if err != nil {
228
+ res.SetError(err, cmds.ErrNormal)
229
+ return
230
+ }
231
+
232
+ err = n.Repo.Keystore().Delete(name)
233
+ if err != nil {
234
+ res.SetError(err, cmds.ErrNormal)
235
+ return
236
+ }
237
+
238
+ pubKey := removed.GetPublic()
239
+
240
+ pid, err := peer.IDFromPublicKey(pubKey)
241
+ if err != nil {
242
+ res.SetError(err, cmds.ErrNormal)
243
+ return
244
+ }
245
+
246
+ res.SetOutput(&KeyOutput{
247
+ Name: name,
248
+ Id: pid.Pretty(),
249
+ })
250
+ },
251
+ Marshalers: cmds.MarshalerMap{
252
+ cmds.Text: func(res cmds.Response) (io.Reader, error) {
253
+ v := res.Output().(*KeyOutput)
254
+ s := fmt.Sprintf("Removed key %s with Id: %s\n", v.Name, v.Id)
255
+ return strings.NewReader(s), nil
256
+ },
257
+ },
258
+ Type: KeyOutput{},
259
+}
260
+
261
func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) {
262
withId, _, _ := res.Request().Option("l").Bool()
263
test/sharness/t0165-keystore.sh
+8
@@ -36,6 +36,14 @@ test_key_cmd() {
36
PeerID="$(ipfs config Identity.PeerID)"
37
ipfs key list -l | grep "$PeerID self"
38
'
39
+
40
+ test_expect_success "key rm remove a key" '
41
+ ipfs key rm foobarsa
42
+ echo bazed > list_exp &&
43
+ echo self >> list_exp
44
+ ipfs key list | sort > list_out &&
45
+ test_cmp list_exp list_out
46
+ '
47
}
48
49
test_key_cmd