Skip to content

Commit 0d8485a

Browse files
tonistiigiTibor Vass
authored andcommitted
config: remove docker api dependency from cli/config
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com> Signed-off-by: Tibor Vass <tibor@docker.com>
1 parent 080f30a commit 0d8485a

File tree

12 files changed

+71
-23
lines changed

12 files changed

+71
-23
lines changed

cli/command/container/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command {
6868
}
6969

7070
func runRun(dockerCli command.Cli, flags *pflag.FlagSet, ropts *runOptions, copts *containerOptions) error {
71-
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), copts.env.GetAll())
71+
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), opts.ConvertKVStringsToMapWithNil(copts.env.GetAll()))
7272
newEnv := []string{}
7373
for k, v := range proxyConfig {
7474
if v == nil {

cli/command/image/build.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,11 @@ func runBuild(dockerCli command.Cli, options buildOptions) error {
383383
}
384384

385385
configFile := dockerCli.ConfigFile()
386-
authConfigs, _ := configFile.GetAllCredentials()
386+
creds, _ := configFile.GetAllCredentials()
387+
authConfigs := make(map[string]types.AuthConfig, len(creds))
388+
for k, auth := range creds {
389+
authConfigs[k] = types.AuthConfig(auth)
390+
}
387391
buildOptions := imageBuildOptions(dockerCli, options)
388392
buildOptions.Version = types.BuilderV1
389393
buildOptions.Dockerfile = relDockerfile
@@ -619,7 +623,7 @@ func imageBuildOptions(dockerCli command.Cli, options buildOptions) types.ImageB
619623
CgroupParent: options.cgroupParent,
620624
ShmSize: options.shmSize.Value(),
621625
Ulimits: options.ulimits.GetList(),
622-
BuildArgs: configFile.ParseProxyConfig(dockerCli.Client().DaemonHost(), options.buildArgs.GetAll()),
626+
BuildArgs: configFile.ParseProxyConfig(dockerCli.Client().DaemonHost(), opts.ConvertKVStringsToMapWithNil(options.buildArgs.GetAll())),
623627
Labels: opts.ConvertKVStringsToMap(options.labels.GetAll()),
624628
CacheFrom: options.cacheFrom,
625629
SecurityOpt: options.securityOpt,

cli/command/registry.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"runtime"
1212
"strings"
1313

14+
configtypes "github.com/docker/cli/cli/config/types"
1415
"github.com/docker/cli/cli/debug"
1516
"github.com/docker/distribution/reference"
1617
"github.com/docker/docker/api/types"
@@ -76,7 +77,7 @@ func ResolveAuthConfig(ctx context.Context, cli Cli, index *registrytypes.IndexI
7677
}
7778

7879
a, _ := cli.ConfigFile().GetAuthConfig(configKey)
79-
return a
80+
return types.AuthConfig(a)
8081
}
8182

8283
// GetDefaultAuthConfig gets the default auth config given a serverAddress
@@ -85,16 +86,17 @@ func GetDefaultAuthConfig(cli Cli, checkCredStore bool, serverAddress string, is
8586
if !isDefaultRegistry {
8687
serverAddress = registry.ConvertToHostname(serverAddress)
8788
}
88-
var authconfig types.AuthConfig
89+
var authconfig configtypes.AuthConfig
8990
var err error
9091
if checkCredStore {
9192
authconfig, err = cli.ConfigFile().GetAuthConfig(serverAddress)
9293
} else {
93-
authconfig = types.AuthConfig{}
94+
authconfig = configtypes.AuthConfig{}
9495
}
9596
authconfig.ServerAddress = serverAddress
9697
authconfig.IdentityToken = ""
97-
return &authconfig, err
98+
res := types.AuthConfig(authconfig)
99+
return &res, err
98100
}
99101

100102
// ConfigureAuth handles prompting of user's username and password if needed

cli/command/registry/login.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/docker/cli/cli"
1010
"github.com/docker/cli/cli/command"
11+
configtypes "github.com/docker/cli/cli/config/types"
1112
"github.com/docker/docker/api/types"
1213
registrytypes "github.com/docker/docker/api/types/registry"
1314
"github.com/docker/docker/client"
@@ -149,7 +150,7 @@ func runLogin(dockerCli command.Cli, opts loginOptions) error { //nolint: gocycl
149150
}
150151
}
151152

152-
if err := creds.Store(*authConfig); err != nil {
153+
if err := creds.Store(configtypes.AuthConfig(*authConfig)); err != nil {
153154
return errors.Errorf("Error saving credentials: %v", err)
154155
}
155156

cli/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/docker/cli/cli/config/configfile"
1010
"github.com/docker/cli/cli/config/credentials"
11-
"github.com/docker/docker/api/types"
11+
"github.com/docker/cli/cli/config/types"
1212
"github.com/docker/docker/pkg/homedir"
1313
"github.com/pkg/errors"
1414
)

