File: store.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-proposed-updates
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (79 lines) | stat: -rw-r--r-- 2,317 bytes parent folder | download | duplicates (7)
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
package test

import (
	"github.com/docker/cli/cli/config/credentials"
	"github.com/docker/cli/cli/config/types"
)

// FakeStore implements a credentials.Store that only acts as an in memory map
type FakeStore struct {
	store      map[string]types.AuthConfig
	eraseFunc  func(serverAddress string) error
	getFunc    func(serverAddress string) (types.AuthConfig, error)
	getAllFunc func() (map[string]types.AuthConfig, error)
	storeFunc  func(authConfig types.AuthConfig) error
}

// NewFakeStore creates a new file credentials store.
func NewFakeStore() credentials.Store {
	return &FakeStore{store: map[string]types.AuthConfig{}}
}

// SetStore is used to overrides Set function
func (c *FakeStore) SetStore(store map[string]types.AuthConfig) {
	c.store = store
}

// SetEraseFunc is used to overrides Erase function
func (c *FakeStore) SetEraseFunc(eraseFunc func(string) error) {
	c.eraseFunc = eraseFunc
}

// SetGetFunc is used to overrides Get function
func (c *FakeStore) SetGetFunc(getFunc func(string) (types.AuthConfig, error)) {
	c.getFunc = getFunc
}

// SetGetAllFunc is used to  overrides GetAll function
func (c *FakeStore) SetGetAllFunc(getAllFunc func() (map[string]types.AuthConfig, error)) {
	c.getAllFunc = getAllFunc
}

// SetStoreFunc is used to override Store function
func (c *FakeStore) SetStoreFunc(storeFunc func(types.AuthConfig) error) {
	c.storeFunc = storeFunc
}

// Erase removes the given credentials from the map store
func (c *FakeStore) Erase(serverAddress string) error {
	if c.eraseFunc != nil {
		return c.eraseFunc(serverAddress)
	}
	delete(c.store, serverAddress)
	return nil
}

// Get retrieves credentials for a specific server from the map store.
func (c *FakeStore) Get(serverAddress string) (types.AuthConfig, error) {
	if c.getFunc != nil {
		return c.getFunc(serverAddress)
	}
	return c.store[serverAddress], nil
}

// GetAll returns the key value pairs of ServerAddress => Username
func (c *FakeStore) GetAll() (map[string]types.AuthConfig, error) {
	if c.getAllFunc != nil {
		return c.getAllFunc()
	}
	return c.store, nil
}

// Store saves the given credentials in the map store.
func (c *FakeStore) Store(authConfig types.AuthConfig) error {
	if c.storeFunc != nil {
		return c.storeFunc(authConfig)
	}
	c.store[authConfig.ServerAddress] = authConfig
	return nil
}