2016-12-25 14:31:52 -05:00
|
|
|
package config
|
2015-04-22 08:06:58 -04:00
|
|
|
|
|
|
|
import (
|
2015-07-28 21:28:14 -04:00
|
|
|
"io"
|
2015-04-22 08:06:58 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-12-25 14:31:52 -05:00
|
|
|
"github.com/docker/docker/cli/config/configfile"
|
2015-04-22 08:06:58 -04:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2015-04-22 08:06:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-07-30 17:36:53 -04:00
|
|
|
// ConfigFileName is the name of config file
|
2015-07-20 18:39:07 -04:00
|
|
|
ConfigFileName = "config.json"
|
2016-02-29 07:43:57 -05:00
|
|
|
configFileDir = ".docker"
|
2015-07-20 18:39:07 -04:00
|
|
|
oldConfigfile = ".dockercfg"
|
2015-04-22 08:06:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-07-20 18:39:07 -04:00
|
|
|
configDir = os.Getenv("DOCKER_CONFIG")
|
2015-04-22 08:06:58 -04:00
|
|
|
)
|
|
|
|
|
2015-04-28 11:00:18 -04:00
|
|
|
func init() {
|
|
|
|
if configDir == "" {
|
2016-02-29 07:43:57 -05:00
|
|
|
configDir = filepath.Join(homedir.Get(), configFileDir)
|
2015-04-28 11:00:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:31:52 -05:00
|
|
|
// Dir returns the directory the configuration file is stored in
|
|
|
|
func Dir() string {
|
2015-04-28 11:00:18 -04:00
|
|
|
return configDir
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:31:52 -05:00
|
|
|
// SetDir sets the directory the configuration file is stored in
|
|
|
|
func SetDir(dir string) {
|
2015-04-28 11:00:18 -04:00
|
|
|
configDir = dir
|
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// NewConfigFile initializes an empty configuration file for the given filename 'fn'
|
2016-04-21 18:37:08 -04:00
|
|
|
func NewConfigFile(fn string) *configfile.ConfigFile {
|
|
|
|
return &configfile.ConfigFile{
|
2015-12-11 23:11:42 -05:00
|
|
|
AuthConfigs: make(map[string]types.AuthConfig),
|
2015-07-20 18:39:07 -04:00
|
|
|
HTTPHeaders: make(map[string]string),
|
2016-04-21 18:37:08 -04:00
|
|
|
Filename: fn,
|
2015-07-28 21:28:14 -04:00
|
|
|
}
|
2016-02-07 19:55:17 -05:00
|
|
|
}
|
|
|
|
|
2015-07-28 21:28:14 -04:00
|
|
|
// LegacyLoadFromReader is a convenience function that creates a ConfigFile object from
|
|
|
|
// a non-nested reader
|
2016-04-21 18:37:08 -04:00
|
|
|
func LegacyLoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
|
|
|
|
configFile := configfile.ConfigFile{
|
2015-12-11 23:11:42 -05:00
|
|
|
AuthConfigs: make(map[string]types.AuthConfig),
|
2015-07-28 21:28:14 -04:00
|
|
|
}
|
|
|
|
err := configFile.LegacyLoadFromReader(configData)
|
|
|
|
return &configFile, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromReader is a convenience function that creates a ConfigFile object from
|
|
|
|
// a reader
|
2016-04-21 18:37:08 -04:00
|
|
|
func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
|
|
|
|
configFile := configfile.ConfigFile{
|
2015-12-11 23:11:42 -05:00
|
|
|
AuthConfigs: make(map[string]types.AuthConfig),
|
2015-07-28 21:28:14 -04:00
|
|
|
}
|
|
|
|
err := configFile.LoadFromReader(configData)
|
|
|
|
return &configFile, err
|
|
|
|
}
|
|
|
|
|
2015-07-20 18:39:07 -04:00
|
|
|
// Load reads the configuration files in the given directory, and sets up
|
2016-03-26 10:06:45 -04:00
|
|
|
// the auth config information and returns values.
|
2015-04-22 08:06:58 -04:00
|
|
|
// FIXME: use the internal golang config parser
|
2016-04-21 18:37:08 -04:00
|
|
|
func Load(configDir string) (*configfile.ConfigFile, error) {
|
2015-04-22 08:06:58 -04:00
|
|
|
if configDir == "" {
|
2016-12-25 14:31:52 -05:00
|
|
|
configDir = Dir()
|
2015-04-22 08:06:58 -04:00
|
|
|
}
|
|
|
|
|
2016-04-21 18:37:08 -04:00
|
|
|
configFile := configfile.ConfigFile{
|
2015-12-11 23:11:42 -05:00
|
|
|
AuthConfigs: make(map[string]types.AuthConfig),
|
2016-04-21 18:37:08 -04:00
|
|
|
Filename: filepath.Join(configDir, ConfigFileName),
|
2015-04-22 08:06:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try happy path first - latest config file
|
2016-04-21 18:37:08 -04:00
|
|
|
if _, err := os.Stat(configFile.Filename); err == nil {
|
|
|
|
file, err := os.Open(configFile.Filename)
|
2015-04-22 08:06:58 -04:00
|
|
|
if err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return &configFile, errors.Errorf("%s - %v", configFile.Filename, err)
|
2015-04-22 08:06:58 -04:00
|
|
|
}
|
|
|
|
defer file.Close()
|
2015-07-28 21:28:14 -04:00
|
|
|
err = configFile.LoadFromReader(file)
|
2015-12-15 21:24:10 -05:00
|
|
|
if err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
err = errors.Errorf("%s - %v", configFile.Filename, err)
|
2015-12-15 21:24:10 -05:00
|
|
|
}
|
2015-07-28 21:28:14 -04:00
|
|
|
return &configFile, err
|
2015-04-22 08:06:58 -04:00
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
// if file is there but we can't stat it for any reason other
|
|
|
|
// than it doesn't exist then stop
|
2017-03-09 13:23:45 -05:00
|
|
|
return &configFile, errors.Errorf("%s - %v", configFile.Filename, err)
|
2015-04-22 08:06:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Can't find latest config file so check for the old one
|
2016-02-29 07:40:45 -05:00
|
|
|
confFile := filepath.Join(homedir.Get(), oldConfigfile)
|
2015-04-22 08:06:58 -04:00
|
|
|
if _, err := os.Stat(confFile); err != nil {
|
|
|
|
return &configFile, nil //missing file is not an error
|
|
|
|
}
|
2015-07-28 21:28:14 -04:00
|
|
|
file, err := os.Open(confFile)
|
2015-04-22 08:06:58 -04:00
|
|
|
if err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return &configFile, errors.Errorf("%s - %v", confFile, err)
|
2015-04-22 08:06:58 -04:00
|
|
|
}
|
2015-07-28 21:28:14 -04:00
|
|
|
defer file.Close()
|
|
|
|
err = configFile.LegacyLoadFromReader(file)
|
2015-12-02 23:53:06 -05:00
|
|
|
if err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return &configFile, errors.Errorf("%s - %v", confFile, err)
|
2015-12-02 23:53:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if configFile.HTTPHeaders == nil {
|
|
|
|
configFile.HTTPHeaders = map[string]string{}
|
|
|
|
}
|
|
|
|
return &configFile, nil
|
2015-04-22 08:06:58 -04:00
|
|
|
}
|