From 2eb61318b5a57a93b99b2380c36cc82717401754 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 Jul 2024 17:04:48 +0200 Subject: [PATCH] cli/command/network: some cleanup and pass smaller interfaces Pass the appropriate API-client where possible instead of all of DockerCLI, and some cleaning up. Signed-off-by: Sebastiaan van Stijn --- cli/command/network/connect.go | 8 ++++---- cli/command/network/create.go | 14 +++++++------- cli/command/network/disconnect.go | 9 ++++----- cli/command/network/inspect.go | 20 +++++++++----------- cli/command/network/remove.go | 4 ++-- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/cli/command/network/connect.go b/cli/command/network/connect.go index 22a685ef1d..0d9e871259 100644 --- a/cli/command/network/connect.go +++ b/cli/command/network/connect.go @@ -25,7 +25,7 @@ type connectOptions struct { driverOpts []string } -func newConnectCommand(dockerCli command.Cli) *cobra.Command { +func newConnectCommand(dockerCLI command.Cli) *cobra.Command { options := connectOptions{ links: opts.NewListOpts(opts.ValidateLink), } @@ -37,14 +37,14 @@ func newConnectCommand(dockerCli command.Cli) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { options.network = args[0] options.container = args[1] - return runConnect(cmd.Context(), dockerCli.Client(), options) + return runConnect(cmd.Context(), dockerCLI.Client(), options) }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return completion.NetworkNames(dockerCli)(cmd, args, toComplete) + return completion.NetworkNames(dockerCLI)(cmd, args, toComplete) } nw := args[0] - return completion.ContainerNames(dockerCli, true, not(isConnected(nw)))(cmd, args, toComplete) + return completion.ContainerNames(dockerCLI, true, not(isConnected(nw)))(cmd, args, toComplete) }, } diff --git a/cli/command/network/create.go b/cli/command/network/create.go index 45aead0229..253781db97 100644 --- a/cli/command/network/create.go +++ b/cli/command/network/create.go @@ -3,6 +3,7 @@ package network import ( "context" "fmt" + "io" "net" "strings" @@ -11,6 +12,7 @@ import ( "github.com/docker/cli/cli/command/completion" "github.com/docker/cli/opts" "github.com/docker/docker/api/types/network" + "github.com/docker/docker/client" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -36,7 +38,7 @@ type createOptions struct { ipamOpt opts.MapOpts } -func newCreateCommand(dockerCli command.Cli) *cobra.Command { +func newCreateCommand(dockerCLI command.Cli) *cobra.Command { var ipv6 bool options := createOptions{ driverOpts: *opts.NewMapOpts(nil, nil), @@ -56,7 +58,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command { options.ipv6 = &ipv6 } - return runCreate(cmd.Context(), dockerCli, options) + return runCreate(cmd.Context(), dockerCLI.Client(), dockerCLI.Out(), options) }, ValidArgsFunction: completion.NoComplete, } @@ -89,9 +91,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runCreate(ctx context.Context, dockerCli command.Cli, options createOptions) error { - client := dockerCli.Client() - +func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io.Writer, options createOptions) error { ipamCfg, err := consolidateIpam(options.ipamSubnet, options.ipamIPRange, options.ipamGateway, options.ipamAux.GetAll()) if err != nil { return err @@ -103,7 +103,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, options createOptions Network: options.configFrom, } } - resp, err := client.NetworkCreate(ctx, options.name, network.CreateOptions{ + resp, err := apiClient.NetworkCreate(ctx, options.name, network.CreateOptions{ Driver: options.driver, Options: options.driverOpts.GetAll(), IPAM: &network.IPAM{ @@ -123,7 +123,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, options createOptions if err != nil { return err } - fmt.Fprintf(dockerCli.Out(), "%s\n", resp.ID) + _, _ = fmt.Fprintf(output, "%s\n", resp.ID) return nil } diff --git a/cli/command/network/disconnect.go b/cli/command/network/disconnect.go index 318a2d2a2b..80c7745373 100644 --- a/cli/command/network/disconnect.go +++ b/cli/command/network/disconnect.go @@ -7,6 +7,7 @@ import ( "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/docker/docker/api/types" + "github.com/docker/docker/client" "github.com/spf13/cobra" ) @@ -26,7 +27,7 @@ func newDisconnectCommand(dockerCli command.Cli) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { opts.network = args[0] opts.container = args[1] - return runDisconnect(cmd.Context(), dockerCli, opts) + return runDisconnect(cmd.Context(), dockerCli.Client(), opts) }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { @@ -43,10 +44,8 @@ func newDisconnectCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runDisconnect(ctx context.Context, dockerCli command.Cli, opts disconnectOptions) error { - client := dockerCli.Client() - - return client.NetworkDisconnect(ctx, opts.network, opts.container, opts.force) +func runDisconnect(ctx context.Context, apiClient client.NetworkAPIClient, opts disconnectOptions) error { + return apiClient.NetworkDisconnect(ctx, opts.network, opts.container, opts.force) } func isConnected(network string) func(types.Container) bool { diff --git a/cli/command/network/inspect.go b/cli/command/network/inspect.go index 200134befc..72f66382fa 100644 --- a/cli/command/network/inspect.go +++ b/cli/command/network/inspect.go @@ -5,6 +5,7 @@ package network import ( "context" + "io" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -12,6 +13,7 @@ import ( "github.com/docker/cli/cli/command/inspect" flagsHelper "github.com/docker/cli/cli/flags" "github.com/docker/docker/api/types/network" + "github.com/docker/docker/client" "github.com/spf13/cobra" ) @@ -21,7 +23,7 @@ type inspectOptions struct { verbose bool } -func newInspectCommand(dockerCli command.Cli) *cobra.Command { +func newInspectCommand(dockerCLI command.Cli) *cobra.Command { var opts inspectOptions cmd := &cobra.Command{ @@ -30,9 +32,9 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { opts.names = args - return runInspect(cmd.Context(), dockerCli, opts) + return runInspect(cmd.Context(), dockerCLI.Client(), dockerCLI.Out(), opts) }, - ValidArgsFunction: completion.NetworkNames(dockerCli), + ValidArgsFunction: completion.NetworkNames(dockerCLI), } cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp) @@ -41,12 +43,8 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command { return cmd } -func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error { - client := dockerCli.Client() - - getNetFunc := func(name string) (any, []byte, error) { - return client.NetworkInspectWithRaw(ctx, name, network.InspectOptions{Verbose: opts.verbose}) - } - - return inspect.Inspect(dockerCli.Out(), opts.names, opts.format, getNetFunc) +func runInspect(ctx context.Context, apiClient client.NetworkAPIClient, output io.Writer, opts inspectOptions) error { + return inspect.Inspect(output, opts.names, opts.format, func(name string) (any, []byte, error) { + return apiClient.NetworkInspectWithRaw(ctx, name, network.InspectOptions{Verbose: opts.verbose}) + }) } diff --git a/cli/command/network/remove.go b/cli/command/network/remove.go index f572fc8c1f..105e629f61 100644 --- a/cli/command/network/remove.go +++ b/cli/command/network/remove.go @@ -60,11 +60,11 @@ func runRemove(ctx context.Context, dockerCli command.Cli, networks []string, op if opts.force && errdefs.IsNotFound(err) { continue } - fmt.Fprintf(dockerCli.Err(), "%s\n", err) + _, _ = fmt.Fprintf(dockerCli.Err(), "%s\n", err) status = 1 continue } - fmt.Fprintf(dockerCli.Out(), "%s\n", name) + _, _ = fmt.Fprintf(dockerCli.Out(), "%s\n", name) } if status != 0 {