From a00e058178b41c75856532e8f45a121546722ec8 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sun, 25 Feb 2018 03:25:49 -0800 Subject: [PATCH] Adds pm key ls --- cmd/pm/main.go | 5 +++++ keyring/keyring.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/cmd/pm/main.go b/cmd/pm/main.go index 17b5036..41ec93e 100644 --- a/cmd/pm/main.go +++ b/cmd/pm/main.go @@ -19,6 +19,7 @@ const keyUsage = `pm keyring: interact with pm's OpenPGP keyring subcommands: create (c) -- create a fresh keypair + list (ls) -- list configured key info ` func main() { @@ -41,6 +42,10 @@ func main() { } sub := os.Args[2] switch sub { + case "ls", "list": + if err := keyring.ListKeys(root, os.Stdout); err != nil { + fatalf("listing keypair: %v\n", err) + } case "c", "create": var name, email string s := bufio.NewScanner(os.Stdin) diff --git a/keyring/keyring.go b/keyring/keyring.go index 591845b..a601a30 100644 --- a/keyring/keyring.go +++ b/keyring/keyring.go @@ -1,8 +1,11 @@ package keyring import ( + "fmt" + "io" "os" "path/filepath" + "strings" "github.com/pkg/errors" "golang.org/x/crypto/openpgp" @@ -63,6 +66,33 @@ func NewKeyPair(root, name, email string) error { return nil } +// ListKeys prints keyring information to w. +func ListKeys(root string, w io.Writer) error { + if err := ensureDir(root); err != nil { + return errors.Wrap(err, "can't find or create pgp dir") + } + srn, prn := getNames(root) + secs, pubs, err := getELs(srn, prn) + if err != nil { + return errors.Wrap(err, "getting existing keyrings") + } + for _, s := range secs { + names := []string{} + for _, v := range s.Identities { + names = append(names, v.Name) + } + fmt.Fprintf(w, "sec: %+v:\t%v\n", s.PrimaryKey.KeyIdShortString(), strings.Join(names, ",")) + } + for _, p := range pubs { + names := []string{} + for _, v := range p.Identities { + names = append(names, v.Name) + } + fmt.Fprintf(w, "pub: %+v:\t%v\n", p.PrimaryKey.KeyIdShortString(), strings.Join(names, ",")) + } + return nil +} + func pGPDir(root string) string { return filepath.Join(root, "var", "lib", "pm", "pgp") }