-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchanges.go
More file actions
60 lines (54 loc) · 1.38 KB
/
changes.go
File metadata and controls
60 lines (54 loc) · 1.38 KB
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
package vault
import (
"context"
"sort"
"github.com/keys-pub/keys"
"github.com/keys-pub/vault/syncer"
)
// Change on remote.
type Change struct {
VID keys.ID
Local int64
Remote int64
Timestamp int64
Push bool
}
// Changes for any keys in the keyring.
// If the keyring isn't synced this may not return all changes for those keyring
// keys, so you should usually sync the keyring first.
func (v *Vault) Changes(ctx context.Context) ([]*Change, error) {
vaults, err := getVaults(v.db)
if err != nil {
return nil, err
}
logger.Debugf("Changes for %d vaults", len(vaults))
status, err := v.client.Status(ctx, vaults)
if err != nil {
return nil, err
}
for _, st := range status {
logger.Debugf("Status: %s %d", st.ID, st.Index)
}
pullIndexes, err := syncer.PullIndexes(v.db)
if err != nil {
return nil, err
}
pushIndexes, err := syncer.PushIndexes(v.db)
if err != nil {
return nil, err
}
changes := []*Change{}
for _, st := range status {
kid := st.ID
local := pullIndexes[kid]
_, push := pushIndexes[kid]
if local < st.Index || push {
logger.Debugf("Changed %s, %d < %d", kid, local, st.Index)
changes = append(changes, &Change{VID: kid, Local: local, Remote: st.Index, Timestamp: st.Timestamp, Push: push})
}
}
sort.Slice(changes, func(i, j int) bool {
return changes[i].Timestamp > changes[j].Timestamp
})
return changes, nil
}