ops

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 17 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 ops is devrun's operations layer: resolve the active config, talk to the daemon, read and write service and target definitions. Every function returns data and errors and prints nothing, so the CLI and the MCP server share one implementation and render it their own way — a table for a person, JSON for an agent.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoDaemon = errors.New("no daemon running")

ErrNoDaemon means the daemon is not running. Stop operations return an error matching it (errors.Is) rather than starting a daemon just to find nothing running.

View Source
var ErrNoLogs = errors.New("no logs")

ErrNoLogs means the service has no log file — it has never been started. Logs returns an error matching it (errors.Is).

Functions

func ActiveTargets

func ActiveTargets() map[string]bool

ActiveTargets asks a running daemon which targets are active. Best effort: an unreachable daemon yields an empty set rather than an error, and it never starts a daemon just to answer.

func AddToTarget

func AddToTarget(s Scope, name string, svcs []string) ([]string, error)

AddToTarget adds services to a target, creating it if needed. Every service must be defined in the same config. It returns the target's members after the change.

func CreateTarget

func CreateTarget(s Scope, name string) error

CreateTarget adds a new, empty target.

func DropMembers

func DropMembers(existing, drop []string) []string

DropMembers returns existing with every name in drop removed, preserving order.

func EditTargets

func EditTargets(s Scope, fn func(targets map[string][]string, known map[string]bool) error) error

EditTargets loads the config the scope resolves to (project devrun.yaml or the global registry), hands its target map and the set of service names known in that file to fn, and writes the config back when fn returns nil. fn mutates the map in place.

func EnsureDaemon

func EnsureDaemon() error

EnsureDaemon starts the daemon if it is not already running.

func MergeMembers

func MergeMembers(existing, add []string) []string

MergeMembers returns existing with each name in add appended once, preserving order and skipping names already present.

func RemoveFromTarget

func RemoveFromTarget(s Scope, name string, svcs []string) error

RemoveFromTarget drops services from a target, or deletes the target when svcs is empty.

func ScopeToRegistry

func ScopeToRegistry(svcs []ipc.ServiceInfo, reg *config.Registry) []ipc.ServiceInfo

ScopeToRegistry filters daemon-reported services down to the names present in reg, appending registry-only services as "stopped", sorted by name.

func ServiceState

func ServiceState(name string) (*config.ServiceState, error)

ServiceState returns the last-saved state of a service from the state file, or nil when it has never been started. It does not contact the daemon.

func StartTarget

func StartTarget(r *Resolved, name string) (int, error)

StartTarget asks the daemon to start every service in a target, recording it as an active target. It returns how many services the target has. It starts the daemon when needed.

func StopTarget

func StopTarget(name string) error

StopTarget asks the daemon to stop a target. The daemon keeps any member still held by another active target. Without a daemon it returns ErrNoDaemon.

Types

type AddResult

type AddResult struct {
	// Local is true when it went into the project devrun.yaml.
	Local bool
	// GroupIgnored is true when a Group was given for a project file, which has
	// no per-service group.
	GroupIgnored bool
}

AddResult reports where a service was written.

func AddService

func AddService(s Scope, svc NewService) (*AddResult, error)

AddService writes a new service into the config the scope resolves to: the project devrun.yaml when one is in scope, otherwise the global registry.

type ListResult

type ListResult struct {
	// Services is every service the resolved config defines, sorted by name,
	// with a never-started service reported as "stopped".
	Services []ipc.ServiceInfo
	// ActiveTargets names the targets the daemon currently records as started.
	ActiveTargets map[string]bool
	// Offline is true when the daemon could not be reached and Services was
	// read from the last-saved state file instead.
	Offline bool
}

ListResult is the live state of every service in a scope.

func List

func List(r *Resolved) (*ListResult, error)

List reports every service in the resolved scope with its live state. It starts the daemon when needed so the list reflects live state; if the daemon cannot be reached anyway, it falls back to the last-saved state file and sets Offline.

