config

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 9 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"
	// 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)

BackupsDir is the single place where dumps are stored, shared by every worktree through a symlink.

func Dir

func Dir() (string, error)

Dir is where config.json and the central backups live.

func Expand

func Expand(s, database string) string

Expand substitutes the throwaway database name.

func Path

func Path() (string, error)

Path is the config file location.

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 WithLock added in v0.2.0

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

WithLock loads the registry under an exclusive lock, hands it to fn, and saves it when fn returns nil. The lock is a plain O_EXCL file next to config.json — flock would be nicer but has no Windows equivalent, and this project carries no build tags. The critical section is a read-modify-write of a small JSON file, never a docker command, so a lock older than lockStale can only be the debris of a killed process and is taken over.

Types

type Backup

type Backup struct {
	DBService  string `json:"db_service,omitempty"`  // default "db"
	DBUser     string `json:"db_user,omitempty"`     // default "postgres"
	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.

func (Backup) Validate

func (b Backup) Validate() error

Validate reports what is missing before a refresh can run.

type Config

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

Config is the whole registry.

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)

Get returns a project, listing the known ones when it is unknown.

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

Names lists the registered projects, sorted.

func (*Config) NextPortOffset

func (c *Config) NextPortOffset() int

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

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

ResolveCurrent finds the project whose dir is the given repository root.

func (*Config) Save

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

Save writes the registry, creating the directory when needed.

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

func (p Project) BackupConfig() Backup

BackupConfig returns the project's backup settings with defaults applied.

Jump to

Keyboard shortcuts

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