Skip to content

Commit d418ebc

Browse files
committed
shift to ls and cat
1 parent 998e936 commit d418ebc

File tree

5 files changed

+58
-67
lines changed

5 files changed

+58
-67
lines changed

cli-files/README.md

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# CLI & Files
22

3-
Become familiar with go and the toolchain by building a JSON-file reading CLI tool: `get-data ".flowers[0].name" garden.json`
3+
In this project you're going to get familiar with the [Go programming language][go] by rebuilding two very common tools that programmers and computer administrators use all the time: [cat][cat] and [ls][ls].
44

55
Timebox: 4 days
66

@@ -11,56 +11,57 @@ Objectives:
1111
- Understand what a process is & the basics of process - lifecycle
1212
- Accept arguments on the CLI
1313
- Open, read (and close) files from CLI arguments
14-
- Learn about JSON and parsing
15-
- Read strings from arguments
16-
- Extract data from a JSON file and print it to the CLI
17-
- Add support for YAML
14+
- Reading directories for files
1815

1916
## Instructions
2017

21-
You're going to build a command-line application that reads data from files.
22-
23-
Let's say you have a `garden.json` file like this, containing data about what's growing in our garden:
24-
25-
```json
26-
{
27-
"flowers": [
28-
{
29-
"name": "Great Maiden's Blush",
30-
"genus": "Rosa",
31-
"species": "Rosa × alba",
32-
"color": "white",
33-
"height": 2.2
34-
},
35-
{
36-
"name": "Bright Gem",
37-
"genus": "Tulipa",
38-
"species": "Tulipa linifolia",
39-
"color": "Yellow/orange",
40-
"height": 0.17
41-
},
42-
{
43-
"name": "Elizabeth",
44-
"genus": "Magnolia",
45-
"species": "M. acuminata × M. denudata",
46-
"color": "Pale yellow",
47-
"height": 9.6
48-
}
49-
]
50-
}
18+
You're going to build a command-line application that reads data the computers file system.
19+
20+
Let's say you have a few files containing poems in a directory:
21+
22+
- `dew.txt`
23+
- `for_you.txt`
24+
- `rain.txt`
25+
26+
The `ls` tool will list the files in that directory:
27+
28+
```
29+
> ls
30+
dew.txt
31+
for_you.txt
32+
rain.txt
5133
```
5234

53-
The tool you will build will extra data from this file. Here are some examples:
35+
While the `cat` command will output the contents of a file to you:
36+
37+
```
38+
> cat dew.txt
39+
“A World of Dew” by Kobayashi Issa
40+
41+
A world of dew,
42+
And within every dewdrop
43+
A world of struggle.
44+
```
45+
46+
As you can see, `cat` takes an argument, namely the file to be written out. You'll learn how to do this too.
47+
48+
You're going to rebuild these two tools in Go:
5449

5550
```bash
56-
> get-data ".flowers[0].name" garden.json
57-
"Great Maiden's Blush"
51+
> go-ls
52+
dew.txt
53+
for_you.txt
54+
rain.txt
5855

59-
> get-data ".flowers[1].height" garden.json
60-
2.2
56+
> go-cat dew.txt
57+
“A World of Dew” by Kobayashi Issa
6158

62-
> get-data ".flowers[2]" garden.json
63-
{"name": "Elizabeth", "genus": "Magnolia", "species": "M. acuminata × M. denudata", "color": "Pale yellow", "height": 9.6 }
59+
A world of dew,
60+
And within every dewdrop
61+
A world of struggle.
6462
```
6563

64+
[go]: https://go.dev/
65+
[cat]: https://en.m.wikipedia.org/wiki/Cat_(Unix)
66+
[ls]: https://en.m.wikipedia.org/wiki/Ls
6667
[cobra]: https://github.com/spf13/cobra#overview

cli-files/assets/dew.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
“A World of Dew” by Kobayashi Issa
2+
3+
A world of dew,
4+
And within every dewdrop
5+
A world of struggle.

cli-files/assets/for_you.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Sonia Sanchez “Haiku [for you]”
2+
3+
Love between us is
4+
speech and breath. Loving you is
5+
a long river running.

cli-files/assets/garden.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

cli-files/assets/rain.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
“The Taste of Rain” by Jack Kerouac
2+
3+
The taste
4+
Of rain
5+
—Why kneel?

0 commit comments

Comments
 (0)