54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"jsh"
|
|
"strings"
|
|
)
|
|
|
|
// Converts raw output of "ps" into a slice of Filesystem objects
|
|
func DfOutputToFilesystems(out string) *[]jsh.Filesystem {
|
|
filesystems := []jsh.Filesystem{}
|
|
lines := strings.Split(out, "\n")
|
|
header, fses := lines[0], lines[1:]
|
|
numFields := len(strings.Fields(header))
|
|
|
|
for _, fs := range fses {
|
|
allFields := jsh.FieldsN(fs, numFields)
|
|
if len(allFields) != 0 {
|
|
fields := append(allFields[0:4], allFields[5])
|
|
p, err := jsh.NewFilesystem(fields)
|
|
if err == nil {
|
|
filesystems = append(filesystems, *p)
|
|
}
|
|
}
|
|
}
|
|
return &filesystems
|
|
}
|
|
|
|
func runJsonMode() {
|
|
dfOut := string(*jsh.FallbackWithArgs("df", []string{"-B1"}))
|
|
filesystemsPtr := DfOutputToFilesystems(dfOut)
|
|
frame := jsh.JshFrame{*filesystemsPtr, []string{}}
|
|
|
|
queue := make(chan *jsh.JshFrame)
|
|
done := make(chan bool)
|
|
go jsh.OutputFrames(queue, done)
|
|
queue <- &frame
|
|
close(queue)
|
|
<-done
|
|
}
|
|
|
|
func main() {
|
|
// Parse for JSON flag.
|
|
jsonModePtr := flag.Bool("json", false, "a bool")
|
|
flag.Parse()
|
|
|
|
if !*jsonModePtr {
|
|
fmt.Printf("%s", jsh.Fallback("df"))
|
|
} else {
|
|
runJsonMode()
|
|
}
|
|
}
|