-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
can.go
69 lines (57 loc) · 1.48 KB
/
can.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
package can
import (
"embed"
"fmt"
"os"
"path/filepath"
"github.com/ProtonMail/go-crypto/openpgp"
)
//go:embed gnupg/*
var can embed.FS
func EmbeddedKeyRing() openpgp.EntityList {
fh, err := can.Open("gnupg/pubring.gpg")
if err != nil {
// This must not happen. Since the key material is embedded into the
// binary, it must be available in in the correct format. If it is not
// we ca only panic. Since this is used for tests only this shouldn't
// affect users.
panic(err)
}
defer fh.Close() //nolint:errcheck
el, err := openpgp.ReadKeyRing(fh)
if err != nil {
// See reasoning above.
panic(err)
}
return el
}
func KeyID() string {
el := EmbeddedKeyRing()
if len(el) != 1 {
panic("pubring.gpg must contain exactly one key")
}
return el[0].PrimaryKey.KeyIdShortString()
}
// WriteTo writes the embedded content to the given output
// directory.
func WriteTo(path string) error {
fes, err := can.ReadDir("gnupg")
if err != nil {
return fmt.Errorf("Failed to read can dir: %w", err)
}
for _, fe := range fes {
from := "gnupg/" + fe.Name()
to := filepath.Join(path, fe.Name())
buf, err := can.ReadFile(from)
if err != nil {
return fmt.Errorf("failed to read %s: %w", from, err)
}
if err := os.MkdirAll(filepath.Dir(to), 0o700); err != nil {
return fmt.Errorf("failed to create dir %s: %w", filepath.Dir(to), err)
}
if err := os.WriteFile(to, buf, 0o600); err != nil {
return fmt.Errorf("failed to write %s: %w", to, err)
}
}
return nil
}