From 50c84a2f7df5169f7384c19465b88ff9b03702f8 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Wed, 24 Sep 2014 22:24:46 -0400 Subject: [PATCH 1/3] 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() + } +} + From 64600ac6688d14d7b5e2f55a31aa9df7eeb623c7 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Wed, 24 Sep 2014 22:29:00 -0400 Subject: [PATCH 2/3] Go fmt --- cat.go | 2 +- cat/main.go | 9 +++++---- free/main_test.go | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/cat.go b/cat.go index 2d14bf1..6e5b0f7 100644 --- a/cat.go +++ b/cat.go @@ -2,5 +2,5 @@ package jsh type CatOutput struct { Output string - Files []string + Files []string } diff --git a/cat/main.go b/cat/main.go index 6959ea6..5cb2ff0 100644 --- a/cat/main.go +++ b/cat/main.go @@ -3,26 +3,28 @@ package main import ( "flag" "fmt" - "jsh" "io/ioutil" + "jsh" ) 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 { + for _, fileName := range fileNames { file, err := ioutil.ReadFile(fileName) var jshFrame jsh.JshFrame + // If there's an error, output an error frame to the queue and continue if err != nil { errText := make(map[string]string) - errText[fileName] = fmt.Sprintf("%s",err.Error()) + errText[fileName] = fmt.Sprintf("%s", err.Error()) jshFrame = jsh.JshFrame{"", errText} queue <- &jshFrame continue } + // Put the file contents into a frame and send them to the output output := jsh.CatOutput{} output.Files = []string{fileName} output.Output = string(file) @@ -51,4 +53,3 @@ func main() { runJsonMode() } } - diff --git a/free/main_test.go b/free/main_test.go index c60e170..c09603e 100644 --- a/free/main_test.go +++ b/free/main_test.go @@ -2,8 +2,8 @@ package main import ( "jsh" - "testing" "reflect" + "testing" ) func testUnitError(expected jsh.Unit, t *testing.T) { @@ -45,14 +45,14 @@ func TestConvertBadUnit(t *testing.T) { func TestParseGoodLine(t *testing.T) { goodLine := "MemTotal: 16370344 kB" - expectedStat := jsh.MemStat { 16370344, jsh.KB } + expectedStat := jsh.MemStat{16370344, jsh.KB} expectedKey := "MemTotal" key, stat, err := ParseLine(goodLine) if err != nil { t.Error(err) } if key != expectedKey { - t.Errorf("Expected key %s, got key %s", expectedKey, key) + t.Errorf("Expected key %s, got key %s", expectedKey, key) } if !reflect.DeepEqual(expectedStat, stat) { t.Error("Expected stat ", expectedStat, " got ", stat) From 9b8d1e01b6272bb0c0085424c1d0313f206edba2 Mon Sep 17 00:00:00 2001 From: Fredric Silberberg Date: Wed, 24 Sep 2014 23:39:07 -0400 Subject: [PATCH 3/3] Updated cat to use the new path searching algorithm --- cat/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cat/main.go b/cat/main.go index 5cb2ff0..cfcd629 100644 --- a/cat/main.go +++ b/cat/main.go @@ -48,7 +48,7 @@ func main() { flag.Parse() if !*jsonModePtr { - fmt.Printf("%s", jsh.FallbackWithArgs("/usr/bin/cat", flag.Args())) + fmt.Printf("%s", jsh.FallbackWithArgs("cat", flag.Args())) } else { runJsonMode() }