Package the edge and leave clues: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
The original English project below is a log cleaner. The current notebooks use the same file-statistics project as the Chinese route. These preserved examples show another application of the concepts; their inputs and expected outputs belong to the log cleaner.
Keep `main/1` thin, log safe context, and build an escript.
Run the CLI with one file path and log a count without copying file contents.
Run this first
Run it once, change one input, and compare the new result.
%% src/cleaner_cli.erl
-module(cleaner_cli).
-export([main/1]).
main(Args) ->
case run(Args) of
ok -> ok;
{error, _Reason} -> halt(1)
end.
run([Path]) ->
case file:read_file(Path) of
{ok, Text} ->
Lines = binary:split(Text, <<"\n">>, [global, trim]),
finish(Path, [cleaner:parse_line(Line) || Line <- Lines]);
{error, Reason} ->
fail(Reason)
end;
run(_Args) ->
fail(usage).
finish(Path, Results) ->
case [Reason || {error, Reason} <- Results] of
[] ->
Records = [Record || {ok, Record} <- Results],
{ok, Counts} = cleaner:run(Records),
logger:info("cleaned path=~ts count=~B", [Path, length(Records)]),
io:format("levels=~tp~n", [Counts]),
ok;
[Reason | _] ->
fail(Reason)
end.
fail(Reason) ->
logger:error("clean_failed reason=~tp", [Reason]),
{error, Reason}.The escript reads one file, calls cleaner, logs the accepted count, prints level totals, and exits with status 1 on failure.
Steps for the full example
- Read
logger:info/2: Write a formatted operational message. - Read
main([Path]): Accept exactly one file argument. - Read
rebar3 escriptize: Package the command after tests pass.
Original lesson notes
Run the example first. Predict one result, then change one input and run it again.
A CLI entry point parses arguments, calls the API, prints a result, and does little else.
Logger metadata keeps fields such as path and count separate from the message.
Symbols and terms
logger:info/2- Write a formatted operational message.
main([Path])- Accept exactly one file argument.
rebar3 escriptize- Package the command after tests pass.
- thin entry point
- A CLI entry point parses arguments, calls the API, prints a result, and does little else.
- structured context
- Logger metadata keeps fields such as path and count separate from the message.
- privacy boundary
- Operational clues should not expose full user data or secrets.
Original full exercise
Return a usage error when no path is supplied.
main([]) -> ____.Return {error,usage}.
Show hint
Keep the error tagged.
Show original answer
main([]) -> {error, usage}.Should logs copy every source line?
No. Record enough context to diagnose a run without exposing the full input.
Original lesson takeaways
- Keep CLI code thin.
- Log decisions and counts.
- Run tests before packaging.