From a0add650bef92c19ccdbe959bf313c8cae52bf5c Mon Sep 17 00:00:00 2001 From: Edward Rousseau Date: Fri, 15 Nov 2024 14:58:27 +0000 Subject: [PATCH 1/6] cli: add --json flag to status command --- app/app.go | 111 ++++++++++++++++++++++++++++++++++++-------------- cmd/status.go | 7 +++- 2 files changed, 87 insertions(+), 31 deletions(-) diff --git a/app/app.go b/app/app.go index 289f6c0d1..5870cfdbc 100644 --- a/app/app.go +++ b/app/app.go @@ -30,7 +30,7 @@ type App interface { Stop(force bool) error Delete() error SSH(args ...string) error - Status(extended bool) error + Status(extended bool, jsonOutput bool) error Version() error Runtime() (string, error) Update() error @@ -292,54 +292,105 @@ func (c colimaApp) SSH(args ...string) error { return guest.SSH(workDir, args...) } -func (c colimaApp) Status(extended bool) error { +type status struct { + DisplayName string `json:"display_name"` + Driver string `json:"driver"` + Arch string `json:"arch"` + Runtime string `json:"runtime"` + MountType string `json:"mount_type"` + IPAddress string `json:"ip_address"` + DockerSocket string `json:"docker_socket"` + Kubernetes bool `json:"kubernetes"` + CPU int `json:"cpu"` + Memory int64 `json:"memory"` + Disk int64 `json:"disk"` +} + +func (c colimaApp) getStatus() (*status, error) { ctx := context.Background() if !c.guest.Running(ctx) { - return fmt.Errorf("%s is not running", config.CurrentProfile().DisplayName) + return nil, fmt.Errorf("%s is not running", config.CurrentProfile().DisplayName) } currentRuntime, err := c.currentRuntime(ctx) if err != nil { - return err + return nil, err } - driver := "QEMU" + var status status + status.DisplayName = config.CurrentProfile().DisplayName + status.Driver = "QEMU" conf, _ := configmanager.LoadInstance() if !conf.Empty() { - driver = conf.DriverLabel() + status.Driver = conf.DriverLabel() } - - log.Println(config.CurrentProfile().DisplayName, "is running using", driver) - log.Println("arch:", c.guest.Arch()) - log.Println("runtime:", currentRuntime) - if conf.MountType != "" { - log.Println("mountType:", conf.MountType) + status.Arch = fmt.Sprintf("%v", c.guest.Arch()) + status.Runtime = currentRuntime + status.MountType = conf.MountType + ipAddress := limautil.IPAddress(config.CurrentProfile().ID) + if ipAddress != "127.0.0.1" { + status.IPAddress = limautil.IPAddress(config.CurrentProfile().ID) } - - // ip address - if ipAddress := limautil.IPAddress(config.CurrentProfile().ID); ipAddress != "127.0.0.1" { - log.Println("address:", ipAddress) - } - - // docker socket if currentRuntime == docker.Name { - log.Println("socket:", "unix://"+docker.HostSocketFile()) + status.DockerSocket = fmt.Sprintf("unix://%s", docker.HostSocketFile()) } - - // kubernetes if k, err := c.Kubernetes(); err == nil && k.Running(ctx) { - log.Println("kubernetes: enabled") + status.Kubernetes = true + } + if inst, err := limautil.Instance(); err == nil { + status.CPU = inst.CPU + status.Memory = inst.Memory + status.Disk = inst.Disk } + return &status, nil +} - // additional details - if extended { - if inst, err := limautil.Instance(); err == nil { - log.Println("cpu:", inst.CPU) - log.Println("mem:", units.BytesSize(float64(inst.Memory))) - log.Println("disk:", units.BytesSize(float64(inst.Disk))) - } +func (c colimaApp) Status(extended bool, jsonOutput bool) error { + status, err := c.getStatus() + if err != nil { + return err } + if jsonOutput { + b, _ := json.Marshal(status) + fmt.Println(string(b)) + } else { + + log.Println(config.CurrentProfile().DisplayName, "is running using", status.Driver) + log.Println("arch:", status.Arch) + log.Println("runtime:", status.Runtime) + if status.MountType != "" { + log.Println("mountType:", status.MountType) + } + + // ip address + if status.IPAddress != "" { + log.Println("address:", status.IPAddress) + } + + // docker socket + if status.DockerSocket != "" { + log.Println("socket:", "unix://"+status.DockerSocket) + } + + // kubernetes + if status.Kubernetes { + log.Println("kubernetes: enabled") + } + + // additional details + if extended { + if status.CPU > 0 { + log.Println("cpu:", status.CPU) + } + if status.Memory > 0 { + log.Println("mem:", units.BytesSize(float64(status.Memory))) + } + if status.Disk > 0 { + log.Println("disk:", units.BytesSize(float64(status.Disk))) + } + } + } return nil } diff --git a/cmd/status.go b/cmd/status.go index 61a20c705..30b12a29d 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -7,6 +7,7 @@ import ( var statusCmdArgs struct { extended bool + json bool } // statusCmd represents the status command @@ -16,7 +17,10 @@ var statusCmd = &cobra.Command{ Long: `Show the status of Colima`, Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - return newApp().Status(statusCmdArgs.extended) + if statusCmdArgs.json { + return newApp().Status(statusCmdArgs.extended, true) + } + return newApp().Status(statusCmdArgs.extended, false) }, } @@ -24,4 +28,5 @@ func init() { root.Cmd().AddCommand(statusCmd) statusCmd.Flags().BoolVarP(&statusCmdArgs.extended, "extended", "e", false, "include additional details") + statusCmd.Flags().BoolVarP(&statusCmdArgs.json, "json", "j", false, "print json output") } From e2d55e2f9a0a9aedbf1863c20109b5c96b27b914 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Thu, 21 Nov 2024 14:39:50 +0100 Subject: [PATCH 2/6] chore: minor refactor --- app/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/app.go b/app/app.go index 5870cfdbc..1c269bbd4 100644 --- a/app/app.go +++ b/app/app.go @@ -329,7 +329,7 @@ func (c colimaApp) getStatus() (*status, error) { status.MountType = conf.MountType ipAddress := limautil.IPAddress(config.CurrentProfile().ID) if ipAddress != "127.0.0.1" { - status.IPAddress = limautil.IPAddress(config.CurrentProfile().ID) + status.IPAddress = ipAddress } if currentRuntime == docker.Name { status.DockerSocket = fmt.Sprintf("unix://%s", docker.HostSocketFile()) From 0491f8f6584f5dadf451137c83c0ca0cc529a40f Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sat, 23 Nov 2024 07:02:13 +0100 Subject: [PATCH 3/6] chore: minor refactor --- app/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/app.go b/app/app.go index 1c269bbd4..1abdcf3e3 100644 --- a/app/app.go +++ b/app/app.go @@ -30,7 +30,7 @@ type App interface { Stop(force bool) error Delete() error SSH(args ...string) error - Status(extended bool, jsonOutput bool) error + Status(extended bool, json bool) error Version() error Runtime() (string, error) Update() error From fbc6f936b061491cff1bc8c58afddee5cd25ffa4 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sat, 23 Nov 2024 07:29:49 +0100 Subject: [PATCH 4/6] chore: apply suggestions from code review --- app/app.go | 10 +++++----- cmd/status.go | 5 +---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/app.go b/app/app.go index 1abdcf3e3..e38f6813f 100644 --- a/app/app.go +++ b/app/app.go @@ -292,7 +292,7 @@ func (c colimaApp) SSH(args ...string) error { return guest.SSH(workDir, args...) } -type status struct { +type statusInfo struct { DisplayName string `json:"display_name"` Driver string `json:"driver"` Arch string `json:"arch"` @@ -317,14 +317,14 @@ func (c colimaApp) getStatus() (*status, error) { return nil, err } - var status status + var status statusInfo status.DisplayName = config.CurrentProfile().DisplayName status.Driver = "QEMU" conf, _ := configmanager.LoadInstance() if !conf.Empty() { status.Driver = conf.DriverLabel() } - status.Arch = fmt.Sprintf("%v", c.guest.Arch()) + status.Arch = string(c.guest.Arch()) status.Runtime = currentRuntime status.MountType = conf.MountType ipAddress := limautil.IPAddress(config.CurrentProfile().ID) @@ -332,7 +332,7 @@ func (c colimaApp) getStatus() (*status, error) { status.IPAddress = ipAddress } if currentRuntime == docker.Name { - status.DockerSocket = fmt.Sprintf("unix://%s", docker.HostSocketFile()) + status.DockerSocket = "unix://" + docker.HostSocketFile() } if k, err := c.Kubernetes(); err == nil && k.Running(ctx) { status.Kubernetes = true @@ -370,7 +370,7 @@ func (c colimaApp) Status(extended bool, jsonOutput bool) error { // docker socket if status.DockerSocket != "" { - log.Println("socket:", "unix://"+status.DockerSocket) + log.Println("socket:", status.DockerSocket) } // kubernetes diff --git a/cmd/status.go b/cmd/status.go index 30b12a29d..98a5c1ce6 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -17,10 +17,7 @@ var statusCmd = &cobra.Command{ Long: `Show the status of Colima`, Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if statusCmdArgs.json { - return newApp().Status(statusCmdArgs.extended, true) - } - return newApp().Status(statusCmdArgs.extended, false) + return newApp().Status(statusCmdArgs.extended, statusCmdArgs.json) }, } From 4a0f25c35ddad6e482395ccc897f2b4d453dbc0a Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sat, 23 Nov 2024 07:39:22 +0100 Subject: [PATCH 5/6] chore: apply suggestions from code review --- app/app.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/app.go b/app/app.go index e38f6813f..57f592164 100644 --- a/app/app.go +++ b/app/app.go @@ -306,15 +306,15 @@ type statusInfo struct { Disk int64 `json:"disk"` } -func (c colimaApp) getStatus() (*status, error) { +func (c colimaApp) getStatus() (status statusInfo, err error) { ctx := context.Background() if !c.guest.Running(ctx) { - return nil, fmt.Errorf("%s is not running", config.CurrentProfile().DisplayName) + return status, fmt.Errorf("%s is not running", config.CurrentProfile().DisplayName) } currentRuntime, err := c.currentRuntime(ctx) if err != nil { - return nil, err + return status, err } var status statusInfo @@ -342,7 +342,7 @@ func (c colimaApp) getStatus() (*status, error) { status.Memory = inst.Memory status.Disk = inst.Disk } - return &status, nil + return status, nil } func (c colimaApp) Status(extended bool, jsonOutput bool) error { @@ -352,8 +352,9 @@ func (c colimaApp) Status(extended bool, jsonOutput bool) error { } if jsonOutput { - b, _ := json.Marshal(status) - fmt.Println(string(b)) + if err := json.NewEncoder(os.Stdout).Encode(status); err != nil { + return fmt.Errorf("error encoding status as json: %w", err) + } } else { log.Println(config.CurrentProfile().DisplayName, "is running using", status.Driver) From f774890cc5595d72a6ea5834c2bf2691cc1d8912 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sat, 23 Nov 2024 07:41:55 +0100 Subject: [PATCH 6/6] chore: fix syntax error --- app/app.go | 1 - 1 file changed, 1 deletion(-) diff --git a/app/app.go b/app/app.go index 57f592164..499614f26 100644 --- a/app/app.go +++ b/app/app.go @@ -317,7 +317,6 @@ func (c colimaApp) getStatus() (status statusInfo, err error) { return status, err } - var status statusInfo status.DisplayName = config.CurrentProfile().DisplayName status.Driver = "QEMU" conf, _ := configmanager.LoadInstance()