Skip to content

docs: add example to count ERROR lines in a log file (#236)#243

Open
shreyaabaranwal wants to merge 2 commits into
bitfield:masterfrom
shreyaabaranwal:add-examples
Open

docs: add example to count ERROR lines in a log file (#236)#243
shreyaabaranwal wants to merge 2 commits into
bitfield:masterfrom
shreyaabaranwal:add-examples

Conversation

@shreyaabaranwal

Copy link
Copy Markdown

Hi! Following up on #236 — here's a first example to get the examples/ directory started.

I went with something straight out of everyday ops life: counting ERROR lines in a log file. Anyone who's stared at logs during an incident knows this one — it's basically grep ERROR app.log | wc -l, but here it's done with script:

script.File(logFile).Match("ERROR").CountLines()

I really like how cleanly the pipe-style API reads here — it maps almost one-to-one to the shell command, which I think makes it a nice, relatable first example for newcomers.

I've included a small sample examples/app.log so it runs out of the box, and verified everything locally with go run, gofmt -l, and go vet (all clean).

Happy to add a couple more examples in the same spirit if you'd like — just wanted to start with one and get your feedback on the format first.

Closes #236

Signed-off-by: shreyaabaranwal <shreyabaranwal229@gmail.com>
Signed-off-by: shreyaabaranwal <shreyabaranwal229@gmail.com>
@bitfield

Copy link
Copy Markdown
Owner

Great contribution, @shreyaabaranwal, thanks a lot!

It might be a nice idea to tie these examples in more directly with the ones in the README, mightn't it? For example, we start with:

contents, err := script.File("test.txt").String()

and work our way through things like counting error lines (to your point). What if we made each of these examples a standalone program, and then we could link from the README to the full listing? If you feel like adding more complicated examples, by all means do, but we should probably start with these.

For examples that need to read a specific file, we should probably come up with a general solution to:

	// Check if examples/app.log exists, otherwise fallback to app.log.

My instinct is that people would naturally run go run examples/count_errors.go, for instance, which would mean the path we're opening should be examples/app.log. We want to minimise any code in the example that's not directly about script, purely because that makes it easier for people to see what the example is actually about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add examples/ in the repo

2 participants