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 RecordWorktreePath(configPath, project, branch, path string) error
- func RekeyWorktree(configPath, project, from, to string) error
- func SamePath(a, b string) bool
- func ValidateCopyPattern(pattern 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
- 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 ¶
func RecordWorktreePath ¶ added in v0.15.0
RecordWorktreePath remembers where a branch's worktree stands, and forgets it when path is empty. A project gone from the registry is not an error: the command carries on either way, and this only feeds a diagnosis.
func RekeyWorktree ¶ added in v0.26.0
RekeyWorktree moves a worktree's index and recorded path from one branch to another in a single locked write: the index, and so the ports, stay the same.
func SamePath ¶ added in v0.17.1
SamePath compares directories, tolerating trailing slashes and the symlinked temp directories macOS hands out, which git and the shell spell differently.
func ValidateCopyPattern ¶ added in v0.21.0
ValidateCopyPattern is ValidateRelativePath for a glob, whose syntax filepath.Glob would otherwise only reject at the first create.
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 lands in the restore script the database runs, a newline in a service name injects keys into a 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.
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"`
// StartDependencies brings up the application service's depends_on for the
// refresh, for a migrate_command that also seeds and needs a cache or object
// storage. Off by default; the refresh takes down whatever it started.
StartDependencies bool `json:"start_dependencies,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: four real projects gave four migration commands, three app service names 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"`
}
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 steps by 1000, and the first project keeps 0 so its existing worktrees are untouched.
type FieldChange ¶ added in v0.3.0
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: the formula knows only the default
// port, the index and the stride, so without it two projects whose database
// listens on 5432 clash. Zero for the first, which keeps its ports.
PortOffset int `json:"port_offset,omitempty"`
// WorktreeIndices pins each branch to the index its stack was created with.
// Deriving it from git's listing order, which resorts alphabetically, would
// renumber running stacks: the index feeds ports and the compose project name.
WorktreeIndices map[string]int `json:"worktree_indices,omitempty"`
// Profiles name subsets of the compose services a stack may start, e.g.
// {"light": ["db", "backend"]}. Unlike compose's own profiles, a choice the
// team made once in the compose file, these are a worktree's: hence here.
Profiles map[string][]string `json:"profiles,omitempty"`
// ProfileDescriptions say when to pick a profile ("the Celery tasks"),
// which its services alone do not; `wtm project profiles` shows them.
ProfileDescriptions map[string]string `json:"profile_descriptions,omitempty"`
// WorktreePaths is where each of those branches stood when its index was
// recorded. Switching branches inside a worktree takes its old name out of
// `git worktree list`, which reads as a worktree that vanished; the path is
// what tells that apart from one that really did.
WorktreePaths map[string]string `json:"worktree_paths,omitempty"`
// PostCreate runs in the application container once a new worktree's stack
// answers, e.g. "python manage.py seed_data". It is where a seed belongs
// when it has to be replayed per worktree; one that can live in the dump
// goes in migrate_command instead and costs nothing per create.
PostCreate string `json:"post_create,omitempty"`
// ReadyTimeout is how long a new worktree waits for a service before
// post_create runs, ReadyInterval how often it asks, both as durations
// ("2m", "10s"). Empty takes the built-in ones: a database is not an app.
ReadyTimeout string `json:"ready_timeout,omitempty"`
ReadyInterval string `json:"ready_interval,omitempty"`
// Copy lists the files git leaves out that a worktree still needs, as
// patterns relative to the project root (".env.local", "config/*.local.json").
// The *.env files are copied without being named here.
Copy []string `json:"copy,omitempty"`
Backup *Backup `json:"backup,omitempty"`
}
func (Project) BackupConfig ¶
func (Project) ProfileNames ¶ added in v0.15.0
ProfileNames lists what a project offers, sorted so the completion and the error above agree from one call to the next.
func (Project) ServicesFor ¶ added in v0.15.0
ServicesFor resolves a profile to the services a stack starts, nil for all. The list is a floor: `compose up -d db backend` also starts backend's depends_on, so leaving a service out keeps it down only if nothing needs it.
type ProjectUpdate ¶ added in v0.3.0
type ProjectUpdate struct {
Dir *string
BaseBranch *string
Dump *bool
GitContainer *bool
PostCreate *string
ReadyTimeout *string
ReadyInterval *string
// Profiles replaces the whole set when given, for the same reason Env does:
// merging name by name would leave no way to drop a profile.
Profiles map[string][]string
ProfileDescriptions map[string]string
// Copy replaces the whole list when given, an empty one included.
Copy []string
DBService *string
DBUser *string
DBEngine *string
DBPath *string
AppService *string
DepsCommand *string
MigrateCommand *string
StartDependencies *bool
MigrationsPath *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": 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".