Skip to content

Commit

Permalink
cli/command/network: some cleanup and pass smaller interfaces
Browse files Browse the repository at this point in the history
Pass the appropriate API-client where possible instead of all of
DockerCLI, and some cleaning up.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jul 3, 2024
1 parent 3837aa6 commit 2eb6131
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
8 changes: 4 additions & 4 deletions cli/command/network/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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)
},
}

Expand Down
14 changes: 7 additions & 7 deletions cli/command/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package network
import (
"context"
"fmt"
"io"
"net"
"strings"

Expand All @@ -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"
)
Expand All @@ -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),
Expand All @@ -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,
}
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand All @@ -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
}

Expand Down
9 changes: 4 additions & 5 deletions cli/command/network/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
20 changes: 9 additions & 11 deletions cli/command/network/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package network

import (
"context"
"io"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"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"
)

Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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})
})
}
4 changes: 2 additions & 2 deletions cli/command/network/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2eb6131

Please sign in to comment.