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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-07-03 17:04:48 +02:00
parent 3837aa62d8
commit 2eb61318b5
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 26 additions and 29 deletions

View File

@ -25,7 +25,7 @@ type connectOptions struct {
driverOpts []string driverOpts []string
} }
func newConnectCommand(dockerCli command.Cli) *cobra.Command { func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
options := connectOptions{ options := connectOptions{
links: opts.NewListOpts(opts.ValidateLink), links: opts.NewListOpts(opts.ValidateLink),
} }
@ -37,14 +37,14 @@ func newConnectCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.network = args[0] options.network = args[0]
options.container = args[1] 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) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 { if len(args) == 0 {
return completion.NetworkNames(dockerCli)(cmd, args, toComplete) return completion.NetworkNames(dockerCLI)(cmd, args, toComplete)
} }
nw := args[0] 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)
}, },
} }

View File

@ -3,6 +3,7 @@ package network
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"net" "net"
"strings" "strings"
@ -11,6 +12,7 @@ import (
"github.com/docker/cli/cli/command/completion" "github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -36,7 +38,7 @@ type createOptions struct {
ipamOpt opts.MapOpts ipamOpt opts.MapOpts
} }
func newCreateCommand(dockerCli command.Cli) *cobra.Command { func newCreateCommand(dockerCLI command.Cli) *cobra.Command {
var ipv6 bool var ipv6 bool
options := createOptions{ options := createOptions{
driverOpts: *opts.NewMapOpts(nil, nil), driverOpts: *opts.NewMapOpts(nil, nil),
@ -56,7 +58,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
options.ipv6 = &ipv6 options.ipv6 = &ipv6
} }
return runCreate(cmd.Context(), dockerCli, options) return runCreate(cmd.Context(), dockerCLI.Client(), dockerCLI.Out(), options)
}, },
ValidArgsFunction: completion.NoComplete, ValidArgsFunction: completion.NoComplete,
} }
@ -89,9 +91,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runCreate(ctx context.Context, dockerCli command.Cli, options createOptions) error { func runCreate(ctx context.Context, apiClient client.NetworkAPIClient, output io.Writer, options createOptions) error {
client := dockerCli.Client()
ipamCfg, err := consolidateIpam(options.ipamSubnet, options.ipamIPRange, options.ipamGateway, options.ipamAux.GetAll()) ipamCfg, err := consolidateIpam(options.ipamSubnet, options.ipamIPRange, options.ipamGateway, options.ipamAux.GetAll())
if err != nil { if err != nil {
return err return err
@ -103,7 +103,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, options createOptions
Network: options.configFrom, Network: options.configFrom,
} }
} }
resp, err := client.NetworkCreate(ctx, options.name, network.CreateOptions{ resp, err := apiClient.NetworkCreate(ctx, options.name, network.CreateOptions{
Driver: options.driver, Driver: options.driver,
Options: options.driverOpts.GetAll(), Options: options.driverOpts.GetAll(),
IPAM: &network.IPAM{ IPAM: &network.IPAM{
@ -123,7 +123,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, options createOptions
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(dockerCli.Out(), "%s\n", resp.ID) _, _ = fmt.Fprintf(output, "%s\n", resp.ID)
return nil return nil
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion" "github.com/docker/cli/cli/command/completion"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -26,7 +27,7 @@ func newDisconnectCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.network = args[0] opts.network = args[0]
opts.container = args[1] 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) { ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 { if len(args) == 0 {
@ -43,10 +44,8 @@ func newDisconnectCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runDisconnect(ctx context.Context, dockerCli command.Cli, opts disconnectOptions) error { func runDisconnect(ctx context.Context, apiClient client.NetworkAPIClient, opts disconnectOptions) error {
client := dockerCli.Client() return apiClient.NetworkDisconnect(ctx, opts.network, opts.container, opts.force)
return client.NetworkDisconnect(ctx, opts.network, opts.container, opts.force)
} }
func isConnected(network string) func(types.Container) bool { func isConnected(network string) func(types.Container) bool {

View File

@ -5,6 +5,7 @@ package network
import ( import (
"context" "context"
"io"
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
@ -12,6 +13,7 @@ import (
"github.com/docker/cli/cli/command/inspect" "github.com/docker/cli/cli/command/inspect"
flagsHelper "github.com/docker/cli/cli/flags" flagsHelper "github.com/docker/cli/cli/flags"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -21,7 +23,7 @@ type inspectOptions struct {
verbose bool verbose bool
} }
func newInspectCommand(dockerCli command.Cli) *cobra.Command { func newInspectCommand(dockerCLI command.Cli) *cobra.Command {
var opts inspectOptions var opts inspectOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -30,9 +32,9 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.names = args 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) cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
@ -41,12 +43,8 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error { func runInspect(ctx context.Context, apiClient client.NetworkAPIClient, output io.Writer, opts inspectOptions) error {
client := dockerCli.Client() return inspect.Inspect(output, opts.names, opts.format, func(name string) (any, []byte, error) {
return apiClient.NetworkInspectWithRaw(ctx, name, network.InspectOptions{Verbose: opts.verbose})
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)
} }

View File

@ -60,11 +60,11 @@ func runRemove(ctx context.Context, dockerCli command.Cli, networks []string, op
if opts.force && errdefs.IsNotFound(err) { if opts.force && errdefs.IsNotFound(err) {
continue continue
} }
fmt.Fprintf(dockerCli.Err(), "%s\n", err) _, _ = fmt.Fprintf(dockerCli.Err(), "%s\n", err)
status = 1 status = 1
continue continue
} }
fmt.Fprintf(dockerCli.Out(), "%s\n", name) _, _ = fmt.Fprintf(dockerCli.Out(), "%s\n", name)
} }
if status != 0 { if status != 0 {