1package gpg 2 3import ( 4 "fmt" 5 "sort" 6 "strings" 7) 8 9// KeyList is a searchable slice of Keys 10type KeyList []Key 11 12// Recipients returns the KeyList formatted as a recipient list 13func (kl KeyList) Recipients() []string { 14 sort.Sort(kl) 15 u := make(map[string]struct{}, len(kl)) 16 for _, k := range kl { 17 u[k.ID()] = struct{}{} 18 } 19 l := make([]string, 0, len(kl)) 20 for k := range u { 21 l = append(l, k) 22 } 23 sort.Strings(l) 24 return l 25} 26 27// UseableKeys returns the list of useable (valid keys) 28func (kl KeyList) UseableKeys(alwaysTrust bool) KeyList { 29 nkl := make(KeyList, 0, len(kl)) 30 sort.Sort(kl) 31 for _, k := range kl { 32 if !k.IsUseable(alwaysTrust) { 33 continue 34 } 35 nkl = append(nkl, k) 36 } 37 return nkl 38} 39 40// UnusableKeys returns the list of unusable keys (invalid keys) 41func (kl KeyList) UnusableKeys(alwaysTrust bool) KeyList { 42 nkl := make(KeyList, 0, len(kl)) 43 for _, k := range kl { 44 if k.IsUseable(alwaysTrust) { 45 continue 46 } 47 nkl = append(nkl, k) 48 } 49 sort.Sort(nkl) 50 return nkl 51} 52 53// FindKey will try to find the requested key 54func (kl KeyList) FindKey(id string) (Key, error) { 55 id = strings.TrimPrefix(id, "0x") 56 for _, k := range kl { 57 if k.Fingerprint == id { 58 return k, nil 59 } 60 if strings.HasSuffix(k.Fingerprint, id) { 61 return k, nil 62 } 63 for _, ident := range k.Identities { 64 if ident.Name == id { 65 return k, nil 66 } 67 if ident.Email == id { 68 return k, nil 69 } 70 } 71 for sk := range k.SubKeys { 72 if strings.HasSuffix(sk, id) { 73 return k, nil 74 } 75 } 76 } 77 return Key{}, fmt.Errorf("no matching key found") 78} 79 80func (kl KeyList) Len() int { 81 return len(kl) 82} 83 84func (kl KeyList) Less(i, j int) bool { 85 return kl[i].Identity().Name < kl[j].Identity().Name 86} 87 88func (kl KeyList) Swap(i, j int) { 89 kl[i], kl[j] = kl[j], kl[i] 90} 91