Skip to content

Commit

Permalink
[WIP] Refactor network name completion
Browse files Browse the repository at this point in the history
Signed-off-by: Harald Albers <[email protected]>
  • Loading branch information
albers committed Oct 17, 2024
1 parent 616b7c9 commit a09c204
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 15 deletions.
23 changes: 16 additions & 7 deletions cli/command/completion/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,30 @@ func VolumeNames(dockerCLI APIClientProvider) ValidArgsFn {
}
}

// NetworkNames offers completion for networks
func NetworkNames(dockerCLI APIClientProvider) ValidArgsFn {
// CompleteNetworkNames offers completion for networks
func CompleteNetworkNames(dockerCLI APIClientProvider) ValidArgsFn {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
list, err := dockerCLI.Client().NetworkList(cmd.Context(), network.ListOptions{})
names, err := NetworkNames(dockerCLI, cmd)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
var names []string
for _, nw := range list {
names = append(names, nw.Name)
}
return names, cobra.ShellCompDirectiveNoFileComp
}
}

// NetworkNames returns a list of networks
func NetworkNames(dockerCLI APIClientProvider, cmd *cobra.Command) ([]string, error) {
summaries, err := dockerCLI.Client().NetworkList(cmd.Context(), network.ListOptions{})
if err != nil {
return nil, err
}
var names []string
for _, nws := range summaries {
names = append(names, nws.Name)
}
return names, nil
}

// EnvVarNames offers completion for environment-variable names. This
// completion can be used for "--env" and "--build-arg" flags, which
// allow obtaining the value of the given environment-variable if present
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command {
_ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames)
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames)
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
_ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli))
_ = cmd.RegisterFlagCompletionFunc("network", completion.CompleteNetworkNames(dockerCli))
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
_ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever))
_ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewRunCommand(dockerCli command.Cli) *cobra.Command {
_ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames)
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames)
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
_ = cmd.RegisterFlagCompletionFunc("network", completion.NetworkNames(dockerCli))
_ = cmd.RegisterFlagCompletionFunc("network", completion.CompleteNetworkNames(dockerCli))
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
_ = cmd.RegisterFlagCompletionFunc("pull", completion.FromList(PullImageAlways, PullImageMissing, PullImageNever))
_ = cmd.RegisterFlagCompletionFunc("restart", completeRestartPolicies)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newConnectCommand(dockerCLI command.Cli) *cobra.Command {
},
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.CompleteNetworkNames(dockerCLI)(cmd, args, toComplete)
}
nw := args[0]
return completion.ContainerNames(dockerCLI, true, not(isConnected(nw)))(cmd, args, toComplete)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func newDisconnectCommand(dockerCli command.Cli) *cobra.Command {
},
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.CompleteNetworkNames(dockerCli)(cmd, args, toComplete)
}
network := args[0]
return completion.ContainerNames(dockerCli, true, isConnected(network))(cmd, args, toComplete)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func newInspectCommand(dockerCLI command.Cli) *cobra.Command {
opts.names = args
return runInspect(cmd.Context(), dockerCLI.Client(), dockerCLI.Out(), opts)
},
ValidArgsFunction: completion.NetworkNames(dockerCLI),
ValidArgsFunction: completion.CompleteNetworkNames(dockerCLI),
}

cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
return runRemove(cmd.Context(), dockerCli, args, &opts)
},
ValidArgsFunction: completion.NetworkNames(dockerCli),
ValidArgsFunction: completion.CompleteNetworkNames(dockerCli),
}

flags := cmd.Flags()
Expand Down
6 changes: 4 additions & 2 deletions cli/command/system/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func completeFilters(dockerCLI completion.APIClientProvider) completion.ValidArg
return nil, cobra.ShellCompDirectiveNoFileComp
}
if strings.HasPrefix(toComplete, "network=") {
// the pure network name list should be pulled out from NetworkNames.
names, _ := completion.NetworkNames(dockerCLI)(cmd, args, toComplete)
names, err := completion.NetworkNames(dockerCLI, cmd)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return prefixWith("network=", names), cobra.ShellCompDirectiveDefault
}
if strings.HasPrefix(toComplete, "type=") {
Expand Down

0 comments on commit a09c204

Please sign in to comment.