Moved standard binary fallback to utility function for all utilities to use

This commit is contained in:
Fredric Silberberg 2014-09-07 22:55:57 +00:00
parent 69f68794cf
commit d26ca0b13a
2 changed files with 22 additions and 23 deletions

View File

@ -4,8 +4,6 @@ import (
"flag"
"fmt"
"jsh"
"os"
"os/exec"
"strings"
)
@ -25,28 +23,9 @@ func PsOutputToProcesses(out string) *[]jsh.Process {
return &processes
}
// Falls back to procps-ng passing the space-separated arguments in the given
// args slice. If args is nil, we default to the arguments passed to the command
// line using os.Args
func fallbackCompletelyWithArgs(args []string) *[]byte {
if args == nil {
args = os.Args[1:]
}
out, err := exec.Command("/usr/bin/ps", args...).Output()
if err != nil {
panic(err)
}
return &out
}
// Falls back to procps-ng with no default arguments
func fallbackCompletely() *[]byte {
return fallbackCompletelyWithArgs(nil)
}
func runJsonMode() {
// Run procps-ng "ps" with full output
psOut := string(*fallbackCompletelyWithArgs([]string{"auxww"}))
psOut := string(*jsh.FallbackWithArgs("/usr/bin/ps", []string{"auxww"}))
processesPtr := PsOutputToProcesses(psOut)
finalOut := jsh.JshOutput{*processesPtr, []string{}}
@ -59,7 +38,7 @@ func main() {
flag.Parse()
if !*jsonModePtr {
fmt.Printf("%s", fallbackCompletely())
fmt.Printf("%s", jsh.Fallback("/usr/bin/ps"))
} else {
runJsonMode()
}

View File

@ -3,6 +3,8 @@ package jsh
import (
"math"
"unicode"
"os"
"os/exec"
)
// FieldsN slices s into substrings after each instance of a whitespace
@ -42,3 +44,21 @@ func FieldsN(s string, maxN int) []string {
}
return a
}
// Falls back to procps-ng passing the space-separated arguments in the given
// args slice. If args is nil, we default to the arguments passed to the command
// line using os.Args
func FallbackWithArgs(program string, args []string) *[]byte {
if args == nil {
args = os.Args[1:]
}
out, err := exec.Command(program, args...).Output()
if err != nil {
panic(err)
}
return &out
}
func Fallback(program string) *[]byte {
return FallbackWithArgs(program, nil)
}