2016-12-25 14:31:52 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2017-06-12 14:36:49 -04:00
|
|
|
"fmt"
|
2016-12-25 14:31:52 -05:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-06-12 14:36:49 -04:00
|
|
|
"github.com/docker/cli/cli/config/credentials"
|
2017-10-15 15:39:56 -04:00
|
|
|
"github.com/docker/cli/cli/config/types"
|
2016-12-25 14:31:52 -05:00
|
|
|
"github.com/docker/docker/pkg/homedir"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-12-25 14:31:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ConfigFileName is the name of config file
|
|
|
|
ConfigFileName = "config.json"
|
|
|
|
configFileDir = ".docker"
|
|
|
|
oldConfigfile = ".dockercfg"
|
2018-12-17 05:27:07 -05:00
|
|
|
contextsDir = "contexts"
|
2016-12-25 14:31:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configDir = os.Getenv("DOCKER_CONFIG")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if configDir == "" {
|
|
|
|
configDir = filepath.Join(homedir.Get(), configFileDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dir returns the directory the configuration file is stored in
|
|
|
|
func Dir() string {
|
|
|
|
return configDir
|
|
|
|
}
|
|
|
|
|
2018-12-17 05:27:07 -05:00
|
|
|
// ContextStoreDir returns the directory the docker contexts are stored in
|
|
|
|
func ContextStoreDir() string {
|
|
|
|
return filepath.Join(Dir(), contextsDir)
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:31:52 -05:00
|
|
|
// SetDir sets the directory the configuration file is stored in
|
|
|
|
func SetDir(dir string) {
|
|
|
|
configDir = dir
|
|
|
|
}
|
|
|
|
|
2019-01-10 10:49:06 -05:00
|
|
|
// Path returns the path to a file relative to the config dir
|
|
|
|
func Path(p ...string) string {
|
|
|
|
return filepath.Join(append([]string{Dir()}, p...)...)
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:31:52 -05:00
|
|
|
// LegacyLoadFromReader is a convenience function that creates a ConfigFile object from
|
|
|
|
// a non-nested reader
|
|
|
|
func LegacyLoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
|
|
|
|
configFile := configfile.ConfigFile{
|
|
|
|
AuthConfigs: make(map[string]types.AuthConfig),
|
|
|
|
}
|
|
|
|
err := configFile.LegacyLoadFromReader(configData)
|
|
|
|
return &configFile, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromReader is a convenience function that creates a ConfigFile object from
|
|
|
|
// a reader
|
|
|
|
func LoadFromReader(configData io.Reader) (*configfile.ConfigFile, error) {
|
|
|
|
configFile := configfile.ConfigFile{
|
|
|
|
AuthConfigs: make(map[string]types.AuthConfig),
|
|
|
|
}
|
|
|
|
err := configFile.LoadFromReader(configData)
|
|
|
|
return &configFile, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load reads the configuration files in the given directory, and sets up
|
|
|
|
// the auth config information and returns values.
|
|
|
|
// FIXME: use the internal golang config parser
|
|
|
|
func Load(configDir string) (*configfile.ConfigFile, error) {
|
|
|
|
if configDir == "" {
|
|
|
|
configDir = Dir()
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:03:58 -04:00
|
|
|
filename := filepath.Join(configDir, ConfigFileName)
|
2017-06-27 10:31:38 -04:00
|
|
|
configFile := configfile.New(filename)
|
2016-12-25 14:31:52 -05:00
|
|
|
|
|
|
|
// Try happy path first - latest config file
|
2017-06-22 12:03:58 -04:00
|
|
|
if _, err := os.Stat(filename); err == nil {
|
|
|
|
file, err := os.Open(filename)
|
2016-12-25 14:31:52 -05:00
|
|
|
if err != nil {
|
2018-03-06 13:44:06 -05:00
|
|
|
return configFile, errors.Wrap(err, filename)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
err = configFile.LoadFromReader(file)
|
|
|
|
if err != nil {
|
2018-03-06 13:44:06 -05:00
|
|
|
err = errors.Wrap(err, filename)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
2017-06-22 12:03:58 -04:00
|
|
|
return configFile, err
|
2016-12-25 14:31:52 -05: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
|
2018-03-06 13:44:06 -05:00
|
|
|
return configFile, errors.Wrap(err, filename)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Can't find latest config file so check for the old one
|
|
|
|
confFile := filepath.Join(homedir.Get(), oldConfigfile)
|
|
|
|
if _, err := os.Stat(confFile); err != nil {
|
2017-06-22 12:03:58 -04:00
|
|
|
return configFile, nil //missing file is not an error
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
file, err := os.Open(confFile)
|
|
|
|
if err != nil {
|
2018-06-22 15:29:38 -04:00
|
|
|
return configFile, errors.Wrap(err, filename)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
err = configFile.LegacyLoadFromReader(file)
|
|
|
|
if err != nil {
|
2018-06-22 15:29:38 -04:00
|
|
|
return configFile, errors.Wrap(err, filename)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
2017-06-22 12:03:58 -04:00
|
|
|
return configFile, nil
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
2017-06-12 14:36:49 -04:00
|
|
|
|
|
|
|
// LoadDefaultConfigFile attempts to load the default config file and returns
|
|
|
|
// an initialized ConfigFile struct if none is found.
|
2017-06-21 17:20:49 -04:00
|
|
|
func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile {
|
|
|
|
configFile, err := Load(Dir())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
|
2017-06-12 14:36:49 -04:00
|
|
|
}
|
|
|
|
if !configFile.ContainsAuth() {
|
2017-06-21 16:47:06 -04:00
|
|
|
configFile.CredentialsStore = credentials.DetectDefaultStore(configFile.CredentialsStore)
|
2017-06-12 14:36:49 -04:00
|
|
|
}
|
|
|
|
return configFile
|
|
|
|
}
|