2020-06-28 21:44:29 +02:00
|
|
|
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) {
|
2021-01-02 18:10:16 +01:00
|
|
|
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) {
|
2020-06-28 21:44:29 +02:00
|
|
|
if !args.Disk {
|
2021-01-02 18:10:16 +01:00
|
|
|
return Memory{}, nil
|
2020-06-28 21:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
usage, err := d.ReadDiskStats("/")
|
|
|
|
if err != nil {
|
2021-01-02 18:10:16 +01:00
|
|
|
return Memory{}, err
|
2020-06-28 21:44:29 +02:00
|
|
|
}
|
|
|
|
|
2021-01-02 18:10:16 +01:00
|
|
|
memory := Memory{Used: bytesToMegaByte(usage.Used), Total: bytesToMegaByte(usage.Total)}
|
|
|
|
return memory, nil
|
2020-06-28 21:44:29 +02:00
|
|
|
}
|