/
store.go
243 lines (192 loc) · 4.52 KB
/
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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Package inmem implements an in memory storage backend for tests.
package inmem
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"time"
"github.com/blang/semver/v4"
"github.com/gopasspw/gopass/internal/backend"
"golang.org/x/exp/maps"
)
// InMem is a thread-safe in-memory store.
type InMem struct {
sync.Mutex
data map[string][]byte
}
// New creates a new mock.
func New() *InMem {
return &InMem{
data: make(map[string][]byte, 128),
}
}
// Get retrieves a value.
func (m *InMem) Get(ctx context.Context, name string) ([]byte, error) {
m.Lock()
defer m.Unlock()
if m.data == nil {
return nil, fmt.Errorf("entry not found")
}
sec, found := m.data[name]
if !found {
// not found
return nil, fmt.Errorf("entry not found")
}
// found
return sec, nil
}
// Set writes a value.
func (m *InMem) Set(ctx context.Context, name string, value []byte) error {
m.Lock()
defer m.Unlock()
m.data[name] = value
return nil
}
// Delete removes a value.
func (m *InMem) Delete(ctx context.Context, name string) error {
m.Lock()
defer m.Unlock()
delete(m.data, name)
return nil
}
// Exists checks is a value exists.
func (m *InMem) Exists(ctx context.Context, name string) bool {
m.Lock()
defer m.Unlock()
_, found := m.data[name]
return found
}
// List shows all values.
func (m *InMem) List(ctx context.Context, prefix string) ([]string, error) {
m.Lock()
defer m.Unlock()
keys := maps.Keys(m.data)
sort.Strings(keys)
return keys, nil
}
// IsDir returns true if the entry is a directory.
func (m *InMem) IsDir(ctx context.Context, name string) bool {
m.Lock()
defer m.Unlock()
for k := range m.data {
if strings.HasPrefix(k, name+"/") {
return true
}
}
return false
}
// Prune removes a directory.
func (m *InMem) Prune(ctx context.Context, prefix string) error {
m.Lock()
defer m.Unlock()
deleted := 0
for k := range m.data {
if strings.HasPrefix(k, prefix+"/") {
delete(m.data, k)
deleted++
}
}
if deleted < 1 {
return fmt.Errorf("not found")
}
return nil
}
// Name returns the name of this backend.
func (m *InMem) Name() string {
return "inmem"
}
// Version returns the version of this backend.
func (m *InMem) Version(context.Context) semver.Version {
return semver.Version{Major: 1}
}
// String implement fmt.Stringer.
func (m *InMem) String() string {
return "inmem()"
}
// Path returns inmem.
func (m *InMem) Path() string {
return "inmem"
}
// Fsck always returns nil.
func (m *InMem) Fsck(ctx context.Context) error {
return nil
}
// Add does nothing.
func (m *InMem) Add(ctx context.Context, args ...string) error {
return nil
}
// TryAdd does nothing.
func (m *InMem) TryAdd(ctx context.Context, args ...string) error {
return nil
}
// Commit does nothing.
func (m *InMem) Commit(ctx context.Context, msg string) error {
return nil
}
// TryCommit does nothing.
func (m *InMem) TryCommit(ctx context.Context, msg string) error {
return nil
}
// Push does nothing.
func (m *InMem) Push(ctx context.Context, origin, branch string) error {
return nil
}
// TryPush does nothing.
func (m *InMem) TryPush(ctx context.Context, origin, branch string) error {
return nil
}
// Pull does nothing.
func (m *InMem) Pull(ctx context.Context, origin, branch string) error {
return nil
}
// Cmd does nothing.
func (m *InMem) Cmd(ctx context.Context, name string, args ...string) error {
return nil
}
// Init does nothing.
func (m *InMem) Init(context.Context, string, string) error {
return nil
}
// InitConfig does nothing.
func (m *InMem) InitConfig(context.Context, string, string) error {
return nil
}
// AddRemote does nothing.
func (m *InMem) AddRemote(ctx context.Context, remote, url string) error {
return nil
}
// RemoveRemote does nothing.
func (m *InMem) RemoveRemote(ctx context.Context, remote string) error {
return nil
}
// Revisions is not implemented.
func (m *InMem) Revisions(context.Context, string) ([]backend.Revision, error) {
return []backend.Revision{
{
Hash: "latest",
Date: time.Now(),
},
}, nil
}
// GetRevision is not implemented.
func (m *InMem) GetRevision(context.Context, string, string) ([]byte, error) {
return []byte("foo\nbar"), nil
}
// Status is not implemented.
func (m *InMem) Status(context.Context) ([]byte, error) {
return []byte(""), nil
}
// Compact is not implemented.
func (m *InMem) Compact(context.Context) error {
return nil
}
// Link is not implemented.
func (m *InMem) Link(context.Context, string, string) error {
return nil
}
// Move is not implemented.
func (m *InMem) Move(context.Context, string, string, bool) error {
return nil
}