-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpg-logic.go
87 lines (73 loc) · 1.69 KB
/
gpg-logic.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
package main
import (
"fmt"
"os/exec"
"runtime"
s "strings"
)
func getLocalFingerprints() []string {
result := make([]string, 0)
var out []byte
if runtime.GOOS == "windows" {
out = gpgListWindows()
} else {
out = gpgListLinux()
}
content := string(out)
lines := s.Split(content, "\n")
for i := 0; i < len(lines); i++ {
line := lines[i]
if s.HasPrefix(line, " ") {
trimmedLine := s.TrimSpace(line)
debug("adding line to gpg result: " + trimmedLine)
result = append(result, trimmedLine)
}
}
debug(fmt.Sprintf("Fingerprints in gpg: %d", len(result)))
return result
}
func gpgListWindows() []byte {
out, err := exec.Command("cmd", "/c", "gpg", "--list-keys").Output()
check(err)
return out
}
func gpgListLinux() []byte {
out, err := exec.Command("bash", "-c", "gpg", "--list-keys").Output()
check(err)
return out
}
func detectUnknownFingerprints(requestedFp []string, presentFp []string) []string {
result := make([]string, 0)
for i := 0; i < len(requestedFp); i++ {
found := false
for k := 0; k < len(presentFp); k++ {
if requestedFp[i] == presentFp[k] {
found = true
break
}
}
if !found {
result = append(result, requestedFp[i])
}
}
return result
}
func importKey(fileName string) {
var output []byte
if runtime.GOOS == "windows" {
output = gpgImportWindows(fileName)
} else {
output = gpgImportLinux(fileName)
}
debug(string(output))
}
func gpgImportWindows(fileName string) []byte {
out, err := exec.Command("cmd", "/c", "gpg", "--import", fileName).Output()
check(err)
return out
}
func gpgImportLinux(fileName string) []byte {
out, err := exec.Command("bash", "-c", "gpg", "--import", fileName).Output()
check(err)
return out
}