Skip to content

Commit

Permalink
polish code
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Jun 4, 2020
1 parent 39b4e19 commit 328d85b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
26 changes: 18 additions & 8 deletions db/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package db

import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)

type Box struct {
CPU string
Mem string
HDLocation string
MACAddr string
CPU string
Mem string
DiskSize string
MACAddr string
}

type vbox struct {
Expand Down Expand Up @@ -52,9 +54,17 @@ func GetBoxInfo(vm string) (*Box, error) {
cpuCount = "1"
}
return &Box{
CPU: cpuCount,
Mem: vb.Machine.Hardware.Memory.RAMSize,
HDLocation: vb.Machine.MediaRegistry.HardDisks.HardDisk.Location,
MACAddr: vb.Machine.Hardware.Network.Adapter.MACAddress,
CPU: cpuCount,
Mem: vb.Machine.Hardware.Memory.RAMSize,
DiskSize: getDiskSizeInGB(vm, vb.Machine.MediaRegistry.HardDisks.HardDisk.Location),
MACAddr: vb.Machine.Hardware.Network.Adapter.MACAddress,
}, nil
}

func getDiskSizeInGB(vm string, hdLocation string) string {
stat, err := os.Stat(GetVMPath(vm) + string(os.PathSeparator) + hdLocation)
if err != nil {
return ""
}
return fmt.Sprintf("%.1fGB", float64(stat.Size())/(1042*1024*1024.0))
}
13 changes: 1 addition & 12 deletions vms/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ type vmInfo struct {
name string
image string
box db.Box
disk string
tags string
}

func (v vmInfo) String() string {
return fmt.Sprintf(format, v.name, v.image, v.box.CPU, v.box.Mem, v.disk, v.tags)
return fmt.Sprintf(format, v.name, v.image, v.box.CPU, v.box.Mem, v.box.DiskSize, v.tags)
}

type vmInfoList []vmInfo
Expand Down Expand Up @@ -172,22 +171,12 @@ func getVMInfo(vm string) vmInfo {
}

box, _ := db.GetBoxInfo(vm)
disk := getDiskSizeInGB(vm, box.HDLocation)
vmdb, _ := db.Load(vm)

return vmInfo{
name: vm,
image: vmdb.Image,
box: *box,
disk: disk,
tags: strings.Join(vmdb.Tags, ", "),
}
}

func getDiskSizeInGB(vm string, hdLocation string) string {
stat, err := os.Stat(db.GetVMPath(vm) + string(os.PathSeparator) + hdLocation)
if err != nil {
return ""
}
return fmt.Sprintf("%.1fGB", float64(stat.Size())/(1042*1024*1024.0))
}

0 comments on commit 328d85b

Please sign in to comment.