github.com/hms58/moby@v1.13.1/cliconfig/config.go (about)

     1  package cliconfig
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/cliconfig/configfile"
    11  	"github.com/docker/docker/pkg/homedir"
    12  )
    13  
    14  const (
    15  	// ConfigFileName is the name of config file
    16  	ConfigFileName = "config.json"
    17  	configFileDir  = ".docker"
    18  	oldConfigfile  = ".dockercfg"
    19  )
    20  
    21  var (
    22  	configDir = os.Getenv("DOCKER_CONFIG")
    23  )
    24  
    25  func init() {
    26  	if configDir == "" {
    27  		configDir = filepath.Join(homedir.Get(), configFileDir)
    28  	}
    29  }
    30  
    31  // ConfigDir returns the directory the configuration file is stored in
    32  func ConfigDir() string {
    33  	return configDir
    34  }
    35  
    36  // SetConfigDir sets the directory the configuration file is stored in
    37  func SetConfigDir(dir string) {
    38  	configDir = dir
    39  }
    40  
    41  // NewConfigFile initializes an empty configuration file for the given filename 'fn'
    42  func NewConfigFile(fn string) *configfile.ConfigFile {
    43  	return &configfile.ConfigFile{
    44  		AuthConfigs: make(map[string]types.AuthConfig),
    45  		HTTPHeaders: make(map[string]string),
    46  		Filename:    fn,
    47  	}
    48  }
    49  
    50  // LegacyLoadFromReader is a convenience function that creates a ConfigFile object from
    51  // a non-nested reader
    52  func LegacyLoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
    53  	configFile := configfile.ConfigFile{
    54  		AuthConfigs: make(map[string]types.AuthConfig),
    55  	}
    56  	err := configFile.LegacyLoadFromReader(configData)
    57  	return &configFile, err
    58  }
    59  
    60  // LoadFromReader is a convenience function that creates a ConfigFile object from
    61  // a reader
    62  func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
    63  	configFile := configfile.ConfigFile{
    64  		AuthConfigs: make(map[string]types.AuthConfig),
    65  	}
    66  	err := configFile.LoadFromReader(configData)
    67  	return &configFile, err
    68  }
    69  
    70  // Load reads the configuration files in the given directory, and sets up
    71  // the auth config information and returns values.
    72  // FIXME: use the internal golang config parser
    73  func Load(configDir string) (*configfile.ConfigFile, error) {
    74  	if configDir == "" {
    75  		configDir = ConfigDir()
    76  	}
    77  
    78  	configFile := configfile.ConfigFile{
    79  		AuthConfigs: make(map[string]types.AuthConfig),
    80  		Filename:    filepath.Join(configDir, ConfigFileName),
    81  	}
    82  
    83  	// Try happy path first - latest config file
    84  	if _, err := os.Stat(configFile.Filename); err == nil {
    85  		file, err := os.Open(configFile.Filename)
    86  		if err != nil {
    87  			return &configFile, fmt.Errorf("%s - %v", configFile.Filename, err)
    88  		}
    89  		defer file.Close()
    90  		err = configFile.LoadFromReader(file)
    91  		if err != nil {
    92  			err = fmt.Errorf("%s - %v", configFile.Filename, err)
    93  		}
    94  		return &configFile, err
    95  	} else if !os.IsNotExist(err) {
    96  		// if file is there but we can't stat it for any reason other
    97  		// than it doesn't exist then stop
    98  		return &configFile, fmt.Errorf("%s - %v", configFile.Filename, err)
    99  	}
   100  
   101  	// Can't find latest config file so check for the old one
   102  	confFile := filepath.Join(homedir.Get(), oldConfigfile)
   103  	if _, err := os.Stat(confFile); err != nil {
   104  		return &configFile, nil //missing file is not an error
   105  	}
   106  	file, err := os.Open(confFile)
   107  	if err != nil {
   108  		return &configFile, fmt.Errorf("%s - %v", confFile, err)
   109  	}
   110  	defer file.Close()
   111  	err = configFile.LegacyLoadFromReader(file)
   112  	if err != nil {
   113  		return &configFile, fmt.Errorf("%s - %v", confFile, err)
   114  	}
   115  
   116  	if configFile.HTTPHeaders == nil {
   117  		configFile.HTTPHeaders = map[string]string{}
   118  	}
   119  	return &configFile, nil
   120  }