Documentation
¶
Overview ¶
Package config reads and writes the project registry used by wtm.
Index ¶
- Constants
- func BackupsDir() (string, error)
- func Dir() (string, error)
- func Expand(s, database string) string
- func Path() (string, error)
- func ValidateIdentifier(kind, name string) error
- func ValidateRelativePath(kind, path string) error
- func WithLock(path string, fn func(*Config) error) error
- type Backup
- type Config
- func (c *Config) BaseBranchFor(p Project) string
- func (c *Config) Get(name string) (Project, error)
- func (c *Config) Has(name string) bool
- func (c *Config) Names() []string
- func (c *Config) NextPortOffset() int
- func (c *Config) ResolveCurrent(repoRoot string) (string, Project, error)
- func (c *Config) Save(path string) error
- type FieldChange
- type Project
- type ProjectUpdate
Constants ¶
const ( EnvConfigDir = "WTM_CONFIG_DIR" EnvBackupsDir = "WTM_BACKUPS_DIR" )
Environment overrides, mostly so tests never touch the real home directory.
const ( DefaultDBService = "db" DefaultDBUser = "postgres" DefaultDBEngine = "postgres" // DefaultDBPath is Django's convention, the most common sqlite layout. DefaultDBPath = "db.sqlite3" // DefaultMigrationsPath covers Django's app/migrations, Prisma's // prisma/migrations and MikroORM's src/database/migrations. DefaultMigrationsPath = "*migrations/*" // DatabasePlaceholder is replaced by the throwaway database name in // migrate_command and in every env value. DatabasePlaceholder = "{{database}}" )
Defaults for the most common docker compose layout.
const FallbackBaseBranch = "develop"
FallbackBaseBranch applies when neither the project nor the config names one.
Variables ¶
This section is empty.
Functions ¶
func BackupsDir ¶
BackupsDir is the single place where dumps are stored, shared by every worktree through a symlink.
func ValidateIdentifier ¶ added in v0.1.1
ValidateIdentifier rejects a name that could not be a compose identifier. A quote or a `$(...)` in a project name would otherwise land in the restore script running inside the database container, and a newline in a service name would inject arbitrary keys into the generated compose file.
func ValidateRelativePath ¶ added in v0.4.0
ValidateRelativePath rejects a path that could escape the directory it is joined under: db_path lands inside each worktree and inside the app container, so it has to stay a plain relative path.
func WithLock ¶ added in v0.2.0
WithLock loads the registry under an exclusive lock, hands it to fn, and saves it when fn returns nil. The lock is an OS file lock (flock on Unix, LockFileEx on Windows) next to config.json: the kernel releases it when the process dies, so a killed wtm can never leave the registry locked. The lock file itself staying on disk afterwards is normal and carries no state.
Types ¶
type Backup ¶
type Backup struct {
DBService string `json:"db_service,omitempty"` // default "db"
DBUser string `json:"db_user,omitempty"` // default "postgres"
// DBEngine names the database engine (see internal/dbengine). Empty means
// postgres, which is what every project registered before this field
// existed means.
DBEngine string `json:"db_engine,omitempty"`
// DBPath is where a file-based engine's database lives, relative to the
// project root. The dump is copied there in each worktree. Ignored by
// server engines.
DBPath string `json:"db_path,omitempty"`
AppService string `json:"app_service,omitempty"` // required
// DepsCommand runs before the migration in the disposable container, for
// projects that install their dependencies at container startup rather
// than baking them into the image (a fresh container has none).
DepsCommand string `json:"deps_command,omitempty"`
// MigrateCommand builds the schema, e.g. "python manage.py migrate",
// "npx prisma migrate deploy", "mikro-orm migration:up". Required.
MigrateCommand string `json:"migrate_command,omitempty"`
// MigrationsPath is the git pathspec of the migration files, used to tell
// whether a dump has fallen behind. The default matches the layout of
// Django, Prisma and MikroORM alike.
MigrationsPath string `json:"migrations_path,omitempty"`
// Env tells the app which database to target: {"DB_NAME": "{{database}}"}
// or {"DATABASE_URL": "postgresql://user:pass@db:5432/{{database}}"}.
Env map[string]string `json:"env,omitempty"`
}
Backup describes how to rebuild a schema. Everything here differs from one project to the next: a survey of four real projects found four different migration commands, three app service names, two database service names, two database users and two ways of telling the app which database to use.
type Config ¶
type Config struct {
DefaultBaseBranch string `json:"default_base_branch,omitempty"`
Projects map[string]Project `json:"projects"`
}
Config is the whole registry.
func (*Config) BaseBranchFor ¶
BaseBranchFor applies the project > config > fallback precedence.
func (*Config) Has ¶
Has reports whether a name is registered, which is how the CLI tells a project argument from a branch argument.
func (*Config) NextPortOffset ¶
NextPortOffset returns a free offset for a new project, in steps of 1000. The first project keeps 0 so its existing worktrees are untouched.
func (*Config) ResolveCurrent ¶
ResolveCurrent finds the project whose dir is the given repository root.
type FieldChange ¶ added in v0.3.0
FieldChange is one edited field, as the command reports it.
type Project ¶
type Project struct {
Dir string `json:"dir"`
BaseBranch string `json:"base_branch,omitempty"`
Dump bool `json:"dump,omitempty"`
// GitContainer creates the .git-container symlinks. Opt-in: it only helps
// projects whose compose bind-mounts the git directory into a container
// (macOS/VirtioFS cannot mount the pointer file a linked worktree uses).
GitContainer bool `json:"git_container,omitempty"`
// PortOffset shifts every rebased port of this project. The allocation
// formula only knows the default port, the worktree index and the stride,
// so without it two projects whose database listens on 5432 would fight
// over the same host port. Zero for the first project, which keeps the
// ports it already had.
PortOffset int `json:"port_offset,omitempty"`
// WorktreeIndices pins each branch to the index its stack was created
// with. The index feeds the port formula and the compose project name,
// so deriving it from git's listing order (which resorts alphabetically)
// renumbers running stacks; recording it here is what keeps it stable.
WorktreeIndices map[string]int `json:"worktree_indices,omitempty"`
Backup *Backup `json:"backup,omitempty"`
}
Project is one registered repository.
func (Project) BackupConfig ¶
BackupConfig returns the project's backup settings with defaults applied.
type ProjectUpdate ¶ added in v0.3.0
type ProjectUpdate struct {
Dir *string
BaseBranch *string
Dump *bool
GitContainer *bool
DBService *string
DBUser *string
DBEngine *string
DBPath *string
AppService *string
DepsCommand *string
MigrateCommand *string
// Env replaces the whole set when given: a map merged field by field
// would leave no way to drop a variable.
Env map[string]string
}
ProjectUpdate carries the fields an edit actually names. A nil pointer means "leave it alone": registering a project and editing one are not the same operation, and an edit must not reset what it says nothing about, the port offset and the recorded worktree indices first of all.
func (ProjectUpdate) Apply ¶ added in v0.3.0
func (u ProjectUpdate) Apply(p Project) (Project, []FieldChange)
Apply returns the edited project along with what actually changed. A field set to the value it already had is not a change and is not reported.
func (ProjectUpdate) IsEmpty ¶ added in v0.3.0
func (u ProjectUpdate) IsEmpty() bool
IsEmpty reports an update that names nothing, which is how a command tells "change this one field" from "walk me through the settings".