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