Skip to content

Commit 2344627

Browse files
authored
Merge pull request #1642 from tiborvass/better-dep-graph-auth-type
Remove docker api dependency from cli/config
2 parents 2e5639d + 27b2797 commit 2344627

File tree

17 files changed

+89
-35
lines changed

17 files changed

+89
-35
lines changed

cli/command/container/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command {
6262
}
6363

6464
func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, options *createOptions, copts *containerOptions) error {
65-
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), copts.env.GetAll())
65+
proxyConfig := dockerCli.ConfigFile().ParseProxyConfig(dockerCli.Client().DaemonHost(), opts.ConvertKVStringsToMapWithNil(copts.env.GetAll()))
6666
newEnv := []string{}
6767
for k, v := range proxyConfig {
6868
if v == nil {

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/cli/cli/streams"
1617
"github.com/docker/distribution/reference"
@@ -77,7 +78,7 @@ func ResolveAuthConfig(ctx context.Context, cli Cli, index *registrytypes.IndexI
7778
}
7879

7980
a, _ := cli.ConfigFile().GetAuthConfig(configKey)
80-
return a
81+
return types.AuthConfig(a)
8182
}
8283

8384
// GetDefaultAuthConfig gets the default auth config given a serverAddress
@@ -86,16 +87,17 @@ func GetDefaultAuthConfig(cli Cli, checkCredStore bool, serverAddress string, is
8687
if !isDefaultRegistry {
8788
serverAddress = registry.ConvertToHostname(serverAddress)
8889
}
89-
var authconfig types.AuthConfig
90+
var authconfig configtypes.AuthConfig
9091
var err error
9192
if checkCredStore {
9293
authconfig, err = cli.ConfigFile().GetAuthConfig(serverAddress)
9394
} else {
94-
authconfig = types.AuthConfig{}
95+
authconfig = configtypes.AuthConfig{}
9596
}
9697
authconfig.ServerAddress = serverAddress
9798
authconfig.IdentityToken = ""
98-
return &authconfig, err
99+
res := types.AuthConfig(authconfig)
100+
return &res, err
99101
}
100102

101103
// 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/command/registry/login_test.go

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

9+
configtypes "github.com/docker/cli/cli/config/types"
910
"github.com/docker/cli/internal/test"
1011
"github.com/docker/docker/api/types"
1112
registrytypes "github.com/docker/docker/api/types/registry"
@@ -79,21 +80,21 @@ func TestRunLogin(t *testing.T) {
7980
const validPassword = "p1"
8081
const validPassword2 = "p2"
8182

82-
validAuthConfig := types.AuthConfig{
83+
validAuthConfig := configtypes.AuthConfig{
8384
ServerAddress: storedServerAddress,
8485
Username: validUsername,
8586
Password: validPassword,
8687
}
87-
expiredAuthConfig := types.AuthConfig{
88+
expiredAuthConfig := configtypes.AuthConfig{
8889
ServerAddress: storedServerAddress,
8990
Username: validUsername,
9091
Password: expiredPassword,
9192
}
9293
testCases := []struct {
9394
inputLoginOption loginOptions
94-
inputStoredCred *types.AuthConfig
95+
inputStoredCred *configtypes.AuthConfig
9596
expectedErr string
96-
expectedSavedCred types.AuthConfig
97+
expectedSavedCred configtypes.AuthConfig
9798
}{
9899
{
99100
inputLoginOption: loginOptions{
@@ -118,7 +119,7 @@ func TestRunLogin(t *testing.T) {
118119
},
119120
inputStoredCred: &validAuthConfig,
120121
expectedErr: "",
121-
expectedSavedCred: types.AuthConfig{
122+
expectedSavedCred: configtypes.AuthConfig{
122123
ServerAddress: storedServerAddress,
123124
Username: validUsername,
124125
Password: validPassword2,

cli/command/registry_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
// Prevents a circular import with "github.com/docker/cli/internal/test"
1414

1515
. "github.com/docker/cli/cli/command"
16+
configtypes "github.com/docker/cli/cli/config/types"
1617
"github.com/docker/cli/cli/debug"
1718
"github.com/docker/cli/internal/test"
1819
"github.com/docker/docker/api/types"
@@ -134,7 +135,7 @@ func TestGetDefaultAuthConfig(t *testing.T) {
134135
errBuf := new(bytes.Buffer)
135136
cli.SetErr(errBuf)
136137
for _, authconfig := range testAuthConfigs {
137-
cli.ConfigFile().GetCredentialsStore(authconfig.ServerAddress).Store(authconfig)
138+
cli.ConfigFile().GetCredentialsStore(authconfig.ServerAddress).Store(configtypes.AuthConfig(authconfig))
138139
}
139140
for _, tc := range testCases {
140141
serverAddress := tc.inputServerAddress

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

@@ -196,7 +195,7 @@ func (configFile *ConfigFile) Save() error {
196195

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

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

cli/config/configfile/file_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package configfile
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"os"
76
"testing"
87

98
"github.com/docker/cli/cli/config/credentials"
10-
"github.com/docker/docker/api/types"
9+
"github.com/docker/cli/cli/config/types"
1110
"gotest.tools/assert"
1211
is "gotest.tools/assert/cmp"
1312
)
@@ -41,7 +40,7 @@ func TestProxyConfig(t *testing.T) {
4140
},
4241
}
4342

44-
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", []string{})
43+
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", nil)
4544
expected := map[string]*string{
4645
"HTTP_PROXY": &httpProxy,
4746
"http_proxy": &httpProxy,
@@ -75,9 +74,14 @@ func TestProxyConfigOverride(t *testing.T) {
7574
},
7675
}
7776

78-
ropts := []string{
79-
fmt.Sprintf("HTTP_PROXY=%s", overrideHTTPProxy),
80-
"NO_PROXY=",
77+
clone := func(s string) *string {
78+
s2 := s
79+
return &s2
80+
}
81+
82+
ropts := map[string]*string{
83+
"HTTP_PROXY": clone(overrideHTTPProxy),
84+
"NO_PROXY": clone(overrideNoProxy),
8185
}
8286
proxyConfig := cfg.ParseProxyConfig("/var/run/docker.sock", ropts)
8387
expected := map[string]*string{
@@ -124,7 +128,7 @@ func TestProxyConfigPerHost(t *testing.T) {
124128
},
125129
}
126130

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

0 commit comments

Comments
 (0)