@@ -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
1314func 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 ( " \n Status: " )
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 ("\n Status:" )
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+
50101var osExit = os .Exit
0 commit comments