forked from gopasspw/gopass
/
sync.go
153 lines (131 loc) · 3.99 KB
/
sync.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package action
import (
"context"
"fmt"
"github.com/gopasspw/gopass/pkg/backend/rcs/noop"
"github.com/gopasspw/gopass/pkg/notify"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/pkg/store"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
// Sync all stores with their remotes
func (s *Action) Sync(ctx context.Context, c *cli.Context) error {
store := c.String("store")
return s.sync(ctx, c, store)
}
func (s *Action) sync(ctx context.Context, c *cli.Context, store string) error {
out.Green(ctx, "Sync starting ...")
numEntries := 0
if l, err := s.Store.Tree(ctx); err == nil {
numEntries = len(l.List(0))
}
numMPs := 0
mps := s.Store.MountPoints()
mps = append([]string{""}, mps...)
// sync all stores (root and all mounted sub stores)
for _, mp := range mps {
if store != "" {
if store != "root" && mp != store {
continue
}
if store == "root" && mp != "" {
continue
}
}
numMPs++
_ = s.syncMount(ctx, mp)
}
out.Green(ctx, "All done")
// calculate number of changes entries
if l, err := s.Store.Tree(ctx); err == nil {
numEntries = len(l.List(0)) - numEntries
}
diff := ""
if numEntries > 0 {
diff = fmt.Sprintf(" Added %d entries", numEntries)
} else if numEntries < 0 {
diff = fmt.Sprintf(" Removed %d entries", -1*numEntries)
}
_ = notify.Notify(ctx, "gopass - sync", fmt.Sprintf("Finished. Synced %d remotes.%s", numMPs, diff))
return nil
}
// syncMount syncs a single mount
func (s *Action) syncMount(ctx context.Context, mp string) error {
ctxno := out.WithNewline(ctx, false)
name := mp
if mp == "" {
name = "<root>"
}
out.Print(ctxno, color.GreenString("[%s] ", name))
sub, err := s.Store.GetSubStore(mp)
if err != nil {
out.Error(ctx, "Failed to get sub store '%s': %s", name, err)
return fmt.Errorf("failed to get sub stores (%s)", err)
}
if sub == nil {
out.Error(ctx, "Failed to get sub stores '%s: nil'", name)
return fmt.Errorf("failed to get sub stores (nil)")
}
numMP := 0
if l, err := sub.List(ctx, ""); err == nil {
numMP = len(l)
}
if sub.RCS().Name() == noop.New().Name() {
out.Error(ctxno, "\n WARNING: Mount uses RCS backend 'noop'. Not syncing!\n")
return nil
}
out.Print(ctxno, "\n "+color.GreenString("git pull and push ... "))
if err := sub.RCS().Push(ctx, "", ""); err != nil {
if errors.Cause(err) == store.ErrGitNoRemote {
out.Yellow(ctx, "Skipped (no remote)")
out.Debug(ctx, "Failed to push '%s' to it's remote: %s", name, err)
return err
}
out.Error(ctx, "Failed to push '%s' to it's remote: %s", name, err)
return err
}
out.Print(ctxno, color.GreenString("OK"))
if l, err := sub.List(ctx, ""); err == nil {
diff := len(l) - numMP
if diff > 0 {
out.Print(ctxno, color.GreenString(" (Added %d entries)", diff))
} else if diff < 0 {
out.Print(ctxno, color.GreenString(" (Removed %d entries)", -1*diff))
} else {
out.Print(ctxno, color.GreenString(" (no changes)"))
}
}
// import keys
out.Print(ctxno, "\n "+color.GreenString("importing missing keys ... "))
if err := sub.ImportMissingPublicKeys(ctx); err != nil {
out.Error(ctx, "Failed to import missing public keys for '%s': %s", name, err)
return err
}
out.Print(ctxno, color.GreenString("OK"))
// export keys
out.Print(ctxno, "\n "+color.GreenString("exporting missing keys ... "))
rs, err := sub.GetRecipients(ctx, "")
if err != nil {
out.Error(ctx, "Failed to load recipients for '%s': %s", name, err)
return err
}
exported, err := sub.ExportMissingPublicKeys(ctx, rs)
if err != nil {
out.Error(ctx, "Failed to export missing public keys for '%s': %s", name, err)
return err
}
// only run second push if we did export any keys
if exported {
if err := sub.RCS().Push(ctx, "", ""); err != nil {
out.Error(ctx, "Failed to push '%s' to it's remote: %s", name, err)
return err
}
out.Print(ctxno, color.GreenString("OK"))
} else {
out.Print(ctxno, color.GreenString("nothing to do"))
}
out.Print(ctx, "\n "+color.GreenString("done"))
return nil
}