package main import ( "errors" "os/exec" "regexp" "strings" ) var ( keyRE = regexp.MustCompile(`^\s+([^\s]+)`) authorRE = regexp.MustCompile(`uid[^]]+]\s([^\s]+)\s<([^>]+)`) ) func gpgKeys() ([]Identity, error) { cmd := exec.Command("gpg", "-k") out, err := cmd.Output() if err != nil { return nil, err } lines := strings.Split(string(out), "\n") if len(lines) < 2 { return nil, errors.New("no GPG keys found") } lines = lines[2:] idents := make([]Identity, 0) for idx := 0; idx < len(lines); idx += 5 { if len(lines) < idx+4 { break } author := authorRE.FindStringSubmatch(lines[idx+2]) idents = append(idents, Identity{ Name: author[1], Email: author[2], Key: keyRE.FindString(lines[idx+1]), }) } return idents, nil }