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/ps/main.go
2014-09-16 21:43:43 -04:00

51 lines
1.0 KiB
Go

package main
import (
"flag"
"fmt"
"jsh"
"strings"
)
// Converts raw output of "ps" into a slice of Process objects
func PsOutputToProcesses(out string) *[]jsh.Process {
processes := []jsh.Process{}
lines := strings.Split(out, "\n")
header, procs := lines[0], lines[1:]
numFields := len(strings.Fields(header))
for _, proc := range procs {
p, err := jsh.NewProcess(jsh.FieldsN(proc, numFields))
if err == nil {
processes = append(processes, *p)
}
}
return &processes
}
func runJsonMode() {
// Run procps-ng "ps" with full output
psOut := string(*jsh.FallbackWithArgs("/usr/bin/ps", []string{"auxww"}))
processesPtr := PsOutputToProcesses(psOut)
frame := jsh.JshFrame{*processesPtr, []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("/usr/bin/ps"))
} else {
runJsonMode()
}
}