Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cli/command/container/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,24 @@ func completeDetachKeys(_ *cobra.Command, _ []string, _ string) ([]string, cobra
// to provide completion for CDI devices that were discovered by the daemon.
func completeCDIDevices(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if strings.HasPrefix(toComplete, "/") {
return nil, cobra.ShellCompDirectiveDefault | cobra.ShellCompDirectiveNoSpace
}

info, err := dockerCLI.Client().Info(cmd.Context())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening as draft, because I had this branch but after implementing CDI completion, I realised we also need to provide completion for file-paths (assuming the daemon and CLI are on the same host), so we need some solid way to distinguish "user wants to complete a CDI device" vs "user wants to add a device (e.g. /dev/foo:/some/path).

if err != nil {
return nil, cobra.ShellCompDirectiveError
return nil, cobra.ShellCompDirectiveDefault
}
var devices []string
// DiscoveredDevices requires Docker v28.2.0 (API 1.50) or above,
// but we just check if it's returned.
for _, di := range info.DiscoveredDevices {
if di.Source == "cdi" {
devices = append(devices, di.ID)
devices = append(devices, cobra.CompletionWithDesc(di.ID, "CDI device"))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still on the fence if cobra.CompletionWithDesc is a bit "too much"; it's the equivalent of <value> + \t + <description>. I guess it's slightly more expressive, but also makes it look more magical than it is 😂

}
}
return devices, cobra.ShellCompDirectiveNoFileComp
devices = append(devices, "/")
return devices, cobra.ShellCompDirectiveDefault
}
}

Expand Down
Loading