Skip to content

Commit 882e0f4

Browse files
committed
basic ls
1 parent 062ba5d commit 882e0f4

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

cli-files/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ A world of struggle.
6262
```
6363

6464
<<<<<<< HEAD
65+
6566
### go-ls
6667

6768
These steps will leave some work for you to do. If something's not clear, search around for to find the answer. If you're stuck for half an hour at most, ask for help. Remember to use Git to store your progress, committing often in small increments with useful descriptions.
@@ -166,13 +167,6 @@ Let's try it out: `go install . && go-cat`. It will do nothing, but it's a start
166167
Now it's over to you: set up a command that takes a path to a file as an argument, then opens that file and prints it out. You'll need the built-in go functions `os.ReadFile` and `os.Stdout.Write`, as well as more from the `os` package.
167168

168169
Bonus task: handle the error if you pass it a directory rather than a file, like cat does.
169-
=======
170-
### Steps (notes)
171-
172-
- Setup cobra with https://github.com/spf13/cobra/blob/master/user_guide.md
173-
- install in cli-files `go get -u github.com/spf13/cobra@latest`
174-
- `cmd` directory, `root.go`
175-
>>>>>>> 740abac (redo file structure for commands and add gitignores)
176170

177171
[go]: https://go.dev/
178172
[cat]: https://en.m.wikipedia.org/wiki/Cat_(Unix)

cli-files/go-ls/cmd/root.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@ import (
1010
var rootCmd = &cobra.Command{
1111
Use: "go-ls",
1212
Short: "go-ls is a re-implementation of the ls command",
13-
Run: func(cmd *cobra.Command, args []string) {
14-
fmt.Println("Hello, world!")
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
files, err := os.ReadDir(".")
15+
if err != nil {
16+
return err
17+
}
18+
for _, file := range files {
19+
fmt.Println(file.Name())
20+
}
21+
return nil
1522
},
1623
}
1724

0 commit comments

Comments
 (0)