How to add a CLI command
Wire a new cobra command into the App-injected command tree without breaking the six-command contract.
You want to extend the command surface. Careful: the CLI is specified to
have exactly six project commands (init, create, inspect,
lint, eval, release), and that surface is documented as part of the
product contract (--help additionally lists the fang-provided
completion and help helpers, which are not part of it). A new command
is a product decision first — this page is
the mechanical path once it is agreed.
Understand the structure
internal/cli/root.go defines the command tree and the App struct. Every
effectful operation — git lookups, PATH searches, TTY prompting, the
GitHub publish — is a field on App; NewApp() wires the real
environment, and tests inject fakes. A command file (init.go, create.go,
…) defines one cobra command and closes over the App.
Add the command
- Create
internal/cli/<name>.gowith a builder function taking*Appand returning a*cobra.Command, following the existing files. - Register it in
NewRootCmdWithAppinroot.go. - Use the
Apphelpers (app.workdir(),app.configPath(cmd),app.lookPath, …) for anything effectful — never reach foros.Getenvorexec.Commanddirectly. - Errors: return them; the entry point maps any returned error to exit
code
1with anERRORbanner viacharmbracelet/fang. - Zero-config: commands other than
releasemust keep working without anagore.yaml.
Test the command
internal/cli/cli_test.go builds an App with fakes (captured writers,
fake RunCommand, fake git helpers, no TTY) and drives commands end to
end. Add:
- a happy-path case,
- each error case with its exact message,
- the exit path (assert on the returned error).
mise run testKeep the docs and the landing honest
- Add the command to CLI overview and give
it its own reference page (
docs/content/docs/en/user/reference/, and the zh mirror). - The landing page's "Six commands, one lifecycle" bento and command
overview live in
docs/app/[lang]/(home)/page.tsx. - The CLI's
Longdescription inroot.gois the banner text quoted in the docs.
Pass the gate
mise run checkRelated
- The dependency-injection core: Testability and injection
- Package responsibilities: Packages reference