Skip to content

Commit

Permalink
Make ipfs key rm variadic
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Michael Muré <batolettre@gmail.com>
  • Loading branch information
MichaelMure committed May 1, 2017
1 parent 2bd5dec commit 469e7db
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions core/commands/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ var keyRmCmd = &cmds.Command{
Tagline: "Remove a keypair",
},
Arguments: []cmds.Argument{
cmds.StringArg("name", true, false, "name of key to remove"),
cmds.StringArg("name", true, true, "names of keys to remove"),
},
Options: []cmds.Option{
cmds.BoolOption("l", "Show extra information about keys."),
},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
Expand All @@ -217,45 +220,46 @@ var keyRmCmd = &cmds.Command{
return
}

name := req.Arguments()[0]
if name == "self" {
res.SetError(fmt.Errorf("cannot remove key with name 'self'"), cmds.ErrNormal)
return
}
names := req.Arguments()

removed, err := n.Repo.Keystore().Get(name)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
list := make([]KeyOutput, 0, len(names))
for _, name := range names {
if name == "self" {
res.SetError(fmt.Errorf("cannot remove key with name 'self'"), cmds.ErrNormal)
return
}

err = n.Repo.Keystore().Delete(name)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
removed, err := n.Repo.Keystore().Get(name)
if err != nil {
res.SetError(fmt.Errorf("no key named %s was found", name), cmds.ErrNormal)
return
}

pubKey := removed.GetPublic()
pubKey := removed.GetPublic()

pid, err := peer.IDFromPublicKey(pubKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
pid, err := peer.IDFromPublicKey(pubKey)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

list = append(list, KeyOutput{Name: name, Id: pid.Pretty()})
}

res.SetOutput(&KeyOutput{
Name: name,
Id: pid.Pretty(),
})
for _, name := range names {
err = n.Repo.Keystore().Delete(name)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}
}

res.SetOutput(&KeyOutputList{list})
},
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
},
cmds.Text: keyOutputListMarshaler,
},
Type: KeyOutput{},
Type: KeyOutputList{},
}

func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) {
Expand Down

0 comments on commit 469e7db

Please sign in to comment.