51 lines
1.0 KiB
Go
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()
|
|
}
|
|
}
|