← Back to lesson 31: Package the edge and leave clues

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 CLI parsing thin, log useful context, and build the escript.

Run the CLI with one file path and log a count without logging file contents.

Run this first

Run it once, change one input, and compare the new result.

Original full example
# lib/cleaner/cli.ex
defmodule Cleaner.CLI do
  require Logger

  def main([path]) do
    with {:ok, text} <- File.read(path),
         {:ok, records} <- parse_lines(String.split(text, "\n")),
         {:ok, counts} <- Cleaner.run(records) do
      Logger.info("cleaned records path=#{inspect(path)} count=#{length(records)}")
      IO.inspect(counts, label: "levels")
      :ok
    else
      {:error, reason} -> fail(reason)
    end
  end

  def main(_args), do: {:error, :usage}

  defp parse_lines(lines) do
    Enum.reduce_while(lines, {:ok, []}, fn line, {:ok, records} ->
      case Cleaner.parse_line(line) do
        {:ok, record} -> {:cont, {:ok, [record | records]}}
        :skip -> {:cont, {:ok, records}}
        {:error, reason} -> {:halt, {:error, reason}}
      end
    end)
    |> then(fn
      {:ok, records} -> {:ok, Enum.reverse(records)}
      error -> error
    end)
  end

  defp fail(reason) do
    Logger.error("clean_failed reason=#{inspect(reason)}")
    {:error, reason}
  end
end

The CLI reads one file, calls the project API, logs the path and accepted record count, and prints the level summary.

Steps for the full example

  1. Read Logger.info/1: Write one useful operational message.
  2. Read main([path]): Accept exactly one file argument.
  3. Read mix escript.build: 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.

Log fields such as path and count separately from the message.

Symbols and terms
Logger.info/1
Write one useful operational message.
main([path])
Accept exactly one file argument.
mix escript.build
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
Log fields such as path and count separately from the message.
privacy boundary
Operational clues should not expose full user data or secrets.

Original full exercise

Return a usage error when the CLI receives no path.

Exercise starter
def main([]), do: ____

Return {:error, :usage}.

Show hint

Keep the error tagged.

Show original answer
Reference answer
def main([]), do: {:error, :usage}
Should a log line copy every source line?

No. Record enough context to diagnose the run without exposing full input.

Original lesson takeaways
Design trade-offs and language comparisons
Original course references