forked from gopasspw/gopass
/
move.go
156 lines (140 loc) · 4.31 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
package sub
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/justwatchcom/gopass/store"
"github.com/justwatchcom/gopass/utils/fsutil"
"github.com/justwatchcom/gopass/utils/out"
"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(from) {
if s.Exists(to) {
return errors.Errorf("Can not copy dir to file")
}
sf, err := s.List("")
if err != nil {
return errors.Wrapf(err, "failed to list store")
}
destPrefix := to
if s.IsDir(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-store 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(from) {
if s.Exists(to) {
return errors.Errorf("Can not move dir to file")
}
sf, err := s.List("")
if err != nil {
return errors.Wrapf(err, "failed to list store")
}
destPrefix := to
if s.IsDir(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
// RemoveFunc given. Use nil or os.Remove for the single-file mode and
// os.RemoveAll for the recursive mode.
func (s *Store) delete(ctx context.Context, name string, recurse bool) error {
path := s.passfile(name)
rf := os.Remove
if !recurse && !fsutil.IsFile(path) {
return store.ErrNotFound
}
if recurse && !fsutil.IsFile(path) {
path = filepath.Join(s.path, name)
rf = os.RemoveAll
if !fsutil.IsDir(path) {
return store.ErrNotFound
}
}
if err := rf(path); err != nil {
return errors.Errorf("Failed to remove secret: %v", err)
}
if err := s.git.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.git.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.git.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
}