...
1
15
16 package docker
17
18 import (
19 "os"
20
21 "github.com/docker/cli/cli/config"
22 "github.com/docker/cli/cli/config/configfile"
23 "github.com/docker/cli/cli/config/credentials"
24 "github.com/pkg/errors"
25
26 "oras.land/oras-go/pkg/auth"
27 )
28
29
30 type Client struct {
31 configs []*configfile.ConfigFile
32 }
33
34
35
36
37
38 func NewClient(configPaths ...string) (auth.Client, error) {
39 if len(configPaths) == 0 {
40 cfg, err := config.Load(config.Dir())
41 if err != nil {
42 return nil, err
43 }
44 if !cfg.ContainsAuth() {
45 cfg.CredentialsStore = credentials.DetectDefaultStore(cfg.CredentialsStore)
46 }
47
48 return &Client{
49 configs: []*configfile.ConfigFile{cfg},
50 }, nil
51 }
52
53 var configs []*configfile.ConfigFile
54 for _, path := range configPaths {
55 cfg, err := loadConfigFile(path)
56 if err != nil {
57 return nil, errors.Wrap(err, path)
58 }
59 configs = append(configs, cfg)
60 }
61
62 return &Client{
63 configs: configs,
64 }, nil
65 }
66
67
68
69
70
71 func NewClientWithDockerFallback(configPaths ...string) (auth.Client, error) {
72 if len(configPaths) == 0 {
73 return NewClient()
74 }
75
76 var configs []*configfile.ConfigFile
77 for _, path := range configPaths {
78 cfg, err := loadConfigFile(path)
79 if err != nil {
80 return nil, errors.Wrap(err, path)
81 }
82 configs = append(configs, cfg)
83 }
84
85
86 dockerFallbackCfg, err := config.Load(config.Dir())
87 if err != nil {
88 return nil, err
89 }
90 if !dockerFallbackCfg.ContainsAuth() {
91 dockerFallbackCfg.CredentialsStore = credentials.DetectDefaultStore(dockerFallbackCfg.CredentialsStore)
92 }
93 configs = append(configs, dockerFallbackCfg)
94
95 return &Client{
96 configs: configs,
97 }, nil
98 }
99
100 func (c *Client) primaryCredentialsStore(hostname string) credentials.Store {
101 return c.configs[0].GetCredentialsStore(hostname)
102 }
103
104
105 func loadConfigFile(path string) (*configfile.ConfigFile, error) {
106 cfg := configfile.New(path)
107 if _, err := os.Stat(path); err == nil {
108 file, err := os.Open(path)
109 if err != nil {
110 return nil, err
111 }
112 defer file.Close()
113 if err := cfg.LoadFromReader(file); err != nil {
114 return nil, err
115 }
116 } else if !os.IsNotExist(err) {
117 return nil, err
118 }
119 if !cfg.ContainsAuth() {
120 cfg.CredentialsStore = credentials.DetectDefaultStore(cfg.CredentialsStore)
121 }
122 return cfg, nil
123 }
124
View as plain text