A template for building Go CLIs with Cobra using a test-first workflow.
- CLI framework:
spf13/cobra - Test runner:
onsi/ginkgo - Assertions:
stretchr/testify/assert - Utility:
samber/lo - Validation:
kaptinlin/gozod - Mode Management:
louiss0/g-tools/mode
git clone https://github.com/louiss0/cobra-cli-template .or
gh repo create <project-name> --template louiss0/cobra-cli-template --public --cloneginkgo run ./...ginkgo watch ./...go run ./scripts/rename-projectThe script infers the new project name from the folder you run it in.
go run ./scripts/rename-project -project-name my-cli-module-path is optional. If omitted, it infers your current module from
go.mod and swaps only the last path segment to <project-name>.
- Add a file in
cmd/. - Create a
New<CommandName>() *cobra.Commandconstructor. - Add flags in that constructor.
- Register the command from
NewRootCmd(). - Add tests in the same package.
- Keep command behavior in
RunEand return explicit errors. - This template includes
custom_flagsforcmd.Flags().Var(...)use cases. custom_errorscentralizes reusable error values.