-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive_remotes_store.go
59 lines (45 loc) · 1.44 KB
/
archive_remotes_store.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
package currentarchive
import (
"encoding/json"
"fmt"
"github.com/archivekeep/archivekeep/archive/remote"
"github.com/archivekeep/archivekeep/internal/josesecrets"
)
const remotesSecretKey = "remotes"
type keyringRemotesStore struct {
Keyring *josesecrets.Keyring
}
func (j keyringRemotesStore) Get(name string) (*remote.Connection, error) {
var connections map[string]*remote.Connection
if !j.Keyring.Contains(remotesSecretKey) {
return nil, nil
}
err := j.Keyring.GetTo(remotesSecretKey, &connections)
if err != nil {
return nil, fmt.Errorf("get connections from keyring: %w", err)
}
return connections[name], nil
}
func (j keyringRemotesStore) Update(f func(current map[string]*remote.Connection) (map[string]*remote.Connection, error)) error {
return j.Keyring.Update(func(values map[string]json.RawMessage) error {
var connections map[string]*remote.Connection
if storedConnections, ok := values[remotesSecretKey]; ok {
err := json.Unmarshal(storedConnections, &connections)
if err != nil {
return fmt.Errorf("unmarshal stored connections: %w", err)
}
} else {
connections = map[string]*remote.Connection{}
}
connections, err := f(connections)
if err != nil {
return fmt.Errorf("preparing store update: %w", err)
}
newConnections, err := json.Marshal(connections)
if err != nil {
return fmt.Errorf("marshal new connections: %w", err)
}
values[remotesSecretKey] = newConnections
return nil
})
}