forked from Foxboron/sbctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
58 lines (49 loc) · 1.11 KB
/
database.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
package sbctl
import (
"encoding/json"
"fmt"
"github.com/foxboron/sbctl/fs"
)
type SigningEntry struct {
File string `json:"file"`
OutputFile string `json:"output_file"`
Checksum string `json:"checksum"`
}
type SigningEntries map[string]*SigningEntry
func ReadFileDatabase(dbpath string) (SigningEntries, error) {
f, err := ReadOrCreateFile(dbpath)
if err != nil {
return nil, err
}
files := make(SigningEntries)
if len(f) == 0 {
return files, nil
}
if err = json.Unmarshal(f, &files); err != nil {
return nil, fmt.Errorf("failed to parse json: %v", err)
}
return files, nil
}
func WriteFileDatabase(dbpath string, files SigningEntries) error {
data, err := json.MarshalIndent(files, "", " ")
if err != nil {
return err
}
err = fs.WriteFile(dbpath, data, 0644)
if err != nil {
return err
}
return nil
}
func SigningEntryIter(fn func(s *SigningEntry) error) error {
files, err := ReadFileDatabase(DBPath)
if err != nil {
return fmt.Errorf("couldn't open database %v: %w", DBPath, err)
}
for _, s := range files {
if err := fn(s); err != nil {
return err
}
}
return nil
}