config

package
v0.4.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package config reads and writes the project registry used by wtm.

Index

Constants

View Source
const (
	EnvConfigDir  = "WTM_CONFIG_DIR"
	EnvBackupsDir = "WTM_BACKUPS_DIR"
)

Environment overrides, mostly so tests never touch the real home directory.

View Source
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.

View Source
const FallbackBaseBranch = "develop"

FallbackBaseBranch applies when neither the project nor the config names one.

Variables

This section is empty.

Functions

func BackupsDir

func BackupsDir() (string, error)

func Dir

func Dir() (string, error)

func Expand

func Expand(s, database string) string

func Path

func Path() (string, error)

func ValidateIdentifier added in v0.1.1

func ValidateIdentifier(kind, name string) error

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

func ValidateRelativePath(kind, path string) error

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

func WithLock(path string, fn func(*Config) error) error

WithLock saves the registry when fn returns nil. The lock is an OS file lock, so the kernel releases it when the process dies and a killed wtm can never leave the registry locked; the lock file staying on disk 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: four real projects gave four migration commands, three app service names and two ways of telling the app which database to use.

func (Backup) Validate

func (b Backup) Validate() error

type Config

type Config struct {
	DefaultBaseBranch string             `json:"default_base_branch,omitempty"`
	Projects          map[string]Project `json:"projects"`
}

func Load

func Load(path string) (*Config, error)

Load reads the registry. A missing file is an empty registry, not an error.

func (*Config) BaseBranchFor

func (c *Config) BaseBranchFor(p Project) string

BaseBranchFor applies the project > config > fallback precedence.

func (*Config) Get

func (c *Config) Get(name string) (Project, error)

func (*Config) Has

func (c *Config) Has(name string) bool

Has reports whether a name is registered, which is how the CLI tells a project argument from a branch argument.

func (*Config) Names

func (c *Config) Names() []string

func (*Config) NextPortOffset

func (c *Config) NextPortOffset() int

NextPortOffset steps by 1000, and the first project keeps 0 so its existing worktrees are untouched.

func (*Config) ResolveCurrent

func (c *Config) ResolveCurrent(repoRoot string) (string, Project, error)

func (*Config) Save

func (c *Config) Save(path string) error

type FieldChange added in v0.3.0

type FieldChange struct {
	Field string
	From  string
	To    string
}

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 only knows the default
	// port, the index and the stride, so without it two projects whose database
	// listens on 5432 fight over the same host port. Zero for the first one,
	// 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.
	// 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"`
	Backup          *Backup        `json:"backup,omitempty"`
}

func (Project) BackupConfig

func (p Project) BackupConfig() Backup

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": 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".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL