github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/config/credentials/file_store.go (about)

     1  package credentials
     2  
     3  import (
     4  	"github.com/docker/docker/api/types"
     5  	"github.com/docker/docker/registry"
     6  )
     7  
     8  type store interface {
     9  	Save() error
    10  	GetAuthConfigs() map[string]types.AuthConfig
    11  	GetFilename() string
    12  }
    13  
    14  // fileStore implements a credentials store using
    15  // the docker configuration file to keep the credentials in plain text.
    16  type fileStore struct {
    17  	file store
    18  }
    19  
    20  // NewFileStore creates a new file credentials store.
    21  func NewFileStore(file store) Store {
    22  	return &fileStore{file: file}
    23  }
    24  
    25  // Erase removes the given credentials from the file store.
    26  func (c *fileStore) Erase(serverAddress string) error {
    27  	delete(c.file.GetAuthConfigs(), serverAddress)
    28  	return c.file.Save()
    29  }
    30  
    31  // Get retrieves credentials for a specific server from the file store.
    32  func (c *fileStore) Get(serverAddress string) (types.AuthConfig, error) {
    33  	authConfig, ok := c.file.GetAuthConfigs()[serverAddress]
    34  	if !ok {
    35  		// Maybe they have a legacy config file, we will iterate the keys converting
    36  		// them to the new format and testing
    37  		for r, ac := range c.file.GetAuthConfigs() {
    38  			if serverAddress == registry.ConvertToHostname(r) {
    39  				return ac, nil
    40  			}
    41  		}
    42  
    43  		authConfig = types.AuthConfig{}
    44  	}
    45  	return authConfig, nil
    46  }
    47  
    48  func (c *fileStore) GetAll() (map[string]types.AuthConfig, error) {
    49  	return c.file.GetAuthConfigs(), nil
    50  }
    51  
    52  // Store saves the given credentials in the file store.
    53  func (c *fileStore) Store(authConfig types.AuthConfig) error {
    54  	c.file.GetAuthConfigs()[authConfig.ServerAddress] = authConfig
    55  	return c.file.Save()
    56  }
    57  
    58  func (c *fileStore) GetFilename() string {
    59  	return c.file.GetFilename()
    60  }
    61  
    62  func (c *fileStore) IsFileStore() bool {
    63  	return true
    64  }