Skip to content

Commit 56cab16

Browse files
committed
Register CLI commands implicitly
This patch removes the explicit `commands.AddCommands` function and instead relies upon the `internal/commands` package which registers each CLI command using `init()` instead. Signed-off-by: Alano Terblanche <[email protected]>
1 parent 4d93a64 commit 56cab16

File tree

21 files changed

+162
-159
lines changed

21 files changed

+162
-159
lines changed

cli/command/builder/cmd.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ import (
66
"github.com/docker/cli/cli"
77
"github.com/docker/cli/cli/command"
88
"github.com/docker/cli/cli/command/image"
9+
"github.com/docker/cli/internal/commands"
910
)
1011

12+
func init() {
13+
commands.Register(newBuilderCommand)
14+
commands.Register(func(c command.Cli) *cobra.Command {
15+
return newBakeStubCommand(c)
16+
})
17+
}
18+
1119
// NewBuilderCommand returns a cobra command for `builder` subcommands
1220
//
1321
// Deprecated: Do not import commands directly. They will be removed in a future release.

cli/command/checkpoint/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ package checkpoint
33
import (
44
"github.com/docker/cli/cli"
55
"github.com/docker/cli/cli/command"
6+
"github.com/docker/cli/internal/commands"
67
"github.com/spf13/cobra"
78
)
89

10+
func init() {
11+
commands.Register(newCheckpointCommand)
12+
}
13+
914
// NewCheckpointCommand returns the `checkpoint` subcommand (only in experimental)
1015
//
1116
// Deprecated: Do not import commands directly. They will be removed in a future release.

cli/command/commands/commands.go

Lines changed: 22 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,31 @@
11
package commands
22

33
import (
4-
"os"
5-
64
"github.com/docker/cli/cli/command"
7-
"github.com/docker/cli/cli/command/builder"
8-
"github.com/docker/cli/cli/command/checkpoint"
9-
"github.com/docker/cli/cli/command/config"
10-
"github.com/docker/cli/cli/command/container"
11-
"github.com/docker/cli/cli/command/context"
12-
"github.com/docker/cli/cli/command/image"
13-
"github.com/docker/cli/cli/command/manifest"
14-
"github.com/docker/cli/cli/command/network"
15-
"github.com/docker/cli/cli/command/node"
16-
"github.com/docker/cli/cli/command/plugin"
17-
"github.com/docker/cli/cli/command/registry"
18-
"github.com/docker/cli/cli/command/secret"
19-
"github.com/docker/cli/cli/command/service"
20-
"github.com/docker/cli/cli/command/stack"
21-
"github.com/docker/cli/cli/command/swarm"
22-
"github.com/docker/cli/cli/command/system"
23-
"github.com/docker/cli/cli/command/trust"
24-
"github.com/docker/cli/cli/command/volume"
5+
_ "github.com/docker/cli/cli/command/builder"
6+
_ "github.com/docker/cli/cli/command/checkpoint"
7+
_ "github.com/docker/cli/cli/command/config"
8+
_ "github.com/docker/cli/cli/command/container"
9+
_ "github.com/docker/cli/cli/command/context"
10+
_ "github.com/docker/cli/cli/command/image"
11+
_ "github.com/docker/cli/cli/command/manifest"
12+
_ "github.com/docker/cli/cli/command/network"
13+
_ "github.com/docker/cli/cli/command/node"
14+
_ "github.com/docker/cli/cli/command/plugin"
15+
_ "github.com/docker/cli/cli/command/registry"
16+
_ "github.com/docker/cli/cli/command/secret"
17+
_ "github.com/docker/cli/cli/command/service"
18+
_ "github.com/docker/cli/cli/command/stack"
19+
_ "github.com/docker/cli/cli/command/swarm"
20+
_ "github.com/docker/cli/cli/command/system"
21+
_ "github.com/docker/cli/cli/command/trust"
22+
_ "github.com/docker/cli/cli/command/volume"
23+
"github.com/docker/cli/internal/commands"
2524
"github.com/spf13/cobra"
2625
)
2726

