@@ -10,6 +10,7 @@ import (
10
10
"runtime"
11
11
12
12
"github.com/docker/docker/api"
13
+ "github.com/docker/docker/api/types/versions"
13
14
cliflags "github.com/docker/docker/cli/flags"
14
15
"github.com/docker/docker/cliconfig"
15
16
"github.com/docker/docker/cliconfig/configfile"
@@ -32,21 +33,24 @@ type Streams interface {
32
33
// DockerCli represents the docker command line client.
33
34
// Instances of the client can be returned from NewDockerCli.
34
35
type DockerCli struct {
35
- configFile * configfile.ConfigFile
36
- in * InStream
37
- out * OutStream
38
- err io.Writer
39
- keyFile string
40
- client client.APIClient
36
+ configFile * configfile.ConfigFile
37
+ in * InStream
38
+ out * OutStream
39
+ err io.Writer
40
+ keyFile string
41
+ client client.APIClient
42
+ hasExperimental bool
43
+ defaultVersion string
41
44
}
42
45
43
- // HasExperimental returns true if experimental features are accessible
46
+ // HasExperimental returns true if experimental features are accessible.
44
47
func (cli * DockerCli ) HasExperimental () bool {
45
- if cli .client == nil {
46
- return false
47
- }
48
- enabled , _ := cli .client .Ping (context .Background ())
49
- return enabled
48
+ return cli .hasExperimental
49
+ }
50
+
51
+ // DefaultVersion returns api.defaultVersion of DOCKER_API_VERSION if specified.
52
+ func (cli * DockerCli ) DefaultVersion () string {
53
+ return cli .defaultVersion
50
54
}
51
55
52
56
// Client returns the APIClient
@@ -93,12 +97,28 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
93
97
if err != nil {
94
98
return err
95
99
}
100
+
101
+ cli .defaultVersion = cli .client .ClientVersion ()
102
+
96
103
if opts .Common .TrustKey == "" {
97
104
cli .keyFile = filepath .Join (cliconfig .ConfigDir (), cliflags .DefaultTrustKeyFile )
98
105
} else {
99
106
cli .keyFile = opts .Common .TrustKey
100
107
}
101
108
109
+ if ping , err := cli .client .Ping (context .Background ()); err == nil {
110
+ cli .hasExperimental = ping .Experimental
111
+
112
+ // since the new header was added in 1.25, assume server is 1.24 if header is not present.
113
+ if ping .APIVersion == "" {
114
+ ping .APIVersion = "1.24"
115
+ }
116
+
117
+ // if server version is lower than the current cli, downgrade
118
+ if versions .LessThan (ping .APIVersion , cli .client .ClientVersion ()) {
119
+ cli .client .UpdateClientVersion (ping .APIVersion )
120
+ }
121
+ }
102
122
return nil
103
123
}
104
124
0 commit comments