-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
move.go
165 lines (145 loc) · 4.44 KB
/
move.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
154
155
156
157
158
159
160
161
162
163
164
165
package sub
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/justwatchcom/gopass/pkg/out"
"github.com/justwatchcom/gopass/pkg/store"
"github.com/pkg/errors"
)
// Copy will copy one entry to another location. Multi-store copies are
// supported. Each entry has to be decoded and encoded for the destination
// to make sure it's encrypted for the right set of recipients.
func (s *Store) Copy(ctx context.Context, from, to string) error {
// recursive copy?
if s.IsDir(ctx, from) {
if s.Exists(ctx, to) {
return errors.Errorf("Can not copy dir to file")
}
sf, err := s.List(ctx, "")
if err != nil {
return errors.Wrapf(err, "failed to list store")
}
destPrefix := to
if s.IsDir(ctx, to) {
destPrefix = filepath.Join(to, filepath.Base(from))
}
for _, e := range sf {
if !strings.HasPrefix(e, strings.TrimSuffix(from, "/")+"/") {
continue
}
et := filepath.Join(destPrefix, strings.TrimPrefix(e, from))
if err := s.Copy(ctx, e, et); err != nil {
out.Red(ctx, "Failed to copy '%s' to '%s': %s", e, et, err)
}
}
return nil
}
content, err := s.Get(ctx, from)
if err != nil {
return errors.Wrapf(err, "failed to get '%s' from store", from)
}
if err := s.Set(WithReason(ctx, fmt.Sprintf("Copied from %s to %s", from, to)), to, content); err != nil {
return errors.Wrapf(err, "failed to save '%s' to store", to)
}
return nil
}
// Move will move one entry from one location to another. Cross.storage moves are
// supported. Moving an entry will decode it from the old location, encode it
// for the destination store with the right set of recipients and remove it
// from the old location afterwards.
func (s *Store) Move(ctx context.Context, from, to string) error {
// recursive move?
if s.IsDir(ctx, from) {
if s.Exists(ctx, to) {
return errors.Errorf("Can not move dir to file")
}
sf, err := s.List(ctx, "")
if err != nil {
return errors.Wrapf(err, "failed to list store")
}
destPrefix := to
if s.IsDir(ctx, to) {
destPrefix = filepath.Join(to, filepath.Base(from))
}
for _, e := range sf {
if !strings.HasPrefix(e, strings.TrimSuffix(from, "/")+"/") {
continue
}
et := filepath.Join(destPrefix, strings.TrimPrefix(e, from))
if err := s.Move(ctx, e, et); err != nil {
out.Red(ctx, "Failed to move '%s' to '%s': %s", e, et, err)
}
}
return nil
}
content, err := s.Get(ctx, from)
if err != nil {
return errors.Wrapf(err, "failed to decrypt '%s'", from)
}
if err := s.Set(WithReason(ctx, fmt.Sprintf("Moved from %s to %s", from, to)), to, content); err != nil {
return errors.Wrapf(err, "failed to write '%s'", to)
}
if err := s.Delete(ctx, from); err != nil {
return errors.Wrapf(err, "failed to delete '%s'", from)
}
return nil
}
// Delete will remove an single entry from the store
func (s *Store) Delete(ctx context.Context, name string) error {
return s.delete(ctx, name, false)
}
// Prune will remove a subtree from the Store
func (s *Store) Prune(ctx context.Context, tree string) error {
return s.delete(ctx, tree, true)
}
// delete will either delete one file or an directory tree depending on the
// recurse flag
func (s *Store) delete(ctx context.Context, name string, recurse bool) error {
path := s.passfile(name)
if recurse {
if err := s.deleteRecurse(ctx, name, path); err != nil {
return err
}
}
if err := s.deleteSingle(ctx, path); err != nil {
if !recurse {
return err
}
}
if err := s.rcs.Add(ctx, path); err != nil {
if errors.Cause(err) == store.ErrGitNotInit {
return nil
}
return errors.Wrapf(err, "failed to add '%s' to git", path)
}
if err := s.rcs.Commit(ctx, fmt.Sprintf("Remove %s from store.", name)); err != nil {
if errors.Cause(err) == store.ErrGitNotInit {
return nil
}
return errors.Wrapf(err, "failed to commit changes to git")
}
if !IsAutoSync(ctx) {
return nil
}
if err := s.rcs.Push(ctx, "", ""); err != nil {
if errors.Cause(err) == store.ErrGitNotInit || errors.Cause(err) == store.ErrGitNoRemote {
return nil
}
return errors.Wrapf(err, "failed to push change to git remote")
}
return nil
}
func (s *Store) deleteRecurse(ctx context.Context, name, path string) error {
if !s.storage.IsDir(ctx, name) && !s.storage.Exists(ctx, path) {
return store.ErrNotFound
}
return s.storage.Prune(ctx, name)
}
func (s *Store) deleteSingle(ctx context.Context, path string) error {
if !s.storage.Exists(ctx, path) {
return store.ErrNotFound
}
return s.storage.Delete(ctx, path)
}