28-
// AddCommands adds all the commands from cli/command to the root command
29-
func AddCommands(cmd *cobra.Command, dockerCli command.Cli) {
30-
cmd.AddCommand(
31-
// commonly used shorthands
32-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
33-
container.NewRunCommand(dockerCli),
34-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
35-
container.NewExecCommand(dockerCli),
36-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
37-
container.NewPsCommand(dockerCli),
38-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
39-
image.NewBuildCommand(dockerCli),
40-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
41-
image.NewPullCommand(dockerCli),
42-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
43-
image.NewPushCommand(dockerCli),
44-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
45-
image.NewImagesCommand(dockerCli),
46-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
47-
registry.NewLoginCommand(dockerCli),
48-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
49-
registry.NewLogoutCommand(dockerCli),
50-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
51-
registry.NewSearchCommand(dockerCli),
52-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
53-
system.NewVersionCommand(dockerCli),
54-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
55-
system.NewInfoCommand(dockerCli),
56-
57-
// management commands
58-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
59-
builder.NewBakeStubCommand(dockerCli),
60-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
61-
builder.NewBuilderCommand(dockerCli),
62-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
63-
checkpoint.NewCheckpointCommand(dockerCli),
64-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
65-
container.NewContainerCommand(dockerCli),
66-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
67-
context.NewContextCommand(dockerCli),
68-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
69-
image.NewImageCommand(dockerCli),
70-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
71-
manifest.NewManifestCommand(dockerCli),
72-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
73-
network.NewNetworkCommand(dockerCli),
74-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
75-
plugin.NewPluginCommand(dockerCli),
76-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
77-
system.NewSystemCommand(dockerCli),
78-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
79-
trust.NewTrustCommand(dockerCli),
80-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
81-
volume.NewVolumeCommand(dockerCli),
82-
83-
// orchestration (swarm) commands
84-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
85-
config.NewConfigCommand(dockerCli),
86-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
87-
node.NewNodeCommand(dockerCli),
88-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
89-
secret.NewSecretCommand(dockerCli),
90-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
91-
service.NewServiceCommand(dockerCli),
92-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
93-
stack.NewStackCommand(dockerCli),
94-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
95-
swarm.NewSwarmCommand(dockerCli),
96-
97-
// legacy commands may be hidden
98-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
99-
hide(container.NewAttachCommand(dockerCli)),
100-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
101-
hide(container.NewCommitCommand(dockerCli)),
102-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
103-
hide(container.NewCopyCommand(dockerCli)),
104-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
105-
hide(container.NewCreateCommand(dockerCli)),
106-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
107-
hide(container.NewDiffCommand(dockerCli)),
108-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
109-
hide(container.NewExportCommand(dockerCli)),
110-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
111-
hide(container.NewKillCommand(dockerCli)),
112-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
113-
hide(container.NewLogsCommand(dockerCli)),
114-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
115-
hide(container.NewPauseCommand(dockerCli)),
116-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
117-
hide(container.NewPortCommand(dockerCli)),
118-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
119-
hide(container.NewRenameCommand(dockerCli)),
120-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
121-
hide(container.NewRestartCommand(dockerCli)),
122-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
123-
hide(container.NewRmCommand(dockerCli)),
124-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
125-
hide(container.NewStartCommand(dockerCli)),
126-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
127-
hide(container.NewStatsCommand(dockerCli)),
128-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
129-
hide(container.NewStopCommand(dockerCli)),
130-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
131-
hide(container.NewTopCommand(dockerCli)),
132-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
133-
hide(container.NewUnpauseCommand(dockerCli)),
134-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
135-
hide(container.NewUpdateCommand(dockerCli)),
136-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
137-
hide(container.NewWaitCommand(dockerCli)),
138-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
139-
hide(image.NewHistoryCommand(dockerCli)),
140-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
141-
hide(image.NewImportCommand(dockerCli)),
142-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
143-
hide(image.NewLoadCommand(dockerCli)),
144-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
145-
hide(image.NewRemoveCommand(dockerCli)),
146-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
147-
hide(image.NewSaveCommand(dockerCli)),
148-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
149-
hide(image.NewTagCommand(dockerCli)),
150-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
151-
hide(system.NewEventsCommand(dockerCli)),
152-
//nolint:staticcheck // TODO: Remove when migration to cli/internal/commands.Register is complete. (see #6283)
153-
hide(system.NewInspectCommand(dockerCli)),
154-
)
155-
}
156-
157-
func hide(cmd *cobra.Command) *cobra.Command {
158-
// If the environment variable with name "DOCKER_HIDE_LEGACY_COMMANDS" is not empty,
159-
// these legacy commands (such as `docker ps`, `docker exec`, etc)
160-
// will not be shown in output console.
161-
if os.Getenv("DOCKER_HIDE_LEGACY_COMMANDS") == "" {
162-
return cmd
27+
func AddCommands(cmd *cobra.Command, dockerCLI command.Cli) {
28+
for _, c := range commands.Commands() {
29+
cmd.AddCommand(c(dockerCLI))
16330
}
164-
cmdCopy := *cmd
165-
cmdCopy.Hidden = true
166-
cmdCopy.Aliases = []string{}
167-
return &cmdCopy
16831
}

cli/command/config/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import (
44
"github.com/docker/cli/cli"
55
"github.com/docker/cli/cli/command"
66
"github.com/docker/cli/cli/command/completion"
7+
"github.com/docker/cli/internal/commands"
78
"github.com/moby/moby/api/types/swarm"
89
"github.com/spf13/cobra"
910
)
1011

12+
func init() {
13+
commands.Register(newConfigCommand)
14+
}
15+
1116
// NewConfigCommand returns a cobra command for `config` subcommands
1217
//
1318
// Deprecated: Do not import commands directly. They will be removed in a future release.

cli/command/container/cmd.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,37 @@ package container
33
import (
44
"github.com/docker/cli/cli"
55
"github.com/docker/cli/cli/command"
6+
"github.com/docker/cli/internal/commands"
67
"github.com/spf13/cobra"
78
)
89

10+
func init() {
11+
commands.Register(newRunCommand)
12+
commands.Register(newExecCommand)
13+
commands.Register(newPsCommand)
14+
commands.Register(newContainerCommand)
15+
commands.RegisterLegacy(newAttachCommand)
16+
commands.RegisterLegacy(newCommitCommand)
17+
commands.RegisterLegacy(newCopyCommand)
18+
commands.RegisterLegacy(newCreateCommand)
19+
commands.RegisterLegacy(newDiffCommand)
20+
commands.RegisterLegacy(newExportCommand)
21+
commands.RegisterLegacy(newKillCommand)
22+
commands.RegisterLegacy(newLogsCommand)
23+
commands.RegisterLegacy(newPauseCommand)
24+
commands.RegisterLegacy(newPortCommand)
25+
commands.RegisterLegacy(newRenameCommand)
26+
commands.RegisterLegacy(newRestartCommand)
27+
commands.RegisterLegacy(newRmCommand)
28+
commands.RegisterLegacy(newStartCommand)
29+
commands.RegisterLegacy(newStatsCommand)
30+
commands.RegisterLegacy(newStopCommand)
31+
commands.RegisterLegacy(newTopCommand)
32+
commands.RegisterLegacy(newUnpauseCommand)
33+
commands.RegisterLegacy(newUpdateCommand)
34+
commands.RegisterLegacy(newWaitCommand)
35+
}
36+
937
// NewContainerCommand returns a cobra command for `container` subcommands
1038
//
1139
// Deprecated: Do not import commands directly. They will be removed in a future release.

cli/command/context/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ package context
33
import (
44
"github.com/docker/cli/cli"
55
"github.com/docker/cli/cli/command"
6+
"github.com/docker/cli/internal/commands"
67
"github.com/spf13/cobra"
78
)
89

10+
func init() {
11+
commands.Register(newContextCommand)
12+
}
13+
914
// NewContextCommand returns the context cli subcommand
1015
//
1116
// Deprecated: Do not import commands directly. They will be removed in a future release.

cli/command/image/cmd.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,24 @@ package image
33
import (
44
"github.com/docker/cli/cli"
55
"github.com/docker/cli/cli/command"
6+
"github.com/docker/cli/internal/commands"
67
"github.com/spf13/cobra"
78
)
89

10+
func init() {
11+
commands.Register(newBuildCommand)
12+
commands.Register(newPullCommand)
13+
commands.Register(newPushCommand)
14+
commands.Register(newImagesCommand)
15+
commands.Register(newImageCommand)
16+
commands.RegisterLegacy(newHistoryCommand)
17+
commands.RegisterLegacy(newImportCommand)
18+
commands.RegisterLegacy(newLoadCommand)
19+
commands.RegisterLegacy(newRemoveCommand)
20+
commands.RegisterLegacy(newSaveCommand)
21+
commands.RegisterLegacy(newTagCommand)
22+
}
23+
924
// NewImageCommand returns a cobra command for `image` subcommands
1025
//
1126
// Deprecated: Do not import commands directly. They will be removed in a future release.

cli/command/manifest/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import (
55

66
"github.com/docker/cli/cli"
77
"github.com/docker/cli/cli/command"
8+
"github.com/docker/cli/internal/commands"
89

910
"github.com/spf13/cobra"
1011
)
1112

13+
func init() {
14+
commands.Register(newManifestCommand)
15+
}
16+
1217
// NewManifestCommand returns a cobra command for `manifest` subcommands
1318
//
1419
// Deprecated: Do not import commands directly. They will be removed in a future release.

cli/command/network/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ package network
33
import (
44
"github.com/docker/cli/cli"
55
"github.com/docker/cli/cli/command"
6+
"github.com/docker/cli/internal/commands"
67
"github.com/spf13/cobra"
78
)
89

10+
func init() {
11+
commands.Register(newNetworkCommand)
12+
}
13+
914
// NewNetworkCommand returns a cobra command for `network` subcommands
1015
//
1116
// Deprecated: Do not import commands directly. They will be removed in a future release.

cli/command/node/cmd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import (
66

77
"github.com/docker/cli/cli"
88
"github.com/docker/cli/cli/command"
9+
"github.com/docker/cli/internal/commands"
910
"github.com/moby/moby/api/types/swarm"
1011
"github.com/moby/moby/client"
1112
"github.com/spf13/cobra"
1213
)
1314

15+
func init() {
16+
commands.Register(newNodeCommand)
17+
}
18+
1419
// NewNodeCommand returns a cobra command for `node` subcommands
1520
//
1621
// Deprecated: Do not import commands directly. They will be removed in a future release.

0 commit comments

Comments
 (0)