type LogQuery

type LogQuery struct {
	// Lines is how many lines to return, counted from the end. Zero or less
	// returns nothing.
	Lines int
	// Grep, when set, keeps only lines containing it, case-insensitively, and
	// Lines then counts matching lines.
	Grep string
	// MaxBytes caps the total size of the returned lines. Zero means no cap.
	// When the cap bites, the oldest lines are dropped first, and a single line
	// longer than the cap is cut.
	MaxBytes int
	// Plain strips every terminal control sequence from each line. Raw output
	// keeps colour codes and control bytes, as the CLI prints them.
	Plain bool
}

LogQuery selects which lines of a service's log to return.

type LogResult

type LogResult struct {
	Path  string
	Lines []string // oldest first
	// Offset is the file size the snapshot was read up to. A follower that
	// seeks here picks up exactly the lines written after the snapshot.
	Offset int64
	// Truncated is true when earlier lines exist that were not returned —
	// because of Lines or MaxBytes — or when a line was cut to fit MaxBytes.
	Truncated bool
}

LogResult is a snapshot of the end of a service's log.

func Logs

func Logs(name string, q LogQuery) (*LogResult, error)

Logs returns the last lines of a service's log. It reads the file backwards from the end, so the cost is proportional to what is returned (or, with Grep, to how far back the matches go) rather than to the size of the file.

Lines are split the way bufio.ScanLines splits them: on "\n", with one trailing "\r" removed, and a final line without a newline still counted.

type NewService

type NewService struct {
	Name    string
	Command string
	// CWD is the service's working directory. Relative paths are taken against
	// the scope's Dir. Empty means the scope's Dir.
	CWD   string
	Group string // global registry only; a project file's group is its name
	Env   map[string]string
	// Overwrite replaces an existing global service of the same name. A project
	// devrun.yaml always refuses a duplicate. The CLI's `devrun add` sets this
	// for the global registry, which is its long-standing behaviour; agents
	// never do.
	Overwrite bool
}

NewService is a service definition to add.

type Outcome

type Outcome string

Outcome is how a start ended, for one service or overall.

const (
	// OutcomeRunning: up, and stayed up for the settle period.
	OutcomeRunning Outcome = "running"
	// OutcomeAlreadyRunning: it was running before the request. Success.
	OutcomeAlreadyRunning Outcome = "already_running"
	// OutcomeStarted: the start was accepted and the caller chose not to wait.
	OutcomeStarted Outcome = "started"
	// OutcomeCrashed: it died with a non-zero code or a signal.
	OutcomeCrashed Outcome = "crashed"
	// OutcomeExited: it finished on its own with code 0 — a one-shot command.
	OutcomeExited Outcome = "exited"
	// OutcomeStopped: something stopped it while we were waiting.
	OutcomeStopped Outcome = "stopped"
	// OutcomeFailed: no process was started at all (bad command, bad cwd, …).
	OutcomeFailed Outcome = "failed"
	// OutcomeTimeout: neither up nor down when the timeout expired.
	OutcomeTimeout Outcome = "timeout"
)

func (Outcome) OK

func (o Outcome) OK() bool

OK reports whether the outcome means the service is running.

type Resolved

type Resolved struct {
	Scope
	Registry *config.Registry
	Source   config.Source
}

Resolved is a Scope with its config loaded.

func Resolve

func Resolve(s Scope) (*Resolved, error)

Resolve loads the config the scope points at. It re-reads from disk on every call — nothing is cached — so an edit made meanwhile by the TUI, the CLI or a person is always seen.

When a project devrun.yaml exists but fails to parse, the returned Resolved still carries its Source (and a nil Registry) alongside the error, so callers can name the broken file.

func (*Resolved) InlineConfig

func (r *Resolved) InlineConfig(name string) *config.ServiceConfig

InlineConfig returns the definition to ship in a start request. The daemon resolves global services by name from services.yaml itself, but it has no way to see a project devrun.yaml — so for a project service the full definition travels inline, and for a global one this is nil.

