Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions commands/completion/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ func NoComplete(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCo
}

// ModelNames offers completion for models present within the local store.
func ModelNames(desktopClient *desktop.Client, limit int) cobra.CompletionFunc {
func ModelNames(desktopClient func() *desktop.Client, limit int) cobra.CompletionFunc {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// HACK: Invoke rootCmd's PersistentPreRunE, which is needed for context
// detection and client initialization. This function isn't invoked
// automatically on autocompletion paths.
cmd.Parent().PersistentPreRunE(cmd, args)

if limit > 0 && len(args) >= limit {
return nil, cobra.ShellCompDirectiveNoFileComp
}
models, err := desktopClient.List()
models, err := desktopClient().List()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can't we just use desktopClient *desktop.Client if we're running cmd.Parent().PersistentPreRunE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose we could, but it would create a circular dependency, and passing the value through at command construction captures the nil value.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I see.

if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down
2 changes: 1 addition & 1 deletion commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func newInspectCmd() *cobra.Command {
cmd.Print(inspectedModel)
return nil
},
ValidArgsFunction: completion.ModelNames(desktopClient, 1),
ValidArgsFunction: completion.ModelNames(getDesktopClient, 1),
}
c.Flags().BoolVar(&openai, "openai", false, "List model in an OpenAI format")
return c
Expand Down
2 changes: 1 addition & 1 deletion commands/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newRemoveCmd() *cobra.Command {
}
return nil
},
ValidArgsFunction: completion.ModelNames(desktopClient, -1),
ValidArgsFunction: completion.ModelNames(getDesktopClient, -1),
}

c.Flags().BoolVarP(&force, "force", "f", false, "Forcefully remove the model")
Expand Down
18 changes: 18 additions & 0 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ import (
// dockerCLI is the Docker CLI environment associated with the command.
var dockerCLI *command.DockerCli

// getDockerCLI is an accessor for dockerCLI that can be passed to other
// packages.
func getDockerCLI() *command.DockerCli {
return dockerCLI
}

// modelRunner is the model runner context. It is initialized by the root
// command's PersistentPreRunE.
var modelRunner *desktop.ModelRunnerContext

// getModelRunner is an accessor for modelRunner that can be passed to other
// packages.
func getModelRunner() *desktop.ModelRunnerContext {
return modelRunner
}

// desktopClient is the model runner client. It is initialized by the root
// command's PersistentPreRunE.
var desktopClient *desktop.Client

// getDesktopClient is an accessor for desktopClient that can be passed to other
// packages.
func getDesktopClient() *desktop.Client {
return desktopClient
}

func NewRootCmd(cli *command.DockerCli) *cobra.Command {
// If we're running in standalone mode, then we're responsible for
// initializing the CLI. In this case, we'll need to initialize the client
Expand Down
2 changes: 1 addition & 1 deletion commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func newRunCmd() *cobra.Command {
}
return nil
},
ValidArgsFunction: completion.ModelNames(desktopClient, 1),
ValidArgsFunction: completion.ModelNames(getDesktopClient, 1),
}
c.Args = func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
Expand Down