Browse Source

Make ps follow new standard

Ian Adam Naval 5 years ago
parent
commit
fb8165fa80
3 changed files with 37 additions and 9 deletions
  1. 26
    3
      common.go
  2. 3
    3
      free/main.go
  3. 8
    3
      ps/main.go

+ 26
- 3
common.go View File

@@ -4,9 +4,10 @@ package jsh
4 4
 
5 5
 import (
6 6
 	"encoding/json"
7
+	"fmt"
7 8
 )
8 9
 
9
-type JshOutput struct {
10
+type JshFrame struct {
10 11
 	StdOut interface{}
11 12
 	StdErr interface{}
12 13
 }
@@ -22,8 +23,8 @@ const (
22 23
 	TB Unit = 2 ^ 40
23 24
 )
24 25
 
25
-// Converts a Jshoutput into a JSON string
26
-func (j JshOutput) ToJson() *string {
26
+// Converts a JshFrame into a JSON string
27
+func (j JshFrame) ToJson() *string {
27 28
 	jsonOut, err := json.Marshal(j)
28 29
 	if err != nil {
29 30
 		panic(err)
@@ -31,3 +32,25 @@ func (j JshOutput) ToJson() *string {
31 32
 	jsonString := string(jsonOut)
32 33
 	return &jsonString
33 34
 }
35
+
36
+// goroutine for outputing frames. Pass it a channel of pointers to JshFrames,
37
+// and it will send "true" to the done channel once you close the queue channel.
38
+func OutputFrames(queue chan *JshFrame, done chan bool) {
39
+	fmt.Printf("[")
40
+	isFirst := true
41
+	for {
42
+		frame, more := <-queue
43
+		if more {
44
+			if !isFirst {
45
+				fmt.Printf(",")
46
+			} else {
47
+				isFirst = false
48
+			}
49
+			fmt.Printf(*frame.ToJson())
50
+		} else {
51
+			fmt.Printf("]\n")
52
+			done <- true
53
+			return
54
+		}
55
+	}
56
+}

+ 3
- 3
free/main.go View File

@@ -53,10 +53,10 @@ func parseLine(line string) (string, jsh.MemStat, error) {
53 53
 	return key, jsh.MemStat{val, unit}, nil
54 54
 }
55 55
 
56
-func parseMemInfo() jsh.JshOutput {
56
+func parseMemInfo() jsh.JshFrame {
57 57
 	file, err := os.Open("/proc/meminfo")
58 58
 	if err != nil {
59
-		return jsh.JshOutput{[]string{}, []error{err}}
59
+		return jsh.JshFrame{[]string{}, []error{err}}
60 60
 	}
61 61
 	defer file.Close()
62 62
 
@@ -72,7 +72,7 @@ func parseMemInfo() jsh.JshOutput {
72 72
 		memInfo[key] = val
73 73
 	}
74 74
 
75
-	finalOut := jsh.JshOutput{memInfo, errors}
75
+	finalOut := jsh.JshFrame{memInfo, errors}
76 76
 	return finalOut
77 77
 }
78 78
 

+ 8
- 3
ps/main.go View File

@@ -26,10 +26,15 @@ func PsOutputToProcesses(out string) *[]jsh.Process {
26 26
 func runJsonMode() {
27 27
 	// Run procps-ng "ps" with full output
28 28
 	psOut := string(*jsh.FallbackWithArgs("/usr/bin/ps", []string{"auxww"}))
29
-
30 29
 	processesPtr := PsOutputToProcesses(psOut)
31
-	finalOut := jsh.JshOutput{*processesPtr, []string{}}
32
-	fmt.Printf("%s", *finalOut.ToJson())
30
+	frame := jsh.JshFrame{*processesPtr, []string{}}
31
+
32
+	queue := make(chan *jsh.JshFrame)
33
+	done := make(chan bool)
34
+	go jsh.OutputFrames(queue, done)
35
+	queue <- &frame
36
+	close(queue)
37
+	<-done
33 38
 }
34 39
 
35 40
 func main() {

Loading…
Cancel
Save