You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: cli-files/README.md
+57-15Lines changed: 57 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ Objectives:
13
13
- Open, read (and close) files from CLI arguments
14
14
- Reading directories for files
15
15
16
-
## Instructions
16
+
## Project
17
17
18
18
You're going to build a command-line application that reads data from your computer's file system.
19
19
@@ -61,11 +61,17 @@ And within every dewdrop
61
61
A world of struggle.
62
62
```
63
63
64
-
### Steps (notes)
64
+
### go-ls
65
65
66
-
- create `go-ls` dir
67
-
- make sure the working directory is go-ls: `cd go-ls`
68
-
- create `go.mod`, `main.go`, `cmd/root.go`
66
+
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.
67
+
68
+
A full implementation of this is available on the `impl/go-ls` branch.
69
+
70
+
The `go-ls` directory is provided for you with some example files in the `assets/` folder.
71
+
72
+
In your command line/terminal, make sure your working directory is go-ls: `cd go-ls`
- follow cobra [user guide](https://github.com/spf13/cobra/blob/master/user_guide.md) to make a root command that prints hello in `cmd/root.go`
99
-
- implement basic ls with `os.ReadDir`
100
-
- allow the command to take arguments with `cobra.ArbitraryArgs`
101
-
- when passed an argument such as `go-ls assets`, read from the passed directory
102
-
- ensure that this directory path can be relative: `go-ls ..` and `go-ls ../go-ls` should both work
103
-
- handle the error (e.g. `Error: fdopendir go.mod: not a directory` when passing `go-ls` a file argument: `go-ls go.mod`)
104
-
- update `go-ls` to match `ls` in terms of how it handles files (hint: `os.Stat`)
105
-
- make `go-ls -h` include a helpful description
106
-
- bonus: write some tests for `go-ls`
103
+
We're going to use the [Cobra][cobra] package to make these commands. It does a lot of magic for you.
104
+
105
+
Install the Cobra package using the `go get` command: `go get -u github.com/spf13/cobra@latest`
106
+
107
+
The Cobra [user guide](https://github.com/spf13/cobra/blob/master/user_guide.md) will show you how to make a root command that prints hello in `cmd/root.go`.
108
+
109
+
To use your command, install and run it: `go install .`
110
+
111
+
Then run the following so that your command line knows where to look for the executable code that Go is generating: `export PATH=$PATH:$(dirname $(go list -f '{{.Target}}' .))`
112
+
113
+
You should now be able to run `go-ls`.
114
+
115
+
Now, when you change your code, install and run it: `go install . && go-ls`
116
+
117
+
Now you've got something working, we'll speed up the steps. You can do it!
118
+
119
+
The `ls` command reads a directory, generating a list of files or sub-directories. Go comes with packages for interacting with files built-in, include a package called [os][os], which contains a function called `ReadDir` which will do lots of the work of `ls` for you.
120
+
121
+
See if you can implement basic `ls` with `os.ReadDir`. It should read the list of files in the current, "working" directory:
122
+
123
+
```
124
+
> go install .
125
+
> cd assets
126
+
> go-ls
127
+
dew.txt
128
+
for_you.txt
129
+
rain.txt
130
+
```
131
+
132
+
The real `ls` allows you pass a directory to be read: `ls assets`.
133
+
134
+
Extend your `go-ls` to allow the command to take arguments (look for `cobra.ArbitraryArgs`) and then, when passed an argument such as `go-ls assets`, read from the passed directory.
135
+
136
+
Make that this directory path can be relative: `go-ls ..` and `go-ls ../go-ls` should both work.
137
+
138
+
Handle the error `Error: fdopendir go.mod: not a directory` when passing `go-ls` a file argument: `go-ls go.mod`. Think hard about why this is happening before you try to fix it.
139
+
140
+
Update `go-ls` to match `ls` in terms of how it handles files (hint: `os.Stat`) — it should just output the name of the file.
141
+
142
+
Make `go-ls -h` include a helpful description.
143
+
144
+
If you smash through this, here's some fun/tricky extensions:
145
+
146
+
- Write some tests for `go-ls`
147
+
- Extend `go-ls` to support some more features of the real `ls` (for example, `ls -m assets`)
0 commit comments