Skip to content

Commit 019ff5f

Browse files
committed
basic ls
1 parent 65a5a30 commit 019ff5f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

cli-files/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ A world of struggle.
6363

6464
### Steps (notes)
6565

66-
- Setup cobra with https://github.com/spf13/cobra/blob/master/user_guide.md
67-
- install in cli-files `go get -u github.com/spf13/cobra@latest`
68-
- `cmd` directory, `root.go`
66+
- create `go-ls` dir
67+
- create `go.mod`, `main.go`, `cmd/root.go`
68+
- `go get -u github.com/spf13/cobra@latest`
69+
- follow [user guide](https://github.com/spf13/cobra/blob/master/user_guide.md) to `main.go` and `root.go` with a root command that prints hello
70+
- implement basic ls with `os.ReadDir`
6971

7072
[go]: https://go.dev/
7173
[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)