cli/command/network: fix linting issue and pass client instead of cli

Rename variable that shadowed an import, and pass the network API-client
instead of the whole CLI

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit e93abde7a0)
Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Sebastiaan van Stijn 2024-06-10 20:22:15 +02:00 committed by Cory Snider
parent 1d72a32b64
commit 5adf6be6d8
1 changed files with 8 additions and 10 deletions

View File

@ -10,6 +10,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/spf13/cobra" "github.com/spf13/cobra"
) )
@ -36,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(dockerCli, 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)
} }
network := args[0] nw := args[0]
return completion.ContainerNames(dockerCli, true, not(isConnected(network)))(cmd, args, toComplete) return completion.ContainerNames(dockerCli, true, not(isConnected(nw)))(cmd, args, toComplete)
}, },
} }
@ -57,14 +58,13 @@ func newConnectCommand(dockerCli command.Cli) *cobra.Command {
return cmd return cmd
} }
func runConnect(dockerCli command.Cli, options connectOptions) error { func runConnect(ctx context.Context, apiClient client.NetworkAPIClient, options connectOptions) error {
client := dockerCli.Client()
driverOpts, err := convertDriverOpt(options.driverOpts) driverOpts, err := convertDriverOpt(options.driverOpts)
if err != nil { if err != nil {
return err return err
} }
epConfig := &network.EndpointSettings{
return apiClient.NetworkConnect(ctx, options.network, options.container, &network.EndpointSettings{
IPAMConfig: &network.EndpointIPAMConfig{ IPAMConfig: &network.EndpointIPAMConfig{
IPv4Address: options.ipaddress, IPv4Address: options.ipaddress,
IPv6Address: options.ipv6address, IPv6Address: options.ipv6address,
@ -73,9 +73,7 @@ func runConnect(dockerCli command.Cli, options connectOptions) error {
Links: options.links.GetAll(), Links: options.links.GetAll(),
Aliases: options.aliases, Aliases: options.aliases,
DriverOpts: driverOpts, DriverOpts: driverOpts,
} })
return client.NetworkConnect(context.Background(), options.network, options.container, epConfig)
} }
func convertDriverOpt(opts []string) (map[string]string, error) { func convertDriverOpt(opts []string) (map[string]string, error) {