keys.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. rpc "github.com/mikkeloscar/aur"
  9. gopkg "github.com/mikkeloscar/gopkgbuild"
  10. )
  11. // pgpKeySet maps a PGP key with a list of PKGBUILDs that require it.
  12. // This is similar to stringSet, used throughout the code.
  13. type pgpKeySet map[string][]*rpc.Pkg
  14. func (set pgpKeySet) toSlice() []string {
  15. slice := make([]string, 0, len(set))
  16. for v := range set {
  17. slice = append(slice, v)
  18. }
  19. return slice
  20. }
  21. func (set pgpKeySet) set(key string, p *rpc.Pkg) {
  22. // Using ToUpper to make sure keys with a different case will be
  23. // considered the same.
  24. upperKey := strings.ToUpper(key)
  25. if _, exists := set[upperKey]; !exists {
  26. set[upperKey] = []*rpc.Pkg{}
  27. }
  28. set[key] = append(set[key], p)
  29. }
  30. func (set pgpKeySet) get(key string) bool {
  31. upperKey := strings.ToUpper(key)
  32. _, exists := set[upperKey]
  33. return exists
  34. }
  35. // checkPgpKeys iterates through the keys listed in the PKGBUILDs and if needed,
  36. // asks the user whether yay should try to import them.
  37. func checkPgpKeys(pkgs []*rpc.Pkg, bases map[string][]*rpc.Pkg, srcinfos map[string]*gopkg.PKGBUILD) error {
  38. // Let's check the keys individually, and then we can offer to import
  39. // the problematic ones.
  40. problematic := make(pgpKeySet)
  41. args := append(strings.Fields(config.GpgFlags), "--list-keys")
  42. // Mapping all the keys.
  43. for _, pkg := range pkgs {
  44. srcinfo := srcinfos[pkg.PackageBase]
  45. for _, key := range srcinfo.Validpgpkeys {
  46. // If key already marked as problematic, indicate the current
  47. // PKGBUILD requires it.
  48. if problematic.get(key) {
  49. problematic.set(key, pkg)
  50. continue
  51. }
  52. cmd := exec.Command(config.GpgBin, append(args, key)...)
  53. err := cmd.Run()
  54. if err != nil {
  55. problematic.set(key, pkg)
  56. }
  57. }
  58. }
  59. // No key issues!
  60. if len(problematic) == 0 {
  61. return nil
  62. }
  63. str, err := formatKeysToImport(problematic, bases)
  64. if err != nil {
  65. return err
  66. }
  67. fmt.Println()
  68. fmt.Println(str)
  69. if continueTask(bold(green("Import?")), "nN") {
  70. return importKeys(problematic.toSlice())
  71. }
  72. return nil
  73. }
  74. // importKeys tries to import the list of keys specified in its argument.
  75. func importKeys(keys []string) error {
  76. args := append(strings.Fields(config.GpgFlags), "--recv-keys")
  77. cmd := exec.Command(config.GpgBin, append(args, keys...)...)
  78. cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
  79. fmt.Printf("%s %s...\n", bold(cyan("::")), bold("Importing keys with gpg..."))
  80. err := cmd.Run()
  81. if err != nil {
  82. return fmt.Errorf("%s Problem importing keys", bold(red(arrow+" Error:")))
  83. }
  84. return nil
  85. }
  86. // formatKeysToImport receives a set of keys and returns a string containing the
  87. // question asking the user wants to import the problematic keys.
  88. func formatKeysToImport(keys pgpKeySet, bases map[string][]*rpc.Pkg) (string, error) {
  89. if len(keys) == 0 {
  90. return "", fmt.Errorf("%s No keys to import", bold(red(arrow+" Error:")))
  91. }
  92. var buffer bytes.Buffer
  93. buffer.WriteString(bold(green(arrow)))
  94. buffer.WriteString(bold(green(" PGP keys need importing:")))
  95. for key, pkgs := range keys {
  96. pkglist := ""
  97. for _, pkg := range pkgs {
  98. pkglist += formatPkgbase(pkg, bases) + " "
  99. }
  100. pkglist = strings.TrimRight(pkglist, " ")
  101. buffer.WriteString(fmt.Sprintf("\n%s %s, required by: %s", yellow(bold(smallArrow)), cyan(key), cyan(pkglist)))
  102. }
  103. return buffer.String(), nil
  104. }