and search the PATH for that location. If found, it will search all remaining elements for the requested program. If the location is not found on the PATH, then it will search the entire PATH for the requested program. If found, it returns the absolute path to the program. If not, err.
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("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("ps"))
|
|
} else {
|
|
runJsonMode()
|
|
}
|
|
}
|