File tree Expand file tree Collapse file tree 5 files changed +63
-1
lines changed Expand file tree Collapse file tree 5 files changed +63
-1
lines changed Original file line number Diff line number Diff line change
1
+ “A World of Dew” by Kobayashi Issa
2
+
3
+ A world of dew,
4
+ And within every dewdrop
5
+ A world of struggle.
Original file line number Diff line number Diff line change
1
+ Sonia Sanchez “Haiku [for you]”
2
+
3
+ Love between us is
4
+ speech and breath. Loving you is
5
+ a long river running.
Original file line number Diff line number Diff line change
1
+ “The Taste of Rain” by Jack Kerouac
2
+
3
+ The taste
4
+ Of rain
5
+ —Why kneel?
Original file line number Diff line number Diff line change @@ -17,7 +17,34 @@ func NewRoodCmd() *cobra.Command {
17
17
Use : "go-cat" ,
18
18
Short : "Go implementation of cat" ,
19
19
Long : `Works like cat` ,
20
- Run : func (cmd * cobra.Command , args []string ) {
20
+ RunE : func (cmd * cobra.Command , args []string ) error {
21
+ // Don't do anything if we didn't get an arg
22
+ if len (args ) < 1 {
23
+ return nil
24
+ }
25
+ path := args [0 ]
26
+
27
+ // Get data about the file so we can do this safely
28
+ file , err := os .Stat (path )
29
+ if err != nil {
30
+ return err
31
+ }
32
+
33
+ // If it's a directory, do the right thing and error
34
+ if file .IsDir () {
35
+ return fmt .Errorf ("go-cat: %s: Is a directory" , path )
36
+ }
37
+
38
+ data , err := os .ReadFile (path )
39
+ if err != nil {
40
+ return err
41
+ }
42
+
43
+ // Print those lovely bytes
44
+ out := cmd .OutOrStdout ()
45
+ out .Write (data )
46
+
47
+ return nil
21
48
},
22
49
}
23
50
}
Original file line number Diff line number Diff line change @@ -21,3 +21,23 @@ func TestRootCmd(t *testing.T) {
21
21
t .Fatalf ("expected \" %s\" got \" %s\" " , expected , string (out ))
22
22
}
23
23
}
24
+
25
+ func TestRootCmdWithFile (t * testing.T ) {
26
+ cmd := NewRoodCmd ()
27
+ b := bytes .NewBufferString ("" )
28
+ cmd .SetOut (b )
29
+ cmd .SetArgs ([]string {"../assets/dew.txt" })
30
+ cmd .Execute ()
31
+ out , err := ioutil .ReadAll (b )
32
+ if err != nil {
33
+ t .Fatal (err )
34
+ }
35
+ expected := `“A World of Dew” by Kobayashi Issa
36
+
37
+ A world of dew,
38
+ And within every dewdrop
39
+ A world of struggle.`
40
+ if string (out ) != expected {
41
+ t .Fatalf ("expected \" %s\" got \" %s\" " , expected , string (out ))
42
+ }
43
+ }
You can’t perform that action at this time.
0 commit comments