Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit 083bf0f

Browse files
authored
Merge pull request #58 from docker/fix-completion
cli: fix autocompletion to work with context detection
2 parents bb895f3 + 5923dff commit 083bf0f

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

commands/completion/functions.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ func NoComplete(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCo
1010
}
1111

1212
// ModelNames offers completion for models present within the local store.
13-
func ModelNames(desktopClient *desktop.Client, limit int) cobra.CompletionFunc {
13+
func ModelNames(desktopClient func() *desktop.Client, limit int) cobra.CompletionFunc {
1414
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
15+
// HACK: Invoke rootCmd's PersistentPreRunE, which is needed for context
16+
// detection and client initialization. This function isn't invoked
17+
// automatically on autocompletion paths.
18+
cmd.Parent().PersistentPreRunE(cmd, args)
19+
1520
if limit > 0 && len(args) >= limit {
1621
return nil, cobra.ShellCompDirectiveNoFileComp
1722
}
18-
models, err := desktopClient.List()
23+
models, err := desktopClient().List()
1924
if err != nil {
2025
return nil, cobra.ShellCompDirectiveError
2126
}

commands/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func newInspectCmd() *cobra.Command {
3434
cmd.Print(inspectedModel)
3535
return nil
3636
},
37-
ValidArgsFunction: completion.ModelNames(desktopClient, 1),
37+
ValidArgsFunction: completion.ModelNames(getDesktopClient, 1),
3838
}
3939
c.Flags().BoolVar(&openai, "openai", false, "List model in an OpenAI format")
4040
return c

commands/rm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func newRemoveCmd() *cobra.Command {
3737
}
3838
return nil
3939
},
40-
ValidArgsFunction: completion.ModelNames(desktopClient, -1),
40+
ValidArgsFunction: completion.ModelNames(getDesktopClient, -1),
4141
}
4242

4343
c.Flags().BoolVarP(&force, "force", "f", false, "Forcefully remove the model")

commands/root.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,32 @@ import (
1313
// dockerCLI is the Docker CLI environment associated with the command.
1414
var dockerCLI *command.DockerCli
1515

16+
// getDockerCLI is an accessor for dockerCLI that can be passed to other
17+
// packages.
18+
func getDockerCLI() *command.DockerCli {
19+
return dockerCLI
20+
}
21+
1622
// modelRunner is the model runner context. It is initialized by the root
1723
// command's PersistentPreRunE.
1824
var modelRunner *desktop.ModelRunnerContext
1925

26+
// getModelRunner is an accessor for modelRunner that can be passed to other
27+
// packages.
28+
func getModelRunner() *desktop.ModelRunnerContext {
29+
return modelRunner
30+
}
31+
2032
// desktopClient is the model runner client. It is initialized by the root
2133
// command's PersistentPreRunE.
2234
var desktopClient *desktop.Client
2335

36+
// getDesktopClient is an accessor for desktopClient that can be passed to other
37+
// packages.
38+
func getDesktopClient() *desktop.Client {
39+
return desktopClient
40+
}
41+
2442
func NewRootCmd(cli *command.DockerCli) *cobra.Command {
2543
// If we're running in standalone mode, then we're responsible for
2644
// initializing the CLI. In this case, we'll need to initialize the client

commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func newRunCmd() *cobra.Command {
8989
}
9090
return nil
9191
},
92-
ValidArgsFunction: completion.ModelNames(desktopClient, 1),
92+
ValidArgsFunction: completion.ModelNames(getDesktopClient, 1),
9393
}
9494
c.Args = func(cmd *cobra.Command, args []string) error {
9595
if len(args) < 1 {

0 commit comments

Comments
 (0)