mirror of
https://github.com/hamburghammer/gstat.git
synced 2024-09-28 23:26:05 +02:00
Augusto Dwenger J
f004c93773
This implementation still isn't perfect. One of the encoutered problems is that it does not omit Memory sturcts that are technicly empty. Resulting in JSON with empty objects. This should be a problem for other parsers but it doesn't look good.
44 lines
999 B
Go
44 lines
999 B
Go
package commands
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/hamburghammer/gstat/args"
|
|
goDisk "github.com/shirou/gopsutil/disk"
|
|
)
|
|
|
|
// Disk the struct for the DI to compose the disk space reader
|
|
type Disk struct {
|
|
ReadDiskStats func(string) (*goDisk.UsageStat, error)
|
|
}
|
|
|
|
// NewDisk is a ctor for the Disk struct
|
|
func NewDisk() Disk {
|
|
return Disk{ReadDiskStats: goDisk.Usage}
|
|
}
|
|
|
|
// Exec gets the disk space value for the root partition and maps it to the executiondata struct
|
|
func (d Disk) Exec(args args.Arguments) ([]byte, error) {
|
|
memory, err := d.PureExec(args)
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
data := struct {
|
|
Disk Memory `json:"disk"`
|
|
}{Disk: memory}
|
|
return json.Marshal(data)
|
|
}
|
|
|
|
func (d Disk) PureExec(args args.Arguments) (Memory, error) {
|
|
if !args.Disk {
|
|
return Memory{}, nil
|
|
}
|
|
|
|
usage, err := d.ReadDiskStats("/")
|
|
if err != nil {
|
|
return Memory{}, err
|
|
}
|
|
|
|
memory := Memory{Used: bytesToMegaByte(usage.Used), Total: bytesToMegaByte(usage.Total)}
|
|
return memory, nil
|
|
}
|