Skip to content

Commit 7026e68

Browse files
committed
cli/command: remove AddPlatformFlag utility
It was only used internally and has no external users. It should not be used for new uses, because it also adds a minimum API version constraint and a default from env-var, which must be evaluated for each individual use of such flags. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent c0fbbe0 commit 7026e68

File tree

7 files changed

+35
-11
lines changed

7 files changed

+35
-11
lines changed

cli/command/container/create.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command {
8686
// with hostname
8787
flags.Bool("help", false, "Print usage")
8888

89-
command.AddPlatformFlag(flags, &options.platform)
89+
// TODO(thaJeztah): consider adding platform as "image create option" on containerOptions
90+
addPlatformFlag(flags, &options.platform)
91+
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
92+
9093
flags.BoolVar(&options.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image verification")
9194
copts = addFlags(flags)
9295

cli/command/container/opts.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ type containerOptions struct {
141141
Args []string
142142
}
143143

144+
// addPlatformFlag adds "--platform" to a set of flags for API version 1.32 and
145+
// later, using the value of "DOCKER_DEFAULT_PLATFORM" (if set) as a default.
146+
//
147+
// It should not be used for new uses, which may have a different API version
148+
// requirement.
149+
func addPlatformFlag(flags *pflag.FlagSet, target *string) {
150+
flags.StringVar(target, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable")
151+
_ = flags.SetAnnotation("platform", "version", []string{"1.32"})
152+
}
153+
144154
// addFlags adds all command line flags that will be used by parse to the FlagSet
145155
func addFlags(flags *pflag.FlagSet) *containerOptions {
146156
copts := &containerOptions{

cli/command/container/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command {
6666
// with hostname
6767
flags.Bool("help", false, "Print usage")
6868

69-
command.AddPlatformFlag(flags, &options.platform)
69+
// TODO(thaJeztah): consider adding platform as "image create option" on containerOptions
70+
addPlatformFlag(flags, &options.platform)
7071
flags.BoolVar(&options.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image verification")
7172
copts = addFlags(flags)
7273

cli/command/image/import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewImportCommand(dockerCli command.Cli) *cobra.Command {
4747
options.changes = dockeropts.NewListOpts(nil)
4848
flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image")
4949
flags.StringVarP(&options.message, "message", "m", "", "Set commit message for imported image")
50-
command.AddPlatformFlag(flags, &options.platform)
50+
addPlatformFlag(flags, &options.platform)
5151
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
5252

5353
return cmd

cli/command/image/opts.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package image
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/pflag"
7+
)
8+
9+
// addPlatformFlag adds "--platform" to a set of flags for API version 1.32 and
10+
// later, using the value of "DOCKER_DEFAULT_PLATFORM" (if set) as a default.
11+
//
12+
// It should not be used for new uses, which may have a different API version
13+
// requirement.
14+
func addPlatformFlag(flags *pflag.FlagSet, target *string) {
15+
flags.StringVar(target, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable")
16+
_ = flags.SetAnnotation("platform", "version", []string{"1.32"})
17+
}

cli/command/image/pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewPullCommand(dockerCli command.Cli) *cobra.Command {
5050
flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository")
5151
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output")
5252

53-
command.AddPlatformFlag(flags, &opts.platform)
53+
addPlatformFlag(flags, &opts.platform)
5454
flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image verification")
5555

5656
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)

cli/command/utils.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/docker/cli/cli/config"
1212
"github.com/moby/moby/api/types/filters"
1313
"github.com/pkg/errors"
14-
"github.com/spf13/pflag"
1514
)
1615

1716
// PruneFilters merges prune filters specified in config.json with those specified
@@ -55,12 +54,6 @@ func PruneFilters(dockerCLI config.Provider, pruneFilters filters.Args) filters.
5554
return pruneFilters
5655
}
5756

58-
// AddPlatformFlag adds `platform` to a set of flags for API version 1.32 and later.
59-
func AddPlatformFlag(flags *pflag.FlagSet, target *string) {
60-
flags.StringVar(target, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable")
61-
_ = flags.SetAnnotation("platform", "version", []string{"1.32"})
62-
}
63-
6457
// ValidateOutputPath validates the output paths of the "docker cp" command.
6558
func ValidateOutputPath(path string) error {
6659
dir := filepath.Dir(filepath.Clean(path))

0 commit comments

Comments
 (0)