This repository was archived by the owner on Oct 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathroot.go
More file actions
111 lines (98 loc) · 3.32 KB
/
root.go
File metadata and controls
111 lines (98 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package commands
import (
"fmt"
"github.com/docker/cli/cli-plugins/plugin"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
"github.com/docker/model-cli/desktop"
"github.com/spf13/cobra"
)
// 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
// options as well, which we'll add as global flags on the root command. We
// perform that initialization below so that we can register flags with the
// root command.
var globalOptions *flags.ClientOptions
// Set up the root command.
var rootCmd *cobra.Command
rootCmd = &cobra.Command{
Use: "model",
Short: "Docker Model Runner",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Finalize initialization of the CLI.
if plugin.RunningStandalone() {
globalOptions.SetDefaultOptions(rootCmd.Flags())
if err := cli.Initialize(globalOptions); err != nil {
return fmt.Errorf("unable to configure CLI: %w", err)
}
} else if err := plugin.PersistentPreRunE(cmd, args); err != nil {
return err
}
dockerCLI = cli
// Detect the model runner context and create a client for it.
var err error
modelRunner, err = desktop.DetectContext(dockerCLI)
if err != nil {
return fmt.Errorf("unable to detect model runner context: %w", err)
}
desktopClient = desktop.New(modelRunner)
return nil
},
// If running standalone, then we'll register global Docker flags as
// top-level flags on the root command, so we'll have to set
// TraverseChildren in order for those flags to be inherited. We could
// instead register them as PersistentFlags, but our approach here
// better matches the behavior of the Docker CLI, where these flags
// affect all commands, but don't show up in the help output of all
// commands a "Global Flags".
TraverseChildren: plugin.RunningStandalone(),
}
// Initialize client options and register their flags if running in
// standalone mode.
if plugin.RunningStandalone() {
globalOptions = flags.NewClientOptions()
globalOptions.InstallFlags(rootCmd.Flags())
}
// Add subcommands.
rootCmd.AddCommand(
newVersionCmd(),
newStatusCmd(),
newPullCmd(),
newPushCmd(),
newPackagedCmd(),
newListCmd(),
newLogsCmd(),
newRunCmd(),
newRemoveCmd(),
newInspectCmd(),
newComposeCmd(),
newTagCmd(),
newInstallRunner(),
newUninstallRunner(),
)
return rootCmd
}