github.com/docker/docker-ce@v17.12.1-ce-rc2+incompatible/components/cli/internal/test/store.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/docker/cli/cli/config/credentials"
     5  	"github.com/docker/docker/api/types"
     6  )
     7  
     8  // fake store implements a credentials.Store that only acts as an in memory map
     9  type fakeStore struct {
    10  	store      map[string]types.AuthConfig
    11  	eraseFunc  func(serverAddress string) error
    12  	getFunc    func(serverAddress string) (types.AuthConfig, error)
    13  	getAllFunc func() (map[string]types.AuthConfig, error)
    14  	storeFunc  func(authConfig types.AuthConfig) error
    15  }
    16  
    17  // NewFakeStore creates a new file credentials store.
    18  func NewFakeStore() credentials.Store {
    19  	return &fakeStore{store: map[string]types.AuthConfig{}}
    20  }
    21  
    22  func (c *fakeStore) SetStore(store map[string]types.AuthConfig) {
    23  	c.store = store
    24  }
    25  
    26  func (c *fakeStore) SetEraseFunc(eraseFunc func(string) error) {
    27  	c.eraseFunc = eraseFunc
    28  }
    29  
    30  func (c *fakeStore) SetGetFunc(getFunc func(string) (types.AuthConfig, error)) {
    31  	c.getFunc = getFunc
    32  }
    33  
    34  func (c *fakeStore) SetGetAllFunc(getAllFunc func() (map[string]types.AuthConfig, error)) {
    35  	c.getAllFunc = getAllFunc
    36  }
    37  
    38  func (c *fakeStore) SetStoreFunc(storeFunc func(types.AuthConfig) error) {
    39  	c.storeFunc = storeFunc
    40  }
    41  
    42  // Erase removes the given credentials from the map store
    43  func (c *fakeStore) Erase(serverAddress string) error {
    44  	if c.eraseFunc != nil {
    45  		return c.eraseFunc(serverAddress)
    46  	}
    47  	delete(c.store, serverAddress)
    48  	return nil
    49  }
    50  
    51  // Get retrieves credentials for a specific server from the map store.
    52  func (c *fakeStore) Get(serverAddress string) (types.AuthConfig, error) {
    53  	if c.getFunc != nil {
    54  		return c.getFunc(serverAddress)
    55  	}
    56  	return c.store[serverAddress], nil
    57  }
    58  
    59  func (c *fakeStore) GetAll() (map[string]types.AuthConfig, error) {
    60  	if c.getAllFunc != nil {
    61  		return c.getAllFunc()
    62  	}
    63  	return c.store, nil
    64  }
    65  
    66  // Store saves the given credentials in the map store.
    67  func (c *fakeStore) Store(authConfig types.AuthConfig) error {
    68  	if c.storeFunc != nil {
    69  		return c.storeFunc(authConfig)
    70  	}
    71  	c.store[authConfig.ServerAddress] = authConfig
    72  	return nil
    73  }