This repository has been archived on 2015-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
jsh/df/main.go
Ian Adam Naval e2ed6a4111 Add df
2014-10-14 16:32:08 -04:00

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()
}
}