func (*Resolved) IsLocal

func (r *Resolved) IsLocal() bool

IsLocal reports whether the resolved config is a project devrun.yaml.

func (*Resolved) ScopeName

func (r *Resolved) ScopeName() string

ScopeName is "project" or "global".

func (*Resolved) Service

func (r *Resolved) Service(name string) (*config.ServiceConfig, error)

Service returns the named service's definition, or an error naming the file that was searched.

func (*Resolved) SourcePath

func (r *Resolved) SourcePath() string

SourcePath is the file the resolved config was read from: the project devrun.yaml, or the global services.yaml.

type Scope

type Scope struct {
	Dir    string
	Global bool
}

Scope says which config an operation acts on: the project devrun.yaml in Dir when there is one (unless Global), otherwise the global registry. It is the ops-level form of the CLI's working directory plus --global flag.

type ServiceOutcome

type ServiceOutcome struct {
	Name     string   `json:"name"`
	Outcome  Outcome  `json:"outcome"`
	State    string   `json:"state"`
	PID      *int     `json:"pid,omitempty"`
	Port     *int     `json:"port,omitempty"`
	ExitCode *int     `json:"exit_code,omitempty"`
	Error    string   `json:"error,omitempty"`    // the daemon's reason, for OutcomeFailed
	LogTail  []string `json:"log_tail,omitempty"` // when it did not come up
}

ServiceOutcome is how one service's start ended.

type StartResult

type StartResult struct {
	// AlreadyRunning is true when the service was running before the request.
	// That is not an error: the service is in the state the caller wanted.
	AlreadyRunning bool
	// Message is the daemon's own wording when AlreadyRunning.
	Message string
}

StartResult is the daemon's answer to a start request.

func Start

func Start(name string, cfg *config.ServiceConfig) (*StartResult, error)

Start asks the daemon to start one service. cfg is the definition to ship inline — Resolved.InlineConfig — and nil for a global service, which the daemon resolves from services.yaml itself. The daemon must already be running; see EnsureDaemon.

type StartWaitResult

type StartWaitResult struct {
	// Outcome is the overall result: for a target, running only when every
	// member is running, otherwise the worst member outcome.
	Outcome  Outcome          `json:"outcome"`
	Services []ServiceOutcome `json:"services"`
	Waited   time.Duration    `json:"-"`
}

StartWaitResult is the outcome of StartAndWait.

func StartAndWait

func StartAndWait(r *Resolved, service, target string, opts WaitOptions) (*StartWaitResult, error)

StartAndWait starts a service — or, with target set, every service in a target — and waits until each one is up (running for the settle period), down (crashed, exited, stopped, never started), or the timeout expires. A service that did not come up has the end of its log attached.

Exactly one of service and target must be non-empty. A failure to start is an outcome, not an error: the error return is for the request itself going wrong — an unknown name, or a daemon that cannot be reached.

type StopResult

type StopResult struct {
	// NotStopped is true when the daemon declined, typically because the
	// service was not running. Message carries its reason.
	NotStopped bool
	Message    string
}

StopResult is the daemon's answer to a stop request.

func Stop

func Stop(name string) (*StopResult, error)

Stop asks the daemon to stop one service. It does not start a daemon: without one, nothing is running, and it returns ErrNoDaemon.

type WaitOptions

type WaitOptions struct {
	// NoWait returns as soon as the daemon has accepted the start.
	NoWait bool
	// Timeout bounds the whole wait. Default 15s.
	Timeout time.Duration
	// Settle is how long a service must stay running to count as up, which is
	// what catches a crash a moment after boot. Default 2s.
	Settle time.Duration
	// Poll is the interval between state checks. Default 250ms.
	Poll time.Duration
	// TailLines is how many log lines to attach when a service did not come up.
	// Default 50.
	TailLines int
}

WaitOptions controls StartAndWait. Zero values take the defaults.

Jump to

Keyboard shortcuts

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