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 untrusted user: 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: subcommands:
create (c) -- create a fresh keypair create (c) -- create a fresh keypair
list (ls) -- list configured key info
` `
func main() { func main() {
@ -41,6 +42,10 @@ func main() {
} }
sub := os.Args[2] sub := os.Args[2]
switch sub { switch sub {
case "ls", "list":
if err := keyring.ListKeys(root, os.Stdout); err != nil {
fatalf("listing keypair: %v\n", err)
}
case "c", "create": case "c", "create":
var name, email string var name, email string
s := bufio.NewScanner(os.Stdin) s := bufio.NewScanner(os.Stdin)

View File

@ -1,8 +1,11 @@
package keyring package keyring
import ( import (
"fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp"
@ -63,6 +66,33 @@ func NewKeyPair(root, name, email string) error {
return nil 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 { func pGPDir(root string) string {
return filepath.Join(root, "var", "lib", "pm", "pgp") return filepath.Join(root, "var", "lib", "pm", "pgp")
} }