Skip to content

Commit 56580e7

Browse files
authored
Merge pull request #3885 from thaJeztah/context_lazy_evaluate_step2
cli/command: add Cli.CurrentVersion() function
2 parents cbf0522 + a7e2c3e commit 56580e7

5 files changed

Lines changed: 25 additions & 11 deletions

File tree

cli/command/cli.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Cli interface {
5555
ServerInfo() ServerInfo
5656
NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error)
5757
DefaultVersion() string
58+
CurrentVersion() string
5859
ManifestStore() manifeststore.Store
5960
RegistryClient(bool) registryclient.RegistryClient
6061
ContentTrustEnabled() bool
@@ -86,6 +87,15 @@ func (cli *DockerCli) DefaultVersion() string {
8687
return api.DefaultVersion
8788
}
8889

90+
// CurrentVersion returns the API version currently negotiated, or the default
91+
// version otherwise.
92+
func (cli *DockerCli) CurrentVersion() string {
93+
if cli.client == nil {
94+
return api.DefaultVersion
95+
}
96+
return cli.client.ClientVersion()
97+
}
98+
8999
// Client returns the APIClient
90100
func (cli *DockerCli) Client() client.APIClient {
91101
return cli.client

cli/command/container/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func runRun(dockerCli command.Cli, flags *pflag.FlagSet, ropts *runOptions, copt
111111
reportError(dockerCli.Err(), "run", err.Error(), true)
112112
return cli.StatusError{StatusCode: 125}
113113
}
114-
if err = validateAPIVersion(containerConfig, dockerCli.Client().ClientVersion()); err != nil {
114+
if err = validateAPIVersion(containerConfig, dockerCli.CurrentVersion()); err != nil {
115115
reportError(dockerCli.Err(), "run", err.Error(), true)
116116
return cli.StatusError{StatusCode: 125}
117117
}

cli/command/system/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func runVersion(dockerCli command.Cli, opts *versionOptions) error {
136136
Client: clientVersion{
137137
Platform: struct{ Name string }{version.PlatformName},
138138
Version: version.Version,
139-
APIVersion: dockerCli.Client().ClientVersion(),
139+
APIVersion: dockerCli.CurrentVersion(),
140140
DefaultAPIVersion: dockerCli.DefaultVersion(),
141141
GoVersion: runtime.Version(),
142142
GitCommit: version.GitCommit,

cmd/docker/docker.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
cliflags "github.com/docker/cli/cli/flags"
1515
"github.com/docker/cli/cli/version"
1616
"github.com/docker/docker/api/types/versions"
17-
"github.com/docker/docker/client"
1817
"github.com/moby/buildkit/util/appcontext"
1918
"github.com/pkg/errors"
2019
"github.com/sirupsen/logrus"
@@ -274,7 +273,7 @@ func main() {
274273
}
275274

276275
type versionDetails interface {
277-
Client() client.APIClient
276+
CurrentVersion() string
278277
ServerInfo() command.ServerInfo
279278
}
280279

@@ -333,7 +332,7 @@ func hideUnsupportedFeatures(cmd *cobra.Command, details versionDetails) error {
333332
return false
334333
}
335334
}
336-
versionOlderThan = func(v string) bool { return versions.LessThan(details.Client().ClientVersion(), v) }
335+
versionOlderThan = func(v string) bool { return versions.LessThan(details.CurrentVersion(), v) }
337336
)
338337

339338
cmd.Flags().VisitAll(func(f *pflag.Flag) {
@@ -390,8 +389,8 @@ func areFlagsSupported(cmd *cobra.Command, details versionDetails) error {
390389
if !f.Changed {
391390
return
392391
}
393-
if !isVersionSupported(f, details.Client().ClientVersion()) {
394-
errs = append(errs, fmt.Sprintf(`"--%s" requires API version %s, but the Docker daemon API version is %s`, f.Name, getFlagAnnotation(f, "version"), details.Client().ClientVersion()))
392+
if !isVersionSupported(f, details.CurrentVersion()) {
393+
errs = append(errs, fmt.Sprintf(`"--%s" requires API version %s, but the Docker daemon API version is %s`, f.Name, getFlagAnnotation(f, "version"), details.CurrentVersion()))
395394
return
396395
}
397396
if !isOSTypeSupported(f, details.ServerInfo().OSType) {
@@ -417,11 +416,11 @@ func areFlagsSupported(cmd *cobra.Command, details versionDetails) error {
417416
func areSubcommandsSupported(cmd *cobra.Command, details versionDetails) error {
418417
// Check recursively so that, e.g., `docker stack ls` returns the same output as `docker stack`
419418
for curr := cmd; curr != nil; curr = curr.Parent() {
420-
if cmdVersion, ok := curr.Annotations["version"]; ok && versions.LessThan(details.Client().ClientVersion(), cmdVersion) {
421-
return fmt.Errorf("%s requires API version %s, but the Docker daemon API version is %s", cmd.CommandPath(), cmdVersion, details.Client().ClientVersion())
419+
if cmdVersion, ok := curr.Annotations["version"]; ok && versions.LessThan(details.CurrentVersion(), cmdVersion) {
420+
return fmt.Errorf("%s requires API version %s, but the Docker daemon API version is %s", cmd.CommandPath(), cmdVersion, details.CurrentVersion())
422421
}
423-
if os, ok := curr.Annotations["ostype"]; ok && os != details.ServerInfo().OSType {
424-
return fmt.Errorf("%s is only supported on a Docker daemon running on %s, but the Docker daemon is running on %s", cmd.CommandPath(), os, details.ServerInfo().OSType)
422+
if ost, ok := curr.Annotations["ostype"]; ok && ost != details.ServerInfo().OSType {
423+
return fmt.Errorf("%s is only supported on a Docker daemon running on %s, but the Docker daemon is running on %s", cmd.CommandPath(), ost, details.ServerInfo().OSType)
425424
}
426425
if _, ok := curr.Annotations["experimental"]; ok && !details.ServerInfo().HasExperimental {
427426
return fmt.Errorf("%s is only supported on a Docker daemon with experimental features enabled", cmd.CommandPath())

internal/test/cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func (c *FakeCli) Client() client.APIClient {
101101
return c.client
102102
}
103103

104+
// CurrentVersion returns the API version used by FakeCli.
105+
func (c *FakeCli) CurrentVersion() string {
106+
return c.DefaultVersion()
107+
}
108+
104109
// Out returns the output stream (stdout) the cli should write on
105110
func (c *FakeCli) Out() *streams.Out {
106111
return c.out

0 commit comments

Comments
 (0)