Browse Source

Merge branch 'feature/cat' into 'master'

Feature/cat

Initial implementation of cat. So far, this only parses from the command line. I'll add in parsing from input frames once we work out some pipe merging issues.

See merge request !3
Ian Adam Naval 5 years ago
parent
commit
1fc595e559
2 changed files with 61 additions and 0 deletions
  1. 6
    0
      cat.go
  2. 55
    0
      cat/main.go

+ 6
- 0
cat.go View File

@@ -0,0 +1,6 @@
1
+package jsh
2
+
3
+type CatOutput struct {
4
+	Output string
5
+	Files  []string
6
+}

+ 55
- 0
cat/main.go View File

@@ -0,0 +1,55 @@
1
+package main
2
+
3
+import (
4
+	"flag"
5
+	"fmt"
6
+	"io/ioutil"
7
+	"jsh"
8
+)
9
+
10
+func catFiles(queue chan *jsh.JshFrame) {
11
+	// Get all of the non-flags. The rest were processed by the main function
12
+	fileNames := flag.Args()
13
+	for _, fileName := range fileNames {
14
+		file, err := ioutil.ReadFile(fileName)
15
+
16
+		var jshFrame jsh.JshFrame
17
+
18
+		// If there's an error, output an error frame to the queue and continue
19
+		if err != nil {
20
+			errText := make(map[string]string)
21
+			errText[fileName] = fmt.Sprintf("%s", err.Error())
22
+			jshFrame = jsh.JshFrame{"", errText}
23
+			queue <- &jshFrame
24
+			continue
25
+		}
26
+
27
+		// Put the file contents into a frame and send them to the output
28
+		output := jsh.CatOutput{}
29
+		output.Files = []string{fileName}
30
+		output.Output = string(file)
31
+		jshFrame = jsh.JshFrame{output, ""}
32
+		queue <- &jshFrame
33
+	}
34
+}
35
+
36
+func runJsonMode() {
37
+	queue := make(chan *jsh.JshFrame)
38
+	done := make(chan bool)
39
+	go jsh.OutputFrames(queue, done)
40
+	catFiles(queue)
41
+	close(queue)
42
+	<-done
43
+}
44
+
45
+func main() {
46
+	// TODO: Support more flags
47
+	jsonModePtr := flag.Bool("json", false, "whether to use json mode for input and output")
48
+	flag.Parse()
49
+
50
+	if !*jsonModePtr {
51
+		fmt.Printf("%s", jsh.FallbackWithArgs("cat", flag.Args()))
52
+	} else {
53
+		runJsonMode()
54
+	}
55
+}

Loading…
Cancel
Save