forked from zalando/go-keyring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyring_mock.go
46 lines (41 loc) · 1.12 KB
/
keyring_mock.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
package keyring
type mockProvider struct {
mockStore map[string]map[string]string
}
// Set stores user and pass in the keyring under the defined service
// name.
func (m *mockProvider) Set(service, user, pass string) error {
if m.mockStore == nil {
m.mockStore = make(map[string]map[string]string)
}
if m.mockStore[service] == nil {
m.mockStore[service] = make(map[string]string)
}
m.mockStore[service][user] = pass
return nil
}
// Get gets a secret from the keyring given a service name and a user.
func (m *mockProvider) Get(service, user string) (string, error) {
if b, ok := m.mockStore[service]; ok {
if v, ok := b[user]; ok {
return v, nil
}
}
return "", ErrNotFound
}
// Delete deletes a secret, identified by service & user, from the keyring.
func (m *mockProvider) Delete(service, user string) error {
if m.mockStore != nil {
if _, ok := m.mockStore[service]; ok {
if _, ok := m.mockStore[service][user]; ok {
delete(m.mockStore[service], user)
return nil
}
}
}
return ErrNotFound
}
// MockInit sets the provider to a mocked memory store
func MockInit() {
provider = &mockProvider{}
}