gobake operates on a simple principle: Your build system should be as robust as your application.
Instead of relying on fragile shell scripts or complex Makefiles, gobake uses:
- Engine: The core Go library that manages tasks.
- Context: A helper object passed to every task, providing access to the shell, logging, and metadata.
- Recipe: The
Recipe.gofile that ties it all together.
gobake init: Initialize a new project in the current directory. This command scaffoldsrecipe.pimlandRecipe.go, runsgo mod initif nogo.modis found, runsgo mod tidy, and adds thegithub.com/fezcode/gobakelibrary to your dependencies.gobake version: Show the current version of gobake.gobake help: Show the list of available commands and project tasks.gobake template <git-url>: Clone a repository and initialize it.
gobake add-tool <url>: Adds a tool to the(tools)list inrecipe.piml.- Example:
gobake add-tool github.com/vektra/mockery/v2@latest
- Example:
gobake remove-tool <url>: Removes a tool fromrecipe.piml.gobake add-dep <url>: Adds a library dependency togo.mod(wrapper forgo get).- Example:
gobake add-dep github.com/gin-gonic/gin
- Example:
gobake remove-dep <url>: Removes a library dependency.
gobake bump [patch|minor|major]: Increments the project version inrecipe.piml.
The recipe.piml file is the single source of truth for your project.
(name) my-project
(version) 1.2.3
(authors)
> Alice <alice@example.com>
> Bob <bob@example.com>
(tools)
> github.com/swaggo/swag/cmd/swag@latest
name: The name of your project.version: Semantic version (e.g.,1.0.0).description: A short summary.authors: List of contributors.license: Project license (e.g.,MIT,Apache-2.0).repository: Git repository URL.keywords: Search tags.tools: List of External CLI tools required for development (e.g., linters, generators).
It is important to distinguish between Project Dependencies and Build Tools.
-
Project Dependencies (
go.mod):- Libraries your code imports (e.g.,
gin,cobra). - Managed by standard Go commands:
go get,go mod tidy. - Do not list these in
recipe.piml.
- Libraries your code imports (e.g.,
-
Build Tools (
recipe.piml):- Executables used during the build process (e.g.,
golangci-lint,stringer,swag). - These are installed via
go installinto your system or path. gobakeautomates installing these usingctx.InstallTools().
- Executables used during the build process (e.g.,
You can access these fields in your Recipe.go via bake.Info.
fmt.Println("Project:", bake.Info.Name)
fmt.Println("Version:", bake.Info.Version)Tasks are defined using bake.Task(name, description, function).
You can define tasks that depend on other tasks using bake.TaskWithDeps. Dependencies are executed sequentially before the main task.
bake.Task("test", "Run tests", func(ctx *gobake.Context) error { ... })
bake.TaskWithDeps("build", "Build app", []string{"test"}, func(ctx *gobake.Context) error { ... })gobake automatically detects circular dependencies and ensures each task runs only once per execution.
The following names are reserved for gobake CLI commands and cannot be used as task names:
initversionhelpbumptemplateadd-toolremove-tooladd-depremove-dep
The ctx object is powerful. Here are its key methods:
ctx.Run(cmd, args...): Executes a shell command. It streams stdout/stderr to your terminal.ctx.Log(format, args...): Prints a formatted log message with the[gobake]prefix.ctx.InstallTools(): Iterates through thetoolslist inrecipe.pimland runsgo installfor each.ctx.BakeBinary(os, arch, output, flags...): A helper for cross-compilation. It setsGOOSandGOARCHautomatically.ctx.Mkdir(path): Creates a directory and all its parents.ctx.Remove(path): Removes a file or directory recursively.ctx.Copy(src, dst): Copies a file.ctx.SetEnv(key, value): Sets an environment variable only for the current task and commands executed within it viactx.Runorctx.BakeBinary.
- Chaining Tasks: You can run one task from another.
bake.Task("all", "Builds everything", func(ctx *gobake.Context) error { if err := ctx.Run("gobake", "test"); err != nil { return err } return ctx.Run("gobake", "build") })
- CI/CD: Since
gobakeis just Go, it runs perfectly in GitHub Actions or GitLab CI. Just ensure Go is installed. - Injecting Version: You can inject the version from
recipe.pimlinto your binary using-ldflags.bake.Task("build", "Build app", func(ctx *gobake.Context) error { ldflags := fmt.Sprintf("-X main.Version=%s", bake.Info.Version) return ctx.BakeBinary("linux", "amd64", "bin/app", "-ldflags", ldflags) })