Command-line tools with Go
You write command-line tools with subcommands, config files and proper exit codes that ship as a single binary.
Level 201 · 18 lessons · 15 practice labs
Lessons
Tool skeleton
- What makes a good command-line tool
- Subcommands with flag
- Commands and flags with cobra
- stdin, stdout, stderr and exit codes
- Short task: a skeleton with help text and a version flag
Files and configuration
- Finding files with filepath.WalkDir and patterns
- YAML and TOML config files
- Environment variables and setting precedence
- Embedding files into the binary with embed
- Readable tables and coloured output
- Short task: merge file, environment and flag settings
Working with the system and shipping
- Running commands safely with os/exec
- Catching signals and exiting cleanly
- Progress and cancellation for long jobs
- Cross-compiling: GOOS and GOARCH
- Embedding version info: ldflags and debug.BuildInfo
- Short task: a release bundle with checksums for three platforms
Capstone
- Capstone: a tool that scans folders and reports duplicate and large files
Practice labs
- A skeleton with help and version flags: Write a tool skeleton with the flag package that offers help text, a version flag and usage output on an unknown flag.
- Adding subcommands with FlagSet: Write add, list and delete subcommands with separate FlagSets, each with its own help and flags.
- Migrating a flag-based tool to cobra: Migrate a tool with subcommands to cobra, add persistent flags and argument validation and show by test that old command lines give the same output.
- Reading input from a file or a pipe: Write a line counter that reads standard input when no file name or a dash is given.
- Tying errors to stderr and exit codes: Set up a layout that writes results to stdout and diagnostics to stderr and returns distinct exit codes for usage, input and internal errors.
- Printing a coloured table depending on the terminal: Print an aligned table with tabwriter, turn off colour codes when output is not a terminal or NO_COLOR is set and cover both cases with tests.
- Finding files that match a pattern: List files matching a pattern with filepath.WalkDir and Match while skipping hidden directories.
- Reading YAML and TOML settings into one struct: Decode a YAML or TOML file into the same settings struct depending on extension and report an unknown key as an error.
- Merging file, environment and flag settings: Merge settings in default, file, environment variable and flag order and add a dump command showing which source each value came from.
- Running a command without a shell and capturing output: Run a program with exec.Command giving arguments separately, collect stdout and stderr apart and return the exit code.
- Cleaning temp files and exiting on a signal: Catch the interrupt signal with signal.NotifyContext, stop the running job, delete the temp directory and exit with the right code.
- Progress and cancellation in a long copy: Add percentage progress and context cancellation to a command copying a large file so no partial target remains on cancel and the child process is killed.
- Embedding default settings in the binary: Embed the default settings file and help text in the binary with embed and test that it works without the file system.
- Embedding version info at build time: Embed the version with ldflags and write a version command that reads the commit id and build time from debug.BuildInfo when it is not given.
- Producing a release bundle for three platforms: Write a script that cross-compiles to three platforms with GOOS and GOARCH, archives them and produces a SHA-256 checksum file, and show the build is reproducible.
About this course
You write command-line tools with subcommands, config files and proper exit codes that ship as a single binary.
What you will learn
- Tool skeleton
- Files and configuration
- Working with the system and shipping
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.