Browse Source

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

Fredric Silberberg 5 years ago
parent
commit
d26ca0b13a
2 changed files with 22 additions and 23 deletions
  1. 2
    23
      ps/main.go
  2. 20
    0
      utils.go

+ 2
- 23
ps/main.go View File

@@ -4,8 +4,6 @@ import (
4 4
 	"flag"
5 5
 	"fmt"
6 6
 	"jsh"
7
-	"os"
8
-	"os/exec"
9 7
 	"strings"
10 8
 )
11 9
 
@@ -25,28 +23,9 @@ func PsOutputToProcesses(out string) *[]jsh.Process {
25 23
 	return &processes
26 24
 }
27 25
 
28
-// Falls back to procps-ng passing the space-separated arguments in the given
29
-// args slice. If args is nil, we default to the arguments passed to the command
30
-// line using os.Args
31
-func fallbackCompletelyWithArgs(args []string) *[]byte {
32
-	if args == nil {
33
-		args = os.Args[1:]
34
-	}
35
-	out, err := exec.Command("/usr/bin/ps", args...).Output()
36
-	if err != nil {
37
-		panic(err)
38
-	}
39
-	return &out
40
-}
41
-
42
-// Falls back to procps-ng with no default arguments
43
-func fallbackCompletely() *[]byte {
44
-	return fallbackCompletelyWithArgs(nil)
45
-}
46
-
47 26
 func runJsonMode() {
48 27
 	// Run procps-ng "ps" with full output
49
-	psOut := string(*fallbackCompletelyWithArgs([]string{"auxww"}))
28
+	psOut := string(*jsh.FallbackWithArgs("/usr/bin/ps",  []string{"auxww"}))
50 29
 
51 30
 	processesPtr := PsOutputToProcesses(psOut)
52 31
 	finalOut := jsh.JshOutput{*processesPtr, []string{}}
@@ -59,7 +38,7 @@ func main() {
59 38
 	flag.Parse()
60 39
 
61 40
 	if !*jsonModePtr {
62
-		fmt.Printf("%s", fallbackCompletely())
41
+		fmt.Printf("%s", jsh.Fallback("/usr/bin/ps"))
63 42
 	} else {
64 43
 		runJsonMode()
65 44
 	}

+ 20
- 0
utils.go View File

@@ -3,6 +3,8 @@ package jsh
3 3
 import (
4 4
 	"math"
5 5
 	"unicode"
6
+	"os"
7
+	"os/exec"
6 8
 )
7 9
 
8 10
 // FieldsN slices s into substrings after each instance of a whitespace
@@ -42,3 +44,21 @@ func FieldsN(s string, maxN int) []string {
42 44
 	}
43 45
 	return a
44 46
 }
47
+
48
+// Falls back to procps-ng passing the space-separated arguments in the given
49
+// args slice. If args is nil, we default to the arguments passed to the command
50
+// line using os.Args
51
+func FallbackWithArgs(program string, args []string) *[]byte {
52
+	if args == nil {
53
+		args = os.Args[1:]
54
+	}
55
+	out, err := exec.Command(program, args...).Output()
56
+	if err != nil {
57
+		panic(err)
58
+	}
59
+	return &out	
60
+}
61
+
62
+func Fallback(program string) *[]byte {
63
+	return FallbackWithArgs(program, nil)
64
+}

Loading…
Cancel
Save