Make ipfs key rm variadic
License: MIT Signed-off-by: Michael Muré <batolettre@gmail.com>
Michael Muré committed
May 2, 2017 at 01:41 UTC
bd96f71d2c884e298d608963cd3e368287f2e852
1 file changed
+35
-31
core/commands/keystore.go
+35
-31
@@ -208,7 +208,10 @@ var keyRmCmd = &cmds.Command{
208
Tagline: "Remove a keypair",
209
},
210
Arguments: []cmds.Argument{
211
- cmds.StringArg("name", true, false, "name of key to remove"),
211
+ cmds.StringArg("name", true, true, "names of keys to remove"),
212
+ },
213
+ Options: []cmds.Option{
214
+ cmds.BoolOption("l", "Show extra information about keys."),
215
},
216
Run: func(req cmds.Request, res cmds.Response) {
217
n, err := req.InvocContext().GetNode()
@@ -217,45 +220,46 @@ var keyRmCmd = &cmds.Command{
220
return
221
}
222
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
- }
223
+ names := req.Arguments()
224
226
- removed, err := n.Repo.Keystore().Get(name)
227
- if err != nil {
228
- res.SetError(err, cmds.ErrNormal)
229
- return
230
- }
225
+ list := make([]KeyOutput, 0, len(names))
226
+ for _, name := range names {
227
+ if name == "self" {
228
+ res.SetError(fmt.Errorf("cannot remove key with name 'self'"), 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
- }
232
+ removed, err := n.Repo.Keystore().Get(name)
233
+ if err != nil {
234
+ res.SetError(fmt.Errorf("no key named %s was found", name), cmds.ErrNormal)
235
+ return
236
+ }
237
238
- pubKey := removed.GetPublic()
238
+ pubKey := removed.GetPublic()
239
240
- pid, err := peer.IDFromPublicKey(pubKey)
241
- if err != nil {
242
- res.SetError(err, cmds.ErrNormal)
243
- return
240
+ pid, err := peer.IDFromPublicKey(pubKey)
241
+ if err != nil {
242
+ res.SetError(err, cmds.ErrNormal)
243
+ return
244
+ }
245
+
246
+ list = append(list, KeyOutput{Name: name, Id: pid.Pretty()})
247
}
248
246
- res.SetOutput(&KeyOutput{
247
- Name: name,
248
- Id: pid.Pretty(),
249
- })
249
+ for _, name := range names {
250
+ err = n.Repo.Keystore().Delete(name)
251
+ if err != nil {
252
+ res.SetError(err, cmds.ErrNormal)
253
+ return
254
+ }
255
+ }
256
+
257
+ res.SetOutput(&KeyOutputList{list})
258
},
259
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
- },
260
+ cmds.Text: keyOutputListMarshaler,
261
},
258
- Type: KeyOutput{},
262
+ Type: KeyOutputList{},
263
}
264
265
func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) {