diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 9906f63b22..736ee568ea 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -86,8 +86,11 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command { // with hostname flags.Bool("help", false, "Print usage") - command.AddPlatformFlag(flags, &options.platform) - command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled()) + // TODO(thaJeztah): consider adding platform as "image create option" on containerOptions + addPlatformFlag(flags, &options.platform) + _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) + + flags.BoolVar(&options.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image verification") copts = addFlags(flags) addCompletions(cmd, dockerCli) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index 647dc5d510..16b9176db9 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -141,6 +141,16 @@ type containerOptions struct { Args []string } +// addPlatformFlag adds "--platform" to a set of flags for API version 1.32 and +// later, using the value of "DOCKER_DEFAULT_PLATFORM" (if set) as a default. +// +// It should not be used for new uses, which may have a different API version +// requirement. +func addPlatformFlag(flags *pflag.FlagSet, target *string) { + flags.StringVar(target, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable") + _ = flags.SetAnnotation("platform", "version", []string{"1.32"}) +} + // addFlags adds all command line flags that will be used by parse to the FlagSet func addFlags(flags *pflag.FlagSet) *containerOptions { copts := &containerOptions{ diff --git a/cli/command/container/run.go b/cli/command/container/run.go index b86ad9d5b2..6c5748a67a 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -66,8 +66,9 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command { // with hostname flags.Bool("help", false, "Print usage") - command.AddPlatformFlag(flags, &options.platform) - command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled()) + // TODO(thaJeztah): consider adding platform as "image create option" on containerOptions + addPlatformFlag(flags, &options.platform) + flags.BoolVar(&options.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image verification") copts = addFlags(flags) _ = cmd.RegisterFlagCompletionFunc("detach-keys", completeDetachKeys) diff --git a/cli/command/image/build.go b/cli/command/image/build.go index 6c6db76675..15448e7b49 100644 --- a/cli/command/image/build.go +++ b/cli/command/image/build.go @@ -145,7 +145,7 @@ func NewBuildCommand(dockerCli command.Cli) *cobra.Command { flags.SetAnnotation("target", annotation.ExternalURL, []string{"https://docs.docker.com/reference/cli/docker/buildx/build/#target"}) flags.StringVar(&options.imageIDFile, "iidfile", "", "Write the image ID to the file") - command.AddTrustVerificationFlags(flags, &options.untrusted, dockerCli.ContentTrustEnabled()) + flags.BoolVar(&options.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image verification") flags.StringVar(&options.platform, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable") flags.SetAnnotation("platform", "version", []string{"1.38"}) diff --git a/cli/command/image/import.go b/cli/command/image/import.go index a55ea4ab5b..f0da5c6a47 100644 --- a/cli/command/image/import.go +++ b/cli/command/image/import.go @@ -47,7 +47,7 @@ func NewImportCommand(dockerCli command.Cli) *cobra.Command { options.changes = dockeropts.NewListOpts(nil) flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image") flags.StringVarP(&options.message, "message", "m", "", "Set commit message for imported image") - command.AddPlatformFlag(flags, &options.platform) + addPlatformFlag(flags, &options.platform) _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) return cmd diff --git a/cli/command/image/opts.go b/cli/command/image/opts.go new file mode 100644 index 0000000000..37378b6ece --- /dev/null +++ b/cli/command/image/opts.go @@ -0,0 +1,17 @@ +package image + +import ( + "os" + + "github.com/spf13/pflag" +) + +// addPlatformFlag adds "--platform" to a set of flags for API version 1.32 and +// later, using the value of "DOCKER_DEFAULT_PLATFORM" (if set) as a default. +// +// It should not be used for new uses, which may have a different API version +// requirement. +func addPlatformFlag(flags *pflag.FlagSet, target *string) { + flags.StringVar(target, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable") + _ = flags.SetAnnotation("platform", "version", []string{"1.32"}) +} diff --git a/cli/command/image/pull.go b/cli/command/image/pull.go index 235e3a7a17..c916bad24f 100644 --- a/cli/command/image/pull.go +++ b/cli/command/image/pull.go @@ -50,8 +50,8 @@ func NewPullCommand(dockerCli command.Cli) *cobra.Command { flags.BoolVarP(&opts.all, "all-tags", "a", false, "Download all tagged images in the repository") flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output") - command.AddPlatformFlag(flags, &opts.platform) - command.AddTrustVerificationFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) + addPlatformFlag(flags, &opts.platform) + flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image verification") _ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) diff --git a/cli/command/image/push.go b/cli/command/image/push.go index cd79ff9289..1ef31aedd7 100644 --- a/cli/command/image/push.go +++ b/cli/command/image/push.go @@ -57,7 +57,7 @@ func NewPushCommand(dockerCli command.Cli) *cobra.Command { flags := cmd.Flags() flags.BoolVarP(&opts.all, "all-tags", "a", false, "Push all tags of an image to the repository") flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress verbose output") - command.AddTrustSigningFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) + flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image signing") // Don't default to DOCKER_DEFAULT_PLATFORM env variable, always default to // pushing the image as-is. This also avoids forcing the platform selection diff --git a/cli/command/plugin/install.go b/cli/command/plugin/install.go index 33a7179edd..31f123ddfe 100644 --- a/cli/command/plugin/install.go +++ b/cli/command/plugin/install.go @@ -31,7 +31,7 @@ type pluginOptions struct { func loadPullFlags(dockerCli command.Cli, opts *pluginOptions, flags *pflag.FlagSet) { flags.BoolVar(&opts.grantPerms, "grant-all-permissions", false, "Grant all permissions necessary to run the plugin") - command.AddTrustVerificationFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) + flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image verification") } func newInstallCommand(dockerCli command.Cli) *cobra.Command { diff --git a/cli/command/plugin/push.go b/cli/command/plugin/push.go index 573ae3d0ae..f70e36910c 100644 --- a/cli/command/plugin/push.go +++ b/cli/command/plugin/push.go @@ -33,7 +33,7 @@ func newPushCommand(dockerCli command.Cli) *cobra.Command { flags := cmd.Flags() - command.AddTrustSigningFlags(flags, &opts.untrusted, dockerCli.ContentTrustEnabled()) + flags.BoolVar(&opts.untrusted, "disable-content-trust", !dockerCli.ContentTrustEnabled(), "Skip image signing") return cmd } diff --git a/cli/command/trust.go b/cli/command/trust.go deleted file mode 100644 index 65f2408585..0000000000 --- a/cli/command/trust.go +++ /dev/null @@ -1,15 +0,0 @@ -package command - -import ( - "github.com/spf13/pflag" -) - -// AddTrustVerificationFlags adds content trust flags to the provided flagset -func AddTrustVerificationFlags(fs *pflag.FlagSet, v *bool, trusted bool) { - fs.BoolVar(v, "disable-content-trust", !trusted, "Skip image verification") -} - -// AddTrustSigningFlags adds "signing" flags to the provided flagset -func AddTrustSigningFlags(fs *pflag.FlagSet, v *bool, trusted bool) { - fs.BoolVar(v, "disable-content-trust", !trusted, "Skip image signing") -} diff --git a/cli/command/utils.go b/cli/command/utils.go index f024ffd278..5987b53717 100644 --- a/cli/command/utils.go +++ b/cli/command/utils.go @@ -15,7 +15,6 @@ import ( "github.com/docker/cli/internal/prompt" "github.com/docker/docker/api/types/filters" "github.com/pkg/errors" - "github.com/spf13/pflag" ) const ErrPromptTerminated = prompt.ErrTerminated @@ -94,12 +93,6 @@ func PruneFilters(dockerCLI config.Provider, pruneFilters filters.Args) filters. return pruneFilters } -// AddPlatformFlag adds `platform` to a set of flags for API version 1.32 and later. -func AddPlatformFlag(flags *pflag.FlagSet, target *string) { - flags.StringVar(target, "platform", os.Getenv("DOCKER_DEFAULT_PLATFORM"), "Set platform if server is multi-platform capable") - _ = flags.SetAnnotation("platform", "version", []string{"1.32"}) -} - // ValidateOutputPath validates the output paths of the "docker cp" command. func ValidateOutputPath(path string) error { dir := filepath.Dir(filepath.Clean(path))