Skip to content

Commit 4ae7176

Browse files
committed
always add but hide experimental cmds and flags
Signed-off-by: Victor Vieux <vieux@docker.com> update cobra and use Tags Signed-off-by: Victor Vieux <vieux@docker.com> allow client to talk to an older server Signed-off-by: Victor Vieux <vieux@docker.com>
1 parent 086c47f commit 4ae7176

File tree

23 files changed

+75
-52
lines changed

23 files changed

+75
-52
lines changed

command/checkpoint/cmd.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package checkpoint
22

33
import (
4-
"fmt"
5-
64
"github.com/docker/docker/cli"
75
"github.com/docker/docker/cli/command"
86
"github.com/spf13/cobra"
@@ -15,9 +13,10 @@ func NewCheckpointCommand(dockerCli *command.DockerCli) *cobra.Command {
1513
Short: "Manage checkpoints",
1614
Args: cli.NoArgs,
1715
Run: func(cmd *cobra.Command, args []string) {
18-
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
16+
cmd.SetOutput(dockerCli.Err())
17+
cmd.HelpFunc()(cmd, args)
1918
},
20-
Tags: map[string]string{"experimental": ""},
19+
Tags: map[string]string{"experimental": "", "version": "1.25"},
2120
}
2221
cmd.AddCommand(
2322
newCreateCommand(dockerCli),

command/cli.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"runtime"
1111

1212
"github.com/docker/docker/api"
13+
"github.com/docker/docker/api/types/versions"
1314
cliflags "github.com/docker/docker/cli/flags"
1415
"github.com/docker/docker/cliconfig"
1516
"github.com/docker/docker/cliconfig/configfile"
@@ -32,21 +33,24 @@ type Streams interface {
3233
// DockerCli represents the docker command line client.
3334
// Instances of the client can be returned from NewDockerCli.
3435
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
4144
}
4245

43-
// HasExperimental returns true if experimental features are accessible
46+
// HasExperimental returns true if experimental features are accessible.
4447
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
5054
}
5155

5256
// Client returns the APIClient
@@ -93,12 +97,28 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error {
9397
if err != nil {
9498
return err
9599
}
100+
101+
cli.defaultVersion = cli.client.ClientVersion()
102+
96103
if opts.Common.TrustKey == "" {
97104
cli.keyFile = filepath.Join(cliconfig.ConfigDir(), cliflags.DefaultTrustKeyFile)
98105
} else {
99106
cli.keyFile = opts.Common.TrustKey
100107
}
101108

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+
}
102122
return nil
103123
}
104124

command/container/cmd.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package container
22

33
import (
4-
"fmt"
5-
64
"github.com/spf13/cobra"
75

86
"github.com/docker/docker/cli"
@@ -16,7 +14,8 @@ func NewContainerCommand(dockerCli *command.DockerCli) *cobra.Command {
1614
Short: "Manage containers",
1715
Args: cli.NoArgs,
1816
Run: func(cmd *cobra.Command, args []string) {
19-
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
17+
cmd.SetOutput(dockerCli.Err())
18+
cmd.HelpFunc()(cmd, args)
2019
},
2120
}
2221
cmd.AddCommand(

command/container/exec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func NewExecCommand(dockerCli *command.DockerCli) *cobra.Command {
5959
flags.StringVarP(&opts.user, "user", "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
6060
flags.BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the command")
6161
flags.VarP(opts.env, "env", "e", "Set environment variables")
62+
flags.SetAnnotation("env", "version", []string{"1.25"})
6263

6364
return cmd
6465
}

command/container/prune.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
3535
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
3636
return nil
3737
},
38+
Tags: map[string]string{"version": "1.25"},
3839
}
3940

4041
flags := cmd.Flags()

command/image/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func NewBuildCommand(dockerCli *command.DockerCli) *cobra.Command {
113113

114114
flags.BoolVar(&options.squash, "squash", false, "Squash newly built layers into a single new layer")
115115
flags.SetAnnotation("squash", "experimental", nil)
116+
flags.SetAnnotation("squash", "version", []string{"1.25"})
116117

117118
return cmd
118119
}
@@ -144,7 +145,7 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error {
144145
progBuff io.Writer
145146
buildBuff io.Writer
146147
)
147-
148+
148149
specifiedContext := options.context
149150
progBuff = dockerCli.Out()
150151
buildBuff = dockerCli.Out()

command/image/cmd.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package image
22

33
import (
4-
"fmt"
5-
64
"github.com/spf13/cobra"
75

86
"github.com/docker/docker/cli"
@@ -16,7 +14,8 @@ func NewImageCommand(dockerCli *command.DockerCli) *cobra.Command {
1614
Short: "Manage images",
1715
Args: cli.NoArgs,
1816
Run: func(cmd *cobra.Command, args []string) {
19-
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
17+
cmd.SetOutput(dockerCli.Err())
18+
cmd.HelpFunc()(cmd, args)
2019
},
2120
}
2221
cmd.AddCommand(
@@ -33,6 +32,5 @@ func NewImageCommand(dockerCli *command.DockerCli) *cobra.Command {
3332
newInspectCommand(dockerCli),
3433
NewPruneCommand(dockerCli),
3534
)
36-
3735
return cmd
3836
}

command/image/prune.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
3636
fmt.Fprintln(dockerCli.Out(), "Total reclaimed space:", units.HumanSize(float64(spaceReclaimed)))
3737
return nil
3838
},
39+
Tags: map[string]string{"version": "1.25"},
3940
}
4041

4142
flags := cmd.Flags()

command/network/cmd.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package network
22

33
import (
4-
"fmt"
5-
64
"github.com/spf13/cobra"
75

86
"github.com/docker/docker/cli"
@@ -16,7 +14,8 @@ func NewNetworkCommand(dockerCli *command.DockerCli) *cobra.Command {
1614
Short: "Manage networks",
1715
Args: cli.NoArgs,
1816
Run: func(cmd *cobra.Command, args []string) {
19-
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString())
17+
cmd.SetOutput(dockerCli.Err())
18+
cmd.HelpFunc()(cmd, args)
2019
},
2120
}
2221
cmd.AddCommand(

command/network/prune.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
3333
}
3434
return nil
3535
},
36+
Tags: map[string]string{"version": "1.25"},
3637
}
3738

3839
flags := cmd.Flags()

0 commit comments

Comments
 (0)