Ian Adam Naval 5 years ago
parent
commit
e2ed6a4111
4 changed files with 138 additions and 0 deletions
  1. 53
    0
      df/main.go
  2. 22
    0
      df/main_test.go
  3. 35
    0
      filesystem.go
  4. 28
    0
      filesystem_test.go

+ 53
- 0
df/main.go View File

@@ -0,0 +1,53 @@
1
+package main
2
+
3
+import (
4
+	"flag"
5
+	"fmt"
6
+	"jsh"
7
+	"strings"
8
+)
9
+
10
+// Converts raw output of "ps" into a slice of Filesystem objects
11
+func DfOutputToFilesystems(out string) *[]jsh.Filesystem {
12
+	filesystems := []jsh.Filesystem{}
13
+	lines := strings.Split(out, "\n")
14
+	header, fses := lines[0], lines[1:]
15
+	numFields := len(strings.Fields(header))
16
+
17
+	for _, fs := range fses {
18
+		allFields := jsh.FieldsN(fs, numFields)
19
+		if len(allFields) != 0 {
20
+			fields := append(allFields[0:4], allFields[5])
21
+			p, err := jsh.NewFilesystem(fields)
22
+			if err == nil {
23
+				filesystems = append(filesystems, *p)
24
+			}
25
+		}
26
+	}
27
+	return &filesystems
28
+}
29
+
30
+func runJsonMode() {
31
+	dfOut := string(*jsh.FallbackWithArgs("df", []string{"-B1"}))
32
+	filesystemsPtr := DfOutputToFilesystems(dfOut)
33
+	frame := jsh.JshFrame{*filesystemsPtr, []string{}}
34
+
35
+	queue := make(chan *jsh.JshFrame)
36
+	done := make(chan bool)
37
+	go jsh.OutputFrames(queue, done)
38
+	queue <- &frame
39
+	close(queue)
40
+	<-done
41
+}
42
+
43
+func main() {
44
+	// Parse for JSON flag.
45
+	jsonModePtr := flag.Bool("json", false, "a bool")
46
+	flag.Parse()
47
+
48
+	if !*jsonModePtr {
49
+		fmt.Printf("%s", jsh.Fallback("df"))
50
+	} else {
51
+		runJsonMode()
52
+	}
53
+}

+ 22
- 0
df/main_test.go View File

@@ -0,0 +1,22 @@
1
+package main
2
+
3
+import (
4
+	"jsh"
5
+	"reflect"
6
+	"testing"
7
+)
8
+
9
+func TestPsOutputToProcesses(t *testing.T) {
10
+	psOutput := `USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
11
+root         1  0.0  0.1  26204  4916 ?        Ss   00:39   0:00 /sbin/init
12
+root         2  0.0  0.0      0     0 ?        S    00:39   0:00 [kthreadd]`
13
+	processes := PsOutputToProcesses(psOutput)
14
+	expected := []jsh.Process{jsh.Process{"root", "1", "0.0", "0.1", "26204",
15
+		"4916", "?", "Ss", "00:39", "0:00", "/sbin/init"},
16
+		jsh.Process{"root", "2", "0.0", "0.0", "0", "0", "?", "S", "00:39",
17
+			"0:00", "[kthreadd]"}}
18
+	actual := *processes
19
+	if !reflect.DeepEqual(expected, actual) {
20
+		t.Errorf("Processes not parsed correctly.\n%s != %s", expected, actual)
21
+	}
22
+}

+ 35
- 0
filesystem.go View File

@@ -0,0 +1,35 @@
1
+/* File for Filesystem-related structs and methods */
2
+
3
+package jsh
4
+
5
+import (
6
+	"errors"
7
+	"strconv"
8
+)
9
+
10
+// Filesystem is a struct for marshalling into JSON output for the df utility.
11
+type Filesystem struct {
12
+	Name       string
13
+	Size       int
14
+	Used       int
15
+	Available  int
16
+	MountPoint string
17
+}
18
+
19
+// NewFilesystem
20
+func NewFilesystem(args []string) (procPtr *Filesystem, err error) {
21
+	err = nil
22
+	// 11 = number of fields in the Filesystem struct
23
+	if len(args) != 5 {
24
+		procPtr = &Filesystem{}
25
+		err = errors.New("Unexpected number of columns")
26
+	} else {
27
+		// TODO: add error checking
28
+		size, _ := strconv.Atoi(args[1])
29
+		used, _ := strconv.Atoi(args[2])
30
+		available, _ := strconv.Atoi(args[2])
31
+		procPtr = &Filesystem{
32
+			args[0], size, used, available, args[4]}
33
+	}
34
+	return
35
+}

+ 28
- 0
filesystem_test.go View File

@@ -0,0 +1,28 @@
1
+package jsh
2
+
3
+import "testing"
4
+
5
+func TestNewFilesystem(t *testing.T) {
6
+	tooManyArgs := []string{"1", "2", "3", "4"}
7
+	_, err := NewFilesystem(tooManyArgs)
8
+	if err == nil {
9
+		t.Errorf("Passing 4 strings should raise an error")
10
+	}
11
+
12
+	tooFewArgs := []string{"1", "2", "3", "4", "5", "6"}
13
+	_, err = NewFilesystem(tooFewArgs)
14
+	if err == nil {
15
+		t.Errorf("Passing 6 strings should raise an error")
16
+	}
17
+
18
+	justRightArgs := []string{"1", "2", "3", "4", "5"}
19
+	proc, err := NewFilesystem(justRightArgs)
20
+	if err != nil {
21
+		t.Errorf("Passing 5 strings should not raise an error")
22
+	}
23
+	expected := Filesystem{"1", "2", "3", "4", "5"}
24
+	actual := *proc
25
+	if expected != actual {
26
+		t.Errorf("Filesystem was incorrectly generated:\n%s != %s", expected, actual)
27
+	}
28
+}

Loading…
Cancel
Save