Skip to content
Draft
Changes from all commits
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
26 changes: 26 additions & 0 deletions cli/command/container/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func addCompletions(cmd *cobra.Command, dockerCLI completion.APIClientProvider)
_ = cmd.RegisterFlagCompletionFunc("cap-add", completeLinuxCapabilityNames)
_ = cmd.RegisterFlagCompletionFunc("cap-drop", completeLinuxCapabilityNames)
_ = cmd.RegisterFlagCompletionFunc("cgroupns", completeCgroupns())
_ = cmd.RegisterFlagCompletionFunc("device", completeCDIDevices(dockerCLI))
_ = cmd.RegisterFlagCompletionFunc("env", completion.EnvVarNames)
_ = cmd.RegisterFlagCompletionFunc("env-file", completion.FileNames)
_ = cmd.RegisterFlagCompletionFunc("ipc", completeIpc(dockerCLI))
Expand Down Expand Up @@ -153,6 +154,31 @@ func completeDetachKeys(_ *cobra.Command, _ []string, _ string) ([]string, cobra
return []string{"ctrl-"}, cobra.ShellCompDirectiveNoSpace
}

// completeCDIDevices implements shell completion for the '--device' flag
// 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())
Comment on lines 159 to 165
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.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, cobra.CompletionWithDesc(di.ID, "CDI device"))
}
}
devices = append(devices, "/")
return devices, cobra.ShellCompDirectiveDefault
}
}

// completeIpc implements shell completion for the `--ipc` option of `run` and `create`.
// The completion is partly composite.
func completeIpc(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
Expand Down
Loading