Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"ipfs key list": include the hash of the key id in addition to the name #3581

Merged
merged 3 commits into from
Jan 11, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions core/commands/keystore.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package commands

import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"

cmds "github.com/ipfs/go-ipfs/commands"

Expand All @@ -28,6 +31,10 @@ type KeyOutput struct {
Id string
}

type KeyOutputList struct {
Keys []KeyOutput
}

var KeyGenCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Create a new keypair",
Expand Down Expand Up @@ -125,7 +132,7 @@ var KeyGenCmd = &cmds.Command{
return nil, fmt.Errorf("expected a KeyOutput as command result")
}

return strings.NewReader(k.Id), nil
return strings.NewReader(k.Id + "\n"), nil
},
},
Type: KeyOutput{},
Expand All @@ -135,6 +142,9 @@ var KeyListCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List all local keypairs",
},
Options: []cmds.Option{
cmds.BoolOption("show-ids", "l", "also show key ids"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say just l, an have it catch all long format output, I think we might have more in future.

},
Run: func(req cmds.Request, res cmds.Response) {
n, err := req.InvocContext().GetNode()
if err != nil {
Expand All @@ -149,10 +159,52 @@ var KeyListCmd = &cmds.Command{
}

sort.Strings(keys)
res.SetOutput(&stringList{keys})

list := make([]KeyOutput, 0, len(keys))

for _, key := range keys {
privKey, err := n.Repo.Keystore().Get(key)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

pubKey := privKey.GetPublic()

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

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

res.SetOutput(&KeyOutputList{list})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: stringListMarshaler,
cmds.Text: keyOutputListMarshaler,
},
Type: stringList{},
Type: KeyOutputList{},
}

func keyOutputListMarshaler(res cmds.Response) (io.Reader, error) {
withId, _, _ := res.Request().Option("show-ids").Bool()

list, ok := res.Output().(*KeyOutputList)
if !ok {
return nil, errors.New("failed to cast []KeyOutput")
}

buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0)
for _, s := range list.Keys {
if withId {
fmt.Fprintf(w, "%s\t%s\t\n", s.Id, s.Name)
} else {
fmt.Fprintf(w, "%s\n", s.Name)
}
}
w.Flush()
return buf, nil
}