Skip to content
This repository was archived by the owner on Jun 16, 2026. It is now read-only.

Commit 6b5c20d

Browse files
authored
Merge pull request #104 from ndeloof/endpoint_json
introduce status --json and document endpoint URL
2 parents f110a09 + a64a773 commit 6b5c20d

3 files changed

Lines changed: 84 additions & 16 deletions

File tree

commands/status.go

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,95 @@ import (
77

88
"github.com/docker/cli/cli-plugins/hooks"
99
"github.com/docker/model-cli/commands/completion"
10+
"github.com/docker/model-cli/desktop"
1011
"github.com/spf13/cobra"
1112
)
1213

1314
func newStatusCmd() *cobra.Command {
15+
var formatJson bool
1416
c := &cobra.Command{
1517
Use: "status",
1618
Short: "Check if the Docker Model Runner is running",
1719
RunE: func(cmd *cobra.Command, args []string) error {
18-
if _, err := ensureStandaloneRunnerAvailable(cmd.Context(), cmd); err != nil {
20+
standalone, err := ensureStandaloneRunnerAvailable(cmd.Context(), cmd)
21+
if err != nil {
1922
return fmt.Errorf("unable to initialize standalone model runner: %w", err)
2023
}
2124
status := desktopClient.Status()
2225
if status.Error != nil {
2326
return handleClientError(status.Error, "Failed to get Docker Model Runner status")
2427
}
25-
if status.Running {
26-
cmd.Println("Docker Model Runner is running")
27-
cmd.Println("\nStatus:")
28-
var backendStatus map[string]string
29-
if err := json.Unmarshal(status.Status, &backendStatus); err != nil {
30-
cmd.PrintErrln(string(status.Status))
31-
}
32-
for b, s := range backendStatus {
33-
if s != "not running" {
34-
cmd.Println(b+":", s)
35-
}
36-
}
28+
29+
if len(status.Status) == 0 {
30+
status.Status = []byte("{}")
31+
}
32+
33+
var backendStatus map[string]string
34+
if err := json.Unmarshal(status.Status, &backendStatus); err != nil {
35+
cmd.PrintErrln(fmt.Errorf("failed to parse status response: %w", err))
36+
}
37+
38+
if formatJson {
39+
return jsonStatus(standalone, status, backendStatus)
3740
} else {
38-
cmd.Println("Docker Model Runner is not running")
39-
hooks.PrintNextSteps(cmd.OutOrStdout(), []string{enableViaCLI, enableViaGUI})
40-
osExit(1)
41+
textStatus(cmd, status, backendStatus)
4142
}
4243

4344
return nil
4445
},
4546
ValidArgsFunction: completion.NoComplete,
4647
}
48+
c.Flags().BoolVar(&formatJson, "json", false, "Format output in JSON")
4749
return c
4850
}
4951

52+
func textStatus(cmd *cobra.Command, status desktop.Status, backendStatus map[string]string) {
53+
if status.Running {
54+
cmd.Println("Docker Model Runner is running")
55+
cmd.Println("\nStatus:")
56+
for b, s := range backendStatus {
57+
if s != "not running" {
58+
cmd.Println(b+":", s)
59+
}
60+
}
61+
} else {
62+
cmd.Println("Docker Model Runner is not running")
63+
hooks.PrintNextSteps(cmd.OutOrStdout(), []string{enableViaCLI, enableViaGUI})
64+
osExit(1)
65+
}
66+
}
67+
68+
func jsonStatus(standalone *standaloneRunner, status desktop.Status, backendStatus map[string]string) error {
69+
type Status struct {
70+
Running bool `json:"running"`
71+
Backends map[string]string `json:"backends"`
72+
Endpoint string `json:"endpoint"`
73+
}
74+
var endpoint string
75+
kind := modelRunner.EngineKind()
76+
switch kind {
77+
case desktop.ModelRunnerEngineKindDesktop:
78+
endpoint = "http://model-runner.docker.internal/engines/v1/"
79+
case desktop.ModelRunnerEngineKindMobyManual:
80+
endpoint = modelRunner.URL("/engines/v1/")
81+
case desktop.ModelRunnerEngineKindCloud:
82+
fallthrough
83+
case desktop.ModelRunnerEngineKindMoby:
84+
endpoint = fmt.Sprintf("http://%s:%d/engines/v1", standalone.gatewayIP, standalone.gatewayPort)
85+
default:
86+
return fmt.Errorf("unhandled engine kind: %v", kind)
87+
}
88+
s := Status{
89+
Running: status.Running,
90+
Backends: backendStatus,
91+
Endpoint: endpoint,
92+
}
93+
marshal, err := json.Marshal(s)
94+
if err != nil {
95+
return err
96+
}
97+
fmt.Println(string(marshal))
98+
return nil
99+
}
100+
50101
var osExit = os.Exit

docs/reference/docker_model_status.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ long: |
55
usage: docker model status
66
pname: docker model
77
plink: docker_model.yaml
8+
options:
9+
- option: json
10+
value_type: bool
11+
default_value: "false"
12+
description: Format output in JSON
13+
deprecated: false
14+
hidden: false
15+
experimental: false
16+
experimentalcli: false
17+
kubernetes: false
18+
swarm: false
819
deprecated: false
920
hidden: false
1021
experimental: false

docs/reference/model_status.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
<!---MARKER_GEN_START-->
44
Check if the Docker Model Runner is running
55

6+
### Options
7+
8+
| Name | Type | Default | Description |
9+
|:---------|:-------|:--------|:----------------------|
10+
| `--json` | `bool` | | Format output in JSON |
11+
612

713
<!---MARKER_GEN_END-->
814

0 commit comments

Comments
 (0)