From ad5102e433be710867578726d867d39cf1208d85 Mon Sep 17 00:00:00 2001 From: Edward Rousseau Date: Sat, 23 Nov 2024 06:54:34 +0000 Subject: [PATCH] cli: add --json flag to status command (#1196) * cli: add --json flag to status command * chore: minor refactor * chore: minor refactor * chore: apply suggestions from code review * chore: apply suggestions from code review * chore: fix syntax error --------- Co-authored-by: Abiola Ibrahim --- app/app.go | 111 ++++++++++++++++++++++++++++++++++++-------------- cmd/status.go | 4 +- 2 files changed, 84 insertions(+), 31 deletions(-) diff --git a/app/app.go b/app/app.go index 289f6c0d1..499614f26 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, json 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 statusInfo 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 statusInfo, err error) { ctx := context.Background() if !c.guest.Running(ctx) { - return 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 err + return status, err } - driver := "QEMU" + 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 = string(c.guest.Arch()) + status.Runtime = currentRuntime + status.MountType = conf.MountType + ipAddress := limautil.IPAddress(config.CurrentProfile().ID) + if ipAddress != "127.0.0.1" { + status.IPAddress = ipAddress } - - // 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 = "unix://" + 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 { + 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) + 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:", 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..98a5c1ce6 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,7 @@ 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) + return newApp().Status(statusCmdArgs.extended, statusCmdArgs.json) }, } @@ -24,4 +25,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") }