companion

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend string

Backend identifies which runner backend is in use.

const (
	BackendDocker Backend = "docker"
	BackendNix    Backend = "nix"
	BackendLocal  Backend = "local"
)

type CompanionOpts

type CompanionOpts struct {
	// Name is a short name for this companion instance. When using the Docker
	// backend, the container name is prefixed with "codefly-" (e.g. codefly-lsp-go-123).
	Name string

	// SourceDir is the host directory to mount as /workspace (or work in).
	SourceDir string

	// Image is the Docker image (required for Docker backend, ignored otherwise).
	Image *resources.DockerImage

	// PreferredBackend forces a specific backend. If empty, auto-detect.
	PreferredBackend Backend
}

CompanionOpts configures companion runner creation.

type CompanionRunner

type CompanionRunner interface {
	// WithMount makes a host directory visible inside the companion.
	// Docker: bind mount. Nix/Local: no-op (already on host).
	WithMount(hostPath, targetPath string)

	// WithPortMapping maps a companion port to a host port.
	// Docker: Docker port mapping. Nix/Local: no-op (same network).
	WithPortMapping(ctx context.Context, hostPort, companionPort uint16)

	// WithWorkDir sets the working directory for all processes.
	WithWorkDir(dir string)

	// WithPause keeps the environment alive after Init.
	// Docker: runs `sleep infinity`. Nix/Local: no-op.
	WithPause()

	// Init starts the environment.
	Init(ctx context.Context) error

	// NewProcess creates a process inside the companion.
	NewProcess(bin string, args ...string) (base.Proc, error)

	// Shutdown stops and cleans up all resources.
	// Safe to call multiple times.
	Shutdown(ctx context.Context) error

	// RunnerEnv returns the underlying RunnerEnvironment for callers that need
	// WithEnvironmentVariables, WithBinary, etc. Same lifecycle as this companion.
	RunnerEnv() base.RunnerEnvironment

	// Backend returns which backend is in use (e.g. for path or port decisions).
	Backend() Backend
}

CompanionRunner is the golden wrapper for running companions.

It provides a unified interface that works across Docker, Nix, and local runners. Consumers (LSP, code generation, etc.) program against this interface and never care which backend is running underneath.

  • Docker: full isolation, mounts, port mapping
  • Nix: reproducible env via flake.nix, no isolation overhead
  • Local: direct host execution, zero overhead

Use NewCompanionRunner to create one -- it picks the best available backend.

func NewCompanionRunner

func NewCompanionRunner(ctx context.Context, opts CompanionOpts) (CompanionRunner, error)

NewCompanionRunner creates a companion runner using the best available backend.

Selection order (unless PreferredBackend is set):

  1. Docker -- if Docker is running and Image is provided
  2. Nix -- if flake.nix exists in SourceDir (future)
  3. Local -- always available as fallback

Jump to

Keyboard shortcuts

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