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