summaryrefslogtreecommitdiff
path: root/vendor/github.com/cpuguy83/dockercfg/load.go
blob: a1c4dca021be1530b4499e02946ac14f52dde54a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package dockercfg

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
)

// UserHomeConfigPath returns the path to the docker config in the current user's home dir.
func UserHomeConfigPath() (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return "", fmt.Errorf("user home dir: %w", err)
	}

	return filepath.Join(home, ".docker", "config.json"), nil
}

// ConfigPath returns the path to the docker cli config.
//
// It will either use the DOCKER_CONFIG env var if set, or the value from [UserHomeConfigPath]
// DOCKER_CONFIG would be the dir path where `config.json` is stored, this returns the path to config.json.
func ConfigPath() (string, error) {
	if p := os.Getenv("DOCKER_CONFIG"); p != "" {
		return filepath.Join(p, "config.json"), nil
	}
	return UserHomeConfigPath()
}

// LoadDefaultConfig loads the docker cli config from the path returned from [ConfigPath].
func LoadDefaultConfig() (Config, error) {
	var cfg Config
	p, err := ConfigPath()
	if err != nil {
		return cfg, fmt.Errorf("config path: %w", err)
	}

	return cfg, FromFile(p, &cfg)
}

// FromFile loads config from the specified path into cfg.
func FromFile(configPath string, cfg *Config) error {
	f, err := os.Open(configPath)
	if err != nil {
		return fmt.Errorf("open config: %w", err)
	}
	defer f.Close()

	if err = json.NewDecoder(f).Decode(&cfg); err != nil {
		return fmt.Errorf("decode config: %w", err)
	}

	return nil
}