Merge pull request #6329 from Benehiko/cli/commands

Register CLI commands implicitly
This commit is contained in:
Sebastiaan van Stijn
2025-08-21 09:23:30 +02:00
committed by GitHub
21 changed files with 162 additions and 159 deletions
+8
View File
@@ -6,8 +6,16 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/image"
"github.com/docker/cli/internal/commands"
)
func init() {
commands.Register(newBuilderCommand)
commands.Register(func(c command.Cli) *cobra.Command {
return newBakeStubCommand(c)
})
}
// NewBuilderCommand returns a cobra command for `builder` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -3,9 +3,14 @@ package checkpoint
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newCheckpointCommand)
}
// NewCheckpointCommand returns the `checkpoint` subcommand (only in experimental)
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+22 -159
View File
@@ -1,168 +1,31 @@
package commands
import (
"os"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/builder"
"github.com/docker/cli/cli/command/checkpoint"
"github.com/docker/cli/cli/command/config"
"github.com/docker/cli/cli/command/container"
"github.com/docker/cli/cli/command/context"
"github.com/docker/cli/cli/command/image"
"github.com/docker/cli/cli/command/manifest"
"github.com/docker/cli/cli/command/network"
"github.com/docker/cli/cli/command/node"
"github.com/docker/cli/cli/command/plugin"
"github.com/docker/cli/cli/command/registry"
"github.com/docker/cli/cli/command/secret"
"github.com/docker/cli/cli/command/service"
"github.com/docker/cli/cli/command/stack"
"github.com/docker/cli/cli/command/swarm"
"github.com/docker/cli/cli/command/system"
"github.com/docker/cli/cli/command/trust"
"github.com/docker/cli/cli/command/volume"
_ "github.com/docker/cli/cli/command/builder"
_ "github.com/docker/cli/cli/command/checkpoint"
_ "github.com/docker/cli/cli/command/config"
_ "github.com/docker/cli/cli/command/container"
_ "github.com/docker/cli/cli/command/context"
_ "github.com/docker/cli/cli/command/image"
_ "github.com/docker/cli/cli/command/manifest"
_ "github.com/docker/cli/cli/command/network"
_ "github.com/docker/cli/cli/command/node"
_ "github.com/docker/cli/cli/command/plugin"
_ "github.com/docker/cli/cli/command/registry"
_ "github.com/docker/cli/cli/command/secret"
_ "github.com/docker/cli/cli/command/service"
_ "github.com/docker/cli/cli/command/stack"
_ "github.com/docker/cli/cli/command/swarm"
_ "github.com/docker/cli/cli/command/system"
_ "github.com/docker/cli/cli/command/trust"
_ "github.com/docker/cli/cli/command/volume"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
// AddCommands adds all the commands from cli/command to the root command
func AddCommands(cmd *cobra.Command, dockerCli command.Cli) {
cmd.AddCommand(
// commonly used shorthands
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
container.NewRunCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
container.NewExecCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
container.NewPsCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
image.NewBuildCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
image.NewPullCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
image.NewPushCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
image.NewImagesCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
registry.NewLoginCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
registry.NewLogoutCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
registry.NewSearchCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
system.NewVersionCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
system.NewInfoCommand(dockerCli),
// management commands
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
builder.NewBakeStubCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
builder.NewBuilderCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
checkpoint.NewCheckpointCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
container.NewContainerCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
context.NewContextCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
image.NewImageCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
manifest.NewManifestCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
network.NewNetworkCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
plugin.NewPluginCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
system.NewSystemCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
trust.NewTrustCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
volume.NewVolumeCommand(dockerCli),
// orchestration (swarm) commands
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
config.NewConfigCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
node.NewNodeCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
secret.NewSecretCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
service.NewServiceCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
stack.NewStackCommand(dockerCli),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
swarm.NewSwarmCommand(dockerCli),
// legacy commands may be hidden
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewAttachCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewCommitCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewCopyCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewCreateCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewDiffCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewExportCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewKillCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewLogsCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewPauseCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewPortCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewRenameCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewRestartCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewRmCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewStartCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewStatsCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewStopCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewTopCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewUnpauseCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewUpdateCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(container.NewWaitCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(image.NewHistoryCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(image.NewImportCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(image.NewLoadCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(image.NewRemoveCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(image.NewSaveCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(image.NewTagCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(system.NewEventsCommand(dockerCli)),
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
hide(system.NewInspectCommand(dockerCli)),
)
}
func hide(cmd *cobra.Command) *cobra.Command {
// If the environment variable with name "DOCKER_HIDE_LEGACY_COMMANDS" is not empty,
// these legacy commands (such as `docker ps`, `docker exec`, etc)
// will not be shown in output console.
if os.Getenv("DOCKER_HIDE_LEGACY_COMMANDS") == "" {
return cmd
func AddCommands(cmd *cobra.Command, dockerCLI command.Cli) {
for _, c := range commands.Commands() {
cmd.AddCommand(c(dockerCLI))
}
cmdCopy := *cmd
cmdCopy.Hidden = true
cmdCopy.Aliases = []string{}
return &cmdCopy
}
+5
View File
@@ -4,10 +4,15 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/internal/commands"
"github.com/moby/moby/api/types/swarm"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newConfigCommand)
}
// NewConfigCommand returns a cobra command for `config` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+28
View File
@@ -3,9 +3,37 @@ package container
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newRunCommand)
commands.Register(newExecCommand)
commands.Register(newPsCommand)
commands.Register(newContainerCommand)
commands.RegisterLegacy(newAttachCommand)
commands.RegisterLegacy(newCommitCommand)
commands.RegisterLegacy(newCopyCommand)
commands.RegisterLegacy(newCreateCommand)
commands.RegisterLegacy(newDiffCommand)
commands.RegisterLegacy(newExportCommand)
commands.RegisterLegacy(newKillCommand)
commands.RegisterLegacy(newLogsCommand)
commands.RegisterLegacy(newPauseCommand)
commands.RegisterLegacy(newPortCommand)
commands.RegisterLegacy(newRenameCommand)
commands.RegisterLegacy(newRestartCommand)
commands.RegisterLegacy(newRmCommand)
commands.RegisterLegacy(newStartCommand)
commands.RegisterLegacy(newStatsCommand)
commands.RegisterLegacy(newStopCommand)
commands.RegisterLegacy(newTopCommand)
commands.RegisterLegacy(newUnpauseCommand)
commands.RegisterLegacy(newUpdateCommand)
commands.RegisterLegacy(newWaitCommand)
}
// NewContainerCommand returns a cobra command for `container` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -3,9 +3,14 @@ package context
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newContextCommand)
}
// NewContextCommand returns the context cli subcommand
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+15
View File
@@ -3,9 +3,24 @@ package image
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newBuildCommand)
commands.Register(newPullCommand)
commands.Register(newPushCommand)
commands.Register(newImagesCommand)
commands.Register(newImageCommand)
commands.RegisterLegacy(newHistoryCommand)
commands.RegisterLegacy(newImportCommand)
commands.RegisterLegacy(newLoadCommand)
commands.RegisterLegacy(newRemoveCommand)
commands.RegisterLegacy(newSaveCommand)
commands.RegisterLegacy(newTagCommand)
}
// NewImageCommand returns a cobra command for `image` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -5,10 +5,15 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newManifestCommand)
}
// NewManifestCommand returns a cobra command for `manifest` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -3,9 +3,14 @@ package network
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newNetworkCommand)
}
// NewNetworkCommand returns a cobra command for `network` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -6,11 +6,16 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newNodeCommand)
}
// NewNodeCommand returns a cobra command for `node` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -3,9 +3,14 @@ package plugin
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newPluginCommand)
}
// NewPluginCommand returns a cobra command for `plugin` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -15,6 +15,7 @@ import (
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/config/configfile"
configtypes "github.com/docker/cli/cli/config/types"
"github.com/docker/cli/internal/commands"
"github.com/docker/cli/internal/oauth/manager"
"github.com/docker/cli/internal/registry"
"github.com/docker/cli/internal/tui"
@@ -24,6 +25,10 @@ import (
"github.com/spf13/pflag"
)
func init() {
commands.Register(newLoginCommand)
}
type loginOptions struct {
serverAddress string
user string
+5
View File
@@ -7,11 +7,16 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/config/credentials"
"github.com/docker/cli/internal/commands"
"github.com/docker/cli/internal/oauth/manager"
"github.com/docker/cli/internal/registry"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newLogoutCommand)
}
// NewLogoutCommand creates a new `docker logout` command
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -8,11 +8,16 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/internal/commands"
"github.com/docker/cli/opts"
registrytypes "github.com/moby/moby/api/types/registry"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newSearchCommand)
}
type searchOptions struct {
format string
term string
+5
View File
@@ -4,10 +4,15 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/internal/commands"
"github.com/moby/moby/api/types/swarm"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newSecretCommand)
}
// NewSecretCommand returns a cobra command for `secret` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -3,9 +3,14 @@ package service
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newServiceCommand)
}
// NewServiceCommand returns a cobra command for `service` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -7,9 +7,14 @@ import (
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/command/stack/swarm"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newStackCommand)
}
// NewStackCommand returns a cobra command for `stack` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -5,8 +5,13 @@ import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
)
func init() {
commands.Register(newSwarmCommand)
}
// NewSwarmCommand returns a cobra command for `swarm` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+9
View File
@@ -3,9 +3,18 @@ package system
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newVersionCommand)
commands.Register(newInfoCommand)
commands.Register(newSystemCommand)
commands.RegisterLegacy(newEventsCommand)
commands.RegisterLegacy(newInspectCommand)
}
// NewSystemCommand returns a cobra command for `system` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -3,9 +3,14 @@ package trust
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newTrustCommand)
}
// NewTrustCommand returns a cobra command for `trust` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.
+5
View File
@@ -3,9 +3,14 @@ package volume
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/internal/commands"
"github.com/spf13/cobra"
)
func init() {
commands.Register(newVolumeCommand)
}
// NewVolumeCommand returns a cobra command for `volume` subcommands
//
// Deprecated: Do not import commands directly. They will be removed in a future release.