Browse Source

Initial cat that parses file names from the command line

Fredric Silberberg 5 years ago
parent
commit
50c84a2f7d
2 changed files with 60 additions and 0 deletions
  1. 6
    0
      cat.go
  2. 54
    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
+}

+ 54
- 0
cat/main.go View File

@@ -0,0 +1,54 @@
1
+package main
2
+
3
+import (
4
+	"flag"
5
+	"fmt"
6
+	"jsh"
7
+	"io/ioutil"
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 err != nil {
19
+			errText := make(map[string]string)
20
+			errText[fileName] = fmt.Sprintf("%s",err.Error())
21
+			jshFrame = jsh.JshFrame{"", errText}
22
+			queue <- &jshFrame
23
+			continue
24
+		}
25
+
26
+		output := jsh.CatOutput{}
27
+		output.Files = []string{fileName}
28
+		output.Output = string(file)
29
+		jshFrame = jsh.JshFrame{output, ""}
30
+		queue <- &jshFrame
31
+	}
32
+}
33
+
34
+func runJsonMode() {
35
+	queue := make(chan *jsh.JshFrame)
36
+	done := make(chan bool)
37
+	go jsh.OutputFrames(queue, done)
38
+	catFiles(queue)
39
+	close(queue)
40
+	<-done
41
+}
42
+
43
+func main() {
44
+	// TODO: Support more flags
45
+	jsonModePtr := flag.Bool("json", false, "whether to use json mode for input and output")
46
+	flag.Parse()
47
+
48
+	if !*jsonModePtr {
49
+		fmt.Printf("%s", jsh.FallbackWithArgs("/usr/bin/cat", flag.Args()))
50
+	} else {
51
+		runJsonMode()
52
+	}
53
+}
54
+

Loading…
Cancel
Save