mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
"ipfs key list": add option to also list the hash of the key
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
This commit is contained in:
@ -1,11 +1,14 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
cmds "github.com/ipfs/go-ipfs/commands"
|
||||
|
||||
@ -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",
|
||||
@ -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"),
|
||||
},
|
||||
Run: func(req cmds.Request, res cmds.Response) {
|
||||
n, err := req.InvocContext().GetNode()
|
||||
if err != nil {
|
||||
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user