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"
|
2019-03-07 09:28:42 -05:00
|
|
|
"strings"
|
2019-10-24 10:11:04 -04:00
|
|
|
"sync"
|
2016-12-25 14:31:52 -05:00
|
|
|
|
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"
|
2019-09-26 18:12:24 -04: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 (
|
2023-06-28 07:19:39 -04:00
|
|
|
// EnvOverrideConfigDir is the name of the environment variable that can be
|
|
|
|
// used to override the location of the client configuration files (~/.docker).
|
|
|
|
//
|
|
|
|
// It takes priority over the default, but can be overridden by the "--config"
|
|
|
|
// command line option.
|
|
|
|
EnvOverrideConfigDir = "DOCKER_CONFIG"
|
|
|
|
|
|
|
|
// ConfigFileName is the name of the client configuration file inside the
|
|
|
|
// config-directory.
|
2016-12-25 14:31:52 -05:00
|
|
|
ConfigFileName = "config.json"
|
|
|
|
configFileDir = ".docker"
|
2018-12-17 05:27:07 -05:00
|
|
|
contextsDir = "contexts"
|
2016-12-25 14:31:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-07-16 06:01:54 -04:00
|
|
|
initConfigDir = new(sync.Once)
|
2019-10-24 10:11:04 -04:00
|
|
|
configDir string
|
2016-12-25 14:31:52 -05:00
|
|
|
)
|
|
|
|
|
2023-06-28 06:40:38 -04:00
|
|
|
// resetConfigDir is used in testing to reset the "configDir" package variable
|
|
|
|
// and its sync.Once to force re-lookup between tests.
|
|
|
|
func resetConfigDir() {
|
|
|
|
configDir = ""
|
|
|
|
initConfigDir = new(sync.Once)
|
|
|
|
}
|
|
|
|
|
2016-12-25 14:31:52 -05:00
|
|
|
// Dir returns the directory the configuration file is stored in
|
|
|
|
func Dir() string {
|
2023-05-10 04:46:48 -04:00
|
|
|
initConfigDir.Do(func() {
|
2023-06-28 07:19:39 -04:00
|
|
|
configDir = os.Getenv(EnvOverrideConfigDir)
|
2023-05-10 04:46:48 -04:00
|
|
|
if configDir == "" {
|
|
|
|
configDir = filepath.Join(homedir.Get(), configFileDir)
|
|
|
|
}
|
|
|
|
})
|
2016-12-25 14:31:52 -05:00
|
|
|
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) {
|
2023-06-28 06:40:38 -04:00
|
|
|
// trigger the sync.Once to synchronise with Dir()
|
|
|
|
initConfigDir.Do(func() {})
|
2019-03-07 09:28:42 -05:00
|
|
|
configDir = filepath.Clean(dir)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
|
2019-01-10 10:49:06 -05:00
|
|
|
// Path returns the path to a file relative to the config dir
|
2019-03-07 09:28:42 -05:00
|
|
|
func Path(p ...string) (string, error) {
|
|
|
|
path := filepath.Join(append([]string{Dir()}, p...)...)
|
|
|
|
if !strings.HasPrefix(path, Dir()+string(filepath.Separator)) {
|
|
|
|
return "", errors.Errorf("path %q is outside of root config directory %q", path, Dir())
|
|
|
|
}
|
|
|
|
return path, nil
|
2019-01-10 10:49:06 -05:00
|
|
|
}
|
|
|
|
|
2016-12-25 14:31:52 -05:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2024-05-16 08:19:09 -04:00
|
|
|
// Load reads the configuration file ([ConfigFileName]) from the given directory.
|
|
|
|
// If no directory is given, it uses the default [Dir]. A [*configfile.ConfigFile]
|
|
|
|
// is returned containing the contents of the configuration file, or a default
|
|
|
|
// struct if no configfile exists in the given location.
|
2016-12-25 14:31:52 -05:00
|
|
|
func Load(configDir string) (*configfile.ConfigFile, error) {
|
|
|
|
if configDir == "" {
|
|
|
|
configDir = Dir()
|
|
|
|
}
|
2023-05-10 04:46:48 -04:00
|
|
|
return load(configDir)
|
|
|
|
}
|
2016-12-25 14:31:52 -05:00
|
|
|
|
2023-05-10 04:46:48 -04:00
|
|
|
func load(configDir string) (*configfile.ConfigFile, error) {
|
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
|
|
|
|
2023-05-10 04:46:48 -04:00
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
return configFile, nil
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
|
|
|
// if file is there but we can't stat it for any reason other
|
|
|
|
// than it doesn't exist then stop
|
2023-05-10 04:46:48 -04:00
|
|
|
return configFile, nil
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
2023-05-10 04:46:48 -04:00
|
|
|
defer file.Close()
|
|
|
|
err = configFile.LoadFromReader(file)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, filename)
|
2016-12-25 14:31:52 -05:00
|
|
|
}
|
2023-05-10 04:46:48 -04:00
|
|
|
return configFile, err
|
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
|
2024-05-16 08:19:09 -04:00
|
|
|
// a reference to the ConfigFile struct. If none is found or when failing to load
|
|
|
|
// the configuration file, it initializes a default ConfigFile struct. If no
|
|
|
|
// credentials-store is set in the configuration file, it attempts to discover
|
|
|
|
// the default store to use for the current platform.
|
|
|
|
//
|
|
|
|
// Important: LoadDefaultConfigFile prints a warning to stderr when failing to
|
|
|
|
// load the configuration file, but otherwise ignores errors. Consumers should
|
|
|
|
// consider using [Load] (and [credentials.DetectDefaultStore]) to detect errors
|
|
|
|
// when updating the configuration file, to prevent discarding a (malformed)
|
|
|
|
// configuration file.
|
2017-06-21 17:20:49 -04:00
|
|
|
func LoadDefaultConfigFile(stderr io.Writer) *configfile.ConfigFile {
|
2023-05-10 04:46:48 -04:00
|
|
|
configFile, err := load(Dir())
|
2017-06-21 17:20:49 -04:00
|
|
|
if err != nil {
|
2023-05-10 04:46:48 -04:00
|
|
|
_, _ = fmt.Fprintf(stderr, "WARNING: Error loading config file: %v\n", err)
|
2020-07-16 06:01:54 -04:00
|
|
|
}
|
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
|
|
|
|
}
|