Adds pm key ls

This commit is contained in:
Stephen McQuay 2018-02-25 03:25:49 -08:00
parent 9f921135f7
commit a00e058178
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
2 changed files with 35 additions and 0 deletions

View File

@ -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)

View File

@ -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")
}