Skip to content

Commit

Permalink
cli: add --output=json flag to status command
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrousseau committed Nov 15, 2024
1 parent 38c20fc commit 2363161
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 31 deletions.
111 changes: 81 additions & 30 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

var statusCmdArgs struct {
extended bool
output string
}

// statusCmd represents the status command
Expand All @@ -16,12 +17,16 @@ 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.output == "json" {
return newApp().Status(statusCmdArgs.extended, true)
}
return newApp().Status(statusCmdArgs.extended, false)
},
}

func init() {
root.Cmd().AddCommand(statusCmd)

statusCmd.Flags().BoolVarP(&statusCmdArgs.extended, "extended", "e", false, "include additional details")
statusCmd.Flags().StringVarP(&statusCmdArgs.output, "output", "o", "", "output format (only 'json' is supported)")
}

0 comments on commit 2363161

Please sign in to comment.