stack

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: 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 stack owns everything about a worktree's docker stack: how it is named, which ports it gets, and how it goes up and down.

Index

Constants

View Source
const DefaultStride = 1

DefaultStride applies when the project does not configure one.

Variables

This section is empty.

Functions

func AllocatePort

func AllocatePort(defaultPort, index, stride, projectOffset int) (int, error)

AllocatePort rebases a default port for a worktree index.

func PortsOverride

func PortsOverride(allocations []Allocation) string

PortsOverride renders the compose file rebasing the ports a project wrote as literals, which no environment variable can reach. Compose appends `ports` when merging, so the list has to be replaced with !override, and replacing it means restating every port of that service.

This is what lets a project be used untouched: without it, a worktree stack would publish the very ports its main stack already holds.

func ProjectName

func ProjectName(repoName string, index int, branch string) string

ProjectName is the compose project wtc gives a worktree. Mirrors its composeProjectName/sanitize pair, which is the only way to address that stack's containers and volumes from the outside.

func ProjectPrefix added in v0.2.0

func ProjectPrefix(repoName string, index int) string

ProjectPrefix is what every compose project of a worktree at this index starts with, whatever its branch. It is how leftovers of a removed worktree are spotted before the index is handed to a new one.

func ReadPortValues

func ReadPortValues(worktreeDir string) (map[string]string, error)

ReadPortValues returns the allocated ports keyed by environment variable.

func ReadPorts

func ReadPorts(worktreeDir string) ([]string, error)

ReadPorts returns the port assignments recorded in the worktree .env.

func Stride

func Stride(projectDir string) int

Stride reads the project's port stride, from .wtcrc.json when present so an existing setup keeps working untouched.

func StripBlock

func StripBlock(content string) string

StripBlock removes a previously written block, so rewriting is idempotent rather than cumulative.

func WriteEnvOverrides

func WriteEnvOverrides(worktreeDir string, allocations []Allocation) error

WriteEnvOverrides rewrites the port block of the worktree's .env in place. docker compose reads that file from the project directory on its own, which is how the rebased ports reach the containers.

Types

type Allocation

type Allocation struct {
	Service   string
	Var       string
	Port      int
	Container string
}

Allocation is one published port, rebased for a given worktree.

func Allocate

func Allocate(services []compose.ServicePort, index, stride, projectOffset int) ([]Allocation, error)

Allocate rebases every published port of the compose file, whether it is written as a variable or as a literal. A literal cannot be overridden through the environment, so it is rebased through a generated compose file instead; see PortsOverride.

type Client

type Client struct {
	Runner execx.Runner
	Dir    string // project repository root
	Out    io.Writer
}

Client drives the docker stack of one project's worktrees.

func (*Client) Down

func (c *Client) Down(ctx context.Context, project, worktreeDir string) error

Down stops a worktree's stack, keeping its volumes.

func (*Client) FindByBranch

func (c *Client) FindByBranch(ctx context.Context, branch string) (Worktree, error)

FindByBranch locates a branch's worktree (its path, branch and listing position); resolving that into an index is the resolver's job, not this one's.

func (*Client) Up

func (c *Client) Up(ctx context.Context, project, worktreeDir string, files []string) error

Up brings a worktree's stack up. The port assignments live in the .env of the worktree, which docker compose reads from the project directory.

func (*Client) Worktrees

func (c *Client) Worktrees(ctx context.Context) ([]Worktree, error)

Worktrees lists the linked worktrees in `git worktree list --porcelain` order, main repository excluded. It records the position as Pos but leaves Index to the resolver: git resorts this listing alphabetically, which is exactly the instability the persisted index exists to fix.

type Worktree

type Worktree struct {
	Index  int
	Pos    int
	Path   string
	Branch string
}

Worktree is one linked worktree. Index is the stable, allocated index the ports and the compose project name are derived from; this package never fills it — the resolver in internal/index does. Pos is merely where git listed the worktree, which resorts alphabetically as worktrees come and go, so it is only good as a first-guess hint for that resolver.

Jump to

Keyboard shortcuts

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