cli/config/configfile/file.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/docker/cli/cli/config/credentials"
14-
"github.com/docker/cli/opts"
15-
"github.com/docker/docker/api/types"
14+
"github.com/docker/cli/cli/config/types"
1615
"github.com/pkg/errors"
1716
)
1817

@@ -195,7 +194,7 @@ func (configFile *ConfigFile) Save() error {
195194

196195
// ParseProxyConfig computes proxy configuration by retrieving the config for the provided host and
197196
// then checking this against any environment variables provided to the container
198-
func (configFile *ConfigFile) ParseProxyConfig(host string, runOpts []string) map[string]*string {
197+
func (configFile *ConfigFile) ParseProxyConfig(host string, runOpts map[string]*string) map[string]*string {
199198
var cfgKey string
200199

201200
if _, ok := configFile.Proxies[host]; !ok {
@@ -211,7 +210,10 @@ func (configFile *ConfigFile) ParseProxyConfig(host string, runOpts []string) ma
211210
"NO_PROXY": &config.NoProxy,
212211
"FTP_PROXY": &config.FTPProxy,
213212
}
214-
m := opts.ConvertKVStringsToMapWithNil(runOpts)
213+
m := runOpts
214+
if m == nil {
215+
m = make(map[string]*string)
216+
}
215217
for k := range permitted {
216218
if *permitted[k] == "" {
217219
continue

cli/config/configfile/file_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/docker/cli/cli/config/credentials"
10+
"github.com/docker/cli/opts"
1011
"github.com/docker/docker/api/types"
1112
"gotest.tools/assert"
1213
is "gotest.tools/assert/cmp"
@@ -41,7 +42,7 @@ func TestProxyConfig(t *testing.T) {
4142
},
4243
}
4344

44-
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", []string{})
45+
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", nil)
4546
expected := map[string]*string{
4647
"HTTP_PROXY": &httpProxy,
4748
"http_proxy": &httpProxy,
@@ -75,10 +76,10 @@ func TestProxyConfigOverride(t *testing.T) {
7576
},
7677
}
7778

78-
ropts := []string{
79+
ropts := opts.ConvertKVStringsToMapWithNil([]string{
7980
fmt.Sprintf("HTTP_PROXY=%s", overrideHTTPProxy),
8081
"NO_PROXY=",
81-
}
82+
})
8283
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", ropts)
8384
expected := map[string]*string{
8485
"HTTP_PROXY": &overrideHTTPProxy,
@@ -124,7 +125,7 @@ func TestProxyConfigPerHost(t *testing.T) {
124125
},
125126
}
126127

127-
proxyConfig := cfg.ParseProxyConfig("tcp://example.docker.com:2376", []string{})
128+
proxyConfig := cfg.ParseProxyConfig("tcp://example.docker.com:2376", nil)
128129
expected := map[string]*string{
129130
"HTTP_PROXY": &extHTTPProxy,
130131
"http_proxy": &extHTTPProxy,

cli/config/credentials/credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package credentials
22

33
import (
4-
"github.com/docker/docker/api/types"
4+
"github.com/docker/cli/cli/config/types"
55
)
66

77
// Store is the interface that any credentials store must implement.

cli/config/credentials/file_store.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package credentials
22

33
import (
4-
"github.com/docker/docker/api/types"
5-
"github.com/docker/docker/registry"
4+
"strings"
5+
6+
"github.com/docker/cli/cli/config/types"
67
)
78

89
type store interface {
@@ -35,7 +36,7 @@ func (c *fileStore) Get(serverAddress string) (types.AuthConfig, error) {
3536
// Maybe they have a legacy config file, we will iterate the keys converting
3637
// them to the new format and testing
3738
for r, ac := range c.file.GetAuthConfigs() {
38-
if serverAddress == registry.ConvertToHostname(r) {
39+
if serverAddress == ConvertToHostname(r) {
3940
return ac, nil
4041
}
4142
}
@@ -62,3 +63,18 @@ func (c *fileStore) GetFilename() string {
6263
func (c *fileStore) IsFileStore() bool {
6364
return true
6465
}
66+
67+
// ConvertToHostname converts a registry url which has http|https prepended
68+
// to just an hostname.
69+
func ConvertToHostname(url string) string {
70+
stripped := url
71+
if strings.HasPrefix(url, "http://") {
72+
stripped = strings.TrimPrefix(url, "http://")
73+
} else if strings.HasPrefix(url, "https://") {
74+
stripped = strings.TrimPrefix(url, "https://")
75+
}
76+
77+
nameParts := strings.SplitN(stripped, "/", 2)
78+
79+
return nameParts[0]
80+
}

cli/config/credentials/native_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package credentials
22

33
import (
4+
"github.com/docker/cli/cli/config/types"
45
"github.com/docker/docker-credential-helpers/client"
56
"github.com/docker/docker-credential-helpers/credentials"
6-
"github.com/docker/docker/api/types"
77
)
88

99
const (

0 commit comments

Comments
 (0)