Architecture
This page covers how the code is organised, and the conventions to follow when changing it. The command bus and errors pages go into those two parts in more detail.
How a command runs
Every command that does something goes through the same path:
cobra handler (cmd/orca) reads flags, builds a message, dispatches it
└─ bus.Dispatch internal/bus
├─ LoggingMiddleware logs the dispatch at debug level
├─ ExecutionContextMiddleware resolves the workspace/project, if the message has a target
├─ Before hooks
├─ handler a method on a feature package's Manager
└─ After hooks
└─ reportError shows any error to the user, once (cmd/orca/exit.go)
cmd/orca is the only package that knows about cobra, flags or how services are wired together. Everything under internal/ works with messages, context.Context and its own small interfaces.
Packages
Each package is one of three kinds, and named after what it provides, not its layer. There's no common, util, controller or repository package.
Domain
| Package | Contents |
|---|---|
internal/workspace |
Everything about workspaces: the model (Workspace, Project, Extension...), loading their config (Repository), working out which workspace and project a command runs against (Resolver, ExecutionContext, ExecutionContextMiddleware), and the ws commands (Manager). |
The other packages import workspace for the model. It never imports them, so it only depends on them through its own interfaces (e.g. the git calls it needs to clone a project).
Features
Feature packages hold the commands' handlers, and mirror the CLI's command groups.
| Package | Commands |
|---|---|
internal/stack |
up, down, exec, logs, debug (restart dispatches down then up) |
internal/extension |
ext and hooks run, running every type of extension (commands in a service or on the host, syncFile and symlink), and the trust store and prompt |
internal/hosts |
hosts, with the /etc/hosts manager it uses |
internal/git |
g. It's also the wrapper around the git CLI that workspace uses to clone projects. |
internal/tls |
tls gen |
internal/sys |
sys check, sys self-update |
internal/workspace |
ws (see above) |
Adapters and infrastructure
These talk to things outside of orca, or are plumbing shared by the rest.
| Package | Contents |
|---|---|
internal/bus |
The command bus, its middleware and hooks. It has no dependencies on the rest of orca. |
internal/usererr |
Attaching messages for the user to errors. |
internal/process |
Executor, which runs external commands. |
internal/docker |
The docker compose wrapper, and the compose overlays. |
internal/github |
The GitHub API client. |
internal/config |
The user config, ~/.orca/orca.yaml. |
internal/tui |
Terminal output and prompts. |
internal/logging |
The slog handler. |
internal/plugins |
Orca's side of the plugin system, see how plugins work. |
internal/dag |
The dependency graph used to order projects. |
pkg/ is only for public API, and only pkg/plugin (the plugin SDK) belongs there. Anything that's only used by orca goes in internal/.
Adding a command
For example, a ps command that shows the running services:
-
Define the message in the feature package it belongs to, next to its handler. Name it
...Command, or...Queryif it only reads and shows something. Give it aMessageName(), which is the command's path joined with dots: -
Write the handler as a method on the package's
Manager, taking(ctx context.Context, msg). If it runs against a workspace or project, read theExecutionContextfirst: -
Register it in
cmd/orca/services.go. Add the handler toGetBus():If the handler reads the
ExecutionContext, also add a target toGetExecutionContextMiddleware(): -
Dispatch it from the cobra handler, wrapped in
handleErrors. The handler only reads flags and dispatches a message literal: -
Complete its arguments, if their values come from config or the environment, such as service or branch names. See Shell completion below.
-wand-palready complete, asaddWorkspaceOptionandaddProjectOptionregister them.
Two tests in cmd/orca/services_test.go catch wiring mistakes that would otherwise only fail at runtime:
Test_AllDispatchedMessagesHaveHandlerschecks every dispatched message has a handler.Test_ExecutionContextHandlersHaveTargetschecks every handler that reads theExecutionContexthas a target.
Extension types
An extension's properties depend on its type. In internal/workspace/extension.go, each type is its own spec (CommandSpec, SyncFileSpec, SymlinkSpec) behind the sealed ExtensionSpec interface, so code can only read the fields of the type it's handling. Switch on the concrete type, and panic in the default case, as the set is closed.
The config is decoded in two passes. model.Extension reads the shared fields (name, type, description) and keeps the whole YAML node. buildExtensions then looks the type up in extensionKinds, rejects any key that's missing from the yaml tags of the type's model struct, checks the fields tagged required:"true", and decodes the node into that struct. Every problem is collected, rather than stopping at the first.
To add a type:
- Add its model struct to
internal/workspace/internal/model/project.go, with ayamltag on every field. - Add its spec, and an entry in
extensionKinds, toextension.go. - Add a case wherever extensions are switched on.
- Add a section for it to
docs/configuration/extensions.md.Test_ExtensionTypesAreDocumentedfails until that section lists every property.
Shell completion
Completion runs orca as orca __complete <args>, on every Tab, so it must be fast and silent. In that mode (completing in cmd/orca/completion.go), plugins aren't started, logging is discarded, and checkErr prints nothing.
The values offered come from ...ChoicesQuery messages, handled in the feature package that owns them, and returning []workspace.Choice. They go through the bus like any other message, so the ExecutionContextMiddleware works out the workspace and project as it would for the command, from the working directory and any -w/-p typed so far. Interactive prompts can use the same queries, so the two never disagree.
type ServiceChoicesQuery struct {
bus.Returns[[]workspace.Choice]
Workspace string
Project string
}
func (ServiceChoicesQuery) MessageName() string {
return "choices.services"
}
Register the handler with b.Handle(lazyResult(...)), and give it a target if it reads the ExecutionContext. In cmd/orca/completion.go, add a completion function that dispatches the query as a literal and passes the result to toCompletions, which filters by what's typed, and offers nothing on an error. Use it as a command's ValidArgsFunction (with firstArgOnly if it only completes the first argument), or a flag's with RegisterFlagCompletionFunc.
File names are never offered: noFileCompletions turns them off for every builtin command that doesn't complete its own arguments.
Dependencies
- Packages declare the interfaces they need, privately, next to the code that uses them (e.g.
stackdeclarescompose). Keep them to the methods that are actually called, so they're easy to fake. - Constructors take those interfaces, and return the concrete type:
NewManager(compose compose) *Manager. - Services are wired together in
cmd/orca/services.go, by getters that build each one the first time it's needed. Handlers are registered withlazy, so dispatching one command only builds the services it uses. - Don't depend on the tui for errors. Return them instead (see errors). A service only needs the tui to show output.
- The
ExecutionContextis read from thecontext.Contextonce, in the handler, then passed explicitly to any helpers, by convention asexecCtx. Don't add other context values for workspace or project data.
Conventions
- Files are named after what's in them. Name a package's main file after the package (
git/git.go), nevermain.go, which is forpackage main. Use one file per command handler (stack/up.go,stack/down.go), and put the package's errors inerrors.go. - Receivers are short, and the same for every method on a type (
m *Manager,c *Compose). Neverselforthis. - Feature services are always called
Manager(stack.Manager,git.Manager,tls.Manager...), built withNewManager. Their getters incmd/orca/services.goare named after the package (GetStack,GetTLS). Avoid names that repeat the package, likeworkspace.WorkspaceRepository. - Options structs that aren't messages are named
...Opts(git.PullBranchOpts), not...DTO. - Errors follow the conventions in errors: return them, don't print them; use sentinels by default; check them with
errors.Is/errors.AsType. - Exiting: never call
os.Exit,cobra.CheckErrorlog.Fataldirectly. Return an error, or useexit/checkErrfromcmd/orca/exit.go, so plugin processes are always stopped.Test_NoDirectExitsenforces this.
Testing
- Tests use testify, and are mostly table-driven with
t.Run. - Use
afero.NewMemMapFs()for anything that touches the filesystem. - Fake dependencies with small hand-written stubs, or mockery mocks for larger interfaces. Mocks are configured in
.mockery.yaml, generated intotests/mocks/<package>withmake gen-mocks, and committed. -
Handlers that read the
ExecutionContextare tested by putting one in the context, rather than mocking a resolver: -
Check errors with
assert.ErrorIs/assert.ErrorAs, and their text withassert.EqualError, rather than comparing error values.