From 50c84a2f7df5169f7384c19465b88ff9b03702f8 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Wed, 24 Sep 2014 22:24:46 -0400 Subject: [PATCH] Initial cat that parses file names from the command line --- cat.go | 6 ++++++ cat/main.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 cat.go create mode 100644 cat/main.go diff --git a/cat.go b/cat.go new file mode 100644 index 0000000..2d14bf1 --- /dev/null +++ b/cat.go @@ -0,0 +1,6 @@ +package jsh + +type CatOutput struct { + Output string + Files []string +} diff --git a/cat/main.go b/cat/main.go new file mode 100644 index 0000000..6959ea6 --- /dev/null +++ b/cat/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "flag" + "fmt" + "jsh" + "io/ioutil" +) + +func catFiles(queue chan *jsh.JshFrame) { + // Get all of the non-flags. The rest were processed by the main function + fileNames := flag.Args() + for _,fileName := range fileNames { + file, err := ioutil.ReadFile(fileName) + + var jshFrame jsh.JshFrame + + if err != nil { + errText := make(map[string]string) + errText[fileName] = fmt.Sprintf("%s",err.Error()) + jshFrame = jsh.JshFrame{"", errText} + queue <- &jshFrame + continue + } + + output := jsh.CatOutput{} + output.Files = []string{fileName} + output.Output = string(file) + jshFrame = jsh.JshFrame{output, ""} + queue <- &jshFrame + } +} + +func runJsonMode() { + queue := make(chan *jsh.JshFrame) + done := make(chan bool) + go jsh.OutputFrames(queue, done) + catFiles(queue) + close(queue) + <-done +} + +func main() { + // TODO: Support more flags + jsonModePtr := flag.Bool("json", false, "whether to use json mode for input and output") + flag.Parse() + + if !*jsonModePtr { + fmt.Printf("%s", jsh.FallbackWithArgs("/usr/bin/cat", flag.Args())) + } else { + runJsonMode() + } +} +