Skip to content

Commit 51853df

Browse files
committed
import readme from impl
1 parent 4addc41 commit 51853df

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

cli-files/README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Create `go.mod`, `main.go`, `cmd/root.go`. The `touch` command creates files: `t
7575

7676
```go
7777
// go.mod
78-
module vinery/cli-files/go-ls
78+
module go-ls
7979

8080
go 1.18
8181
```
@@ -85,7 +85,7 @@ go 1.18
8585
package main
8686

8787
import (
88-
"vinery/cli-files/go-ls/cmd"
88+
"go-ls/cmd"
8989
)
9090

9191
func main() {
@@ -108,9 +108,9 @@ The Cobra [user guide](https://github.com/spf13/cobra/blob/master/user_guide.md)
108108

109109
To use your command, install and run it: `go install .`
110110

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}}' .))`
111+
To run the code, you need to tell your command line where executable code compiled from go lives. The way to do this is different depending on your operating system, but here's [a guide on the Go website](https://go.dev/doc/install) — look at anything that mentions `go/bin` on your `PATH`.
112112

113-
You should now be able to run `go-ls`.
113+
Once added, you should now be able to run `go-ls`.
114114

115115
Now, when you change your code, install and run it: `go install . && go-ls`
116116

@@ -146,6 +146,26 @@ If you smash through this, here's some fun/tricky extensions:
146146
- Write some tests for `go-ls`
147147
- Extend `go-ls` to support some more features of the real `ls` (for example, `ls -m assets`)
148148

149+
### go-cat
150+
151+
This one we're going to make in a different, so we can see how to use tools to initialise go projects more quickly.
152+
153+
We'll use the [cobra-cli](https://github.com/spf13/cobra-cli/blob/main/README.md) to initialise a new project. There's a guide on that page to installing it, but it's likely `go install github.com/spf13/cobra-cli@latest`.
154+
155+
Then `cd` to the `cli-files` directory.
156+
157+
Make a `go-cat` directory, `cd` into it, and run `go mod init go-cat` ([documentation here](https://pkg.go.dev/cmd/go#hdr-Initialize_new_module_in_current_directory)).
158+
159+
Then run `cobra-cli init .`. [This command](https://github.com/spf13/cobra-cli/blob/main/README.md) will create your initial application code for you.
160+
161+
Take a look at all the files it has created. See how they differ or are similar to what you did in the `go-ls` example.
162+
163+
Let's try it out: `go install . && go-cat`. It will do nothing, but it's a start.
164+
165+
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.
166+
167+
Bonus task: handle the error if you pass it a directory rather than a file, like cat does.
168+
149169
[go]: https://go.dev/
150170
[cat]: https://en.m.wikipedia.org/wiki/Cat_(Unix)
151171
[ls]: https://en.m.wikipedia.org/wiki/Ls

0 commit comments

Comments
 (0)