Documentation
¶
Overview ¶
Package config loads and edits the TOML file that lists managed servers.
Index ¶
- Constants
- Variables
- func AddServer(path string, s Spec) error
- func CacheGood(stateDir, path string, data []byte) error
- func IsComposeCommand(cmd string) bool
- func IsDashboardLabel(label string) bool
- func NewDashboardLabel() string
- func Quote(s string) string
- func ReadPort(path string) (int, bool, error)
- func RemoveServer(path, name string) error
- func Render(s Spec) string
- func ReplaceIfUnchanged(path string, text []byte, rev string) error
- func Revision(data []byte) string
- func SetEnabled(path, name string, enabled bool) error
- func SetKey(path, name, k, literal string) error
- func SetTopLevel(path, k, literal string) error
- func SpecDir(root, abs string) string
- func StarterRoot() string
- func UpdateServer(path, name string, s Spec) error
- func UpdateServerIfUnchanged(path, name string, s Spec, rev string) error
- func ValidName(name string) error
- func WriteAtomic(path string, data []byte) error
- func WriteStarter(path string) error
- type Config
- type Duration
- type Schedule
- type Server
- type Size
- type Spec
- type Watch
Constants ¶
const ( RestartNo = "no" RestartOnFailure = "on-failure" RestartAlways = "always" // WrapAuto (the default) detects a server's tool environment and // WrapNone turns detection off. WrapAuto = "auto" WrapNone = "none" )
Restart policies.
const AutoPortBase = 3001
AutoPortBase is the first port handed out to servers without an explicit port. It is 3001 rather than 3000 because 3000 is the most contested default.
const ComposeStop = "30s"
ComposeStop is the stop timeout given to compose stacks found by discovery or imported, so containers can shut down cleanly.
const DefaultUnreadyAfter = time.Minute
DefaultUnreadyAfter is how long a running server must go without answering before OnUnready runs, unless unready_after says otherwise.
Variables ¶
var ErrConflict = errors.New("the config file changed since it was loaded; reload and try again")
ErrConflict means the file changed since the caller read it.
var ErrNotFound = errors.New("config file not found")
ErrNotFound is returned when the configuration file does not exist.
var Reserved = map[int]bool{ 3659: true, 4045: true, 5000: true, 5060: true, 5061: true, 6000: true, 6566: true, 6665: true, 6666: true, 6667: true, 6668: true, 6669: true, 6697: true, 7000: true, 10080: true, }
Reserved ports are skipped by automatic assignment: 5000/7000 belong to the macOS AirPlay Receiver, the rest are on browsers' unsafe-port lists.
var WatchSkipped = []string{".git", "node_modules", ".venv", "venv", "__pycache__", "target", "dist", "build", ".next"}
WatchSkipped are the directories "**" does not descend into: version control, dependencies, virtual environments, caches and build output.
Functions ¶
func IsComposeCommand ¶
IsComposeCommand reports whether a server's command runs a compose stack.
func IsDashboardLabel ¶
IsDashboardLabel reports whether label has the form of a dashboard host label.
func NewDashboardLabel ¶
func NewDashboardLabel() string
NewDashboardLabel returns a random label that IsDashboardLabel accepts.
func ReadPort ¶
ReadPort reads just the dashboard port even when server validation fails, and whether it is automatic (the file sets none), as Parse decides.
func RemoveServer ¶
RemoveServer deletes a server table and the comments directly above it.
func ReplaceIfUnchanged ¶
ReplaceIfUnchanged writes text only if the file still has revision rev.
func SetEnabled ¶
SetEnabled toggles a server without touching anything else in its table.
func SetKey ¶
SetKey sets one key inside a server table to a TOML literal, keeping every other line (and comment) of the table untouched.
func SetTopLevel ¶
SetTopLevel sets a top-level key (outside any table) to a TOML literal.
func SpecDir ¶
SpecDir is how the absolute directory abs is written as a server's dir: relative to root when inside it ("" for root itself), otherwise with the home directory shortened to ~.
func StarterRoot ¶
func StarterRoot() string
StarterRoot picks the directory most likely to hold the user's projects: the candidate with the most projects one or two levels down, the more conventional name winning a tie, else the home directory.
func UpdateServer ¶
UpdateServer patches changed fields of name with s (which may carry a new name). Comment lines directly above the table are kept unless s.Comment is set.
func UpdateServerIfUnchanged ¶ added in v0.10.2
UpdateServerIfUnchanged is UpdateServer for an edit made from a form that showed revision rev of the file: it returns ErrConflict when the file has changed since ("" skips the check).
func ValidName ¶
ValidName reports whether name can be used as a server name. Names double as DNS labels for the <name>.localhost proxy, hence the strict alphabet.
func WriteAtomic ¶
WriteAtomic replaces the file at path (or its symlink target) under the lock.
func WriteStarter ¶
WriteStarter creates a commented configuration file with no servers.
Types ¶
type Config ¶
type Config struct {
Path string
Root string
Port int
// PortAuto is true when the file sets no dashboard port, so the daemon
// may choose another one when the default is taken (kept in the state
// directory, see daemon.SavedPort).
PortAuto bool
Shell string
PathPrepend []string
Env map[string]string
Restore bool
Notify bool
Editor string
Terminal string
StopTimeout time.Duration
// DockerStart is the command that starts the container engine; empty
// means the engine behind the active Docker context is detected.
DockerStart string
Share bool
// OwnEnv gives servers the daemon's own environment instead of the
// account's login-shell environment (login_env = false).
OwnEnv bool
Servers []Server
// Warnings are non-fatal problems such as duplicate ports.
Warnings []string
}
Config is the parsed configuration file.
func LoadAssignedPorts ¶
LoadAssignedPorts loads the daemon's config and persists automatic ports under the edit lock, so later removals or reordering cannot change them. Automatic ports skip the ports avoid reports (nil avoids none), such as those of temporary servers, which the config does not list.
func LoadRecoverable ¶
LoadRecoverable always returns usable settings. Its error describes the requested file, which remains editable while the last good settings run.
func (*Config) FreePort ¶
FreePort returns the lowest unused, unreserved port at or above AutoPortBase.
func (*Config) Resolve ¶
Resolve expands names and group names into server names, keeping config order.
type Duration ¶
Duration is a time.Duration written as a string such as "10s" in TOML.
func (*Duration) UnmarshalText ¶
type Schedule ¶ added in v0.10.0
type Schedule struct {
// contains filtered or unexported fields
}
Schedule is a parsed crontab expression: the five standard fields (minute, hour, day of month, month, day of week) or a macro, matched in the local time of the instant Next is given.
func ParseSchedule ¶ added in v0.10.0
ParseSchedule parses a crontab expression. Its errors do not repeat the expression; callers name the field and the value.
func (*Schedule) MarshalText ¶ added in v0.10.0
MarshalText writes the expression, so a Server survives JSON (as in a handoff to an upgraded daemon).
func (*Schedule) Next ¶ added in v0.10.0
Next returns the first time after after that the schedule matches, in after's location. A local time that occurs twice (a clock turned back) matches once, at its first occurrence; one that does not occur (a clock turned forward) matches at the first instant after the jump. It returns the zero time when nothing matches within 400 years, the period after which the calendar and its weekdays repeat, which ParseSchedule rules out.
func (*Schedule) UnmarshalText ¶ added in v0.10.0
type Server ¶
type Server struct {
Name string
Dir string // absolute
DirRaw string // as written in the file
Cmd string
Port int // 0 means the server does not listen on a port
PortAuto bool // port was assigned automatically
Group string
DependsOn []string
Projects []string
Env map[string]string
Restart string
Health string // optional HTTP path probed for readiness, e.g. "/"
Icon string // optional absolute icon path
Open string // optional path appended to the URL when opening
// Wrap selects how the command enters the project's tool environment:
// "" or WrapAuto detects direnv, devbox, mise or a Node version file, WrapNone runs
// it as is, anything else is a command prefix.
Wrap string
Stop time.Duration
StopRaw string // stop_timeout as written, "" when inherited
IconRaw string // icon as written
Enabled bool
// LAN says the server is meant to be reachable from other devices, so
// listening on the network is not pointed out.
LAN bool
// OnCrash is the shell command run once the server has crashed for good:
// the server's own on_crash, else the top-level one; "" runs nothing.
OnCrash string
// ReadyLog, when set, is a regular expression a line of the server's
// output must match before it counts as running (checked at load).
ReadyLog string
// ReadyAfter is how long the process must have run before it counts as
// running; 0 means the default (immediately with a port, 1.5 s without).
ReadyAfter time.Duration
// OnUnready runs once a running server has not answered for
// UnreadyAfter (0 means DefaultUnreadyAfter), and OnRecover once a server that OnCrash or OnUnready
// reported is ready again; like OnCrash, the server's own or the
// top-level one, "" for none.
OnUnready string
OnRecover string
UnreadyAfter time.Duration
// RetryAfter, when positive, starts a server that its restart policy
// gave up on (or that could not start) again after that long, with a
// fresh round of restarts. Only with on-failure or always.
RetryAfter time.Duration
// Umask, when set, is the file mode creation mask the command runs
// with, as octal digits such as "0007"; "" keeps the daemon's.
Umask string
// Oneshot makes the server a task: its command runs to completion,
// code 0 is success, and servers that depend on it wait for that
// instead of for readiness. A task has no port.
Oneshot bool
// Schedule, when set, starts the server at the times it matches in
// local time; nil for none.
Schedule *Schedule
// MemoryLimit, when above zero, is how much memory the server's process
// tree may use: a usage sample above it stops the server as a crash.
// The server's own memory_limit, else the top-level one.
MemoryLimit Size
// Watch, when set, holds file patterns relative to Dir whose changes
// restart the server; nil for none.
Watch *Watch
}
Server is one managed dev server with defaults applied and paths resolved.
func (Server) RetriesAfterGivingUp ¶ added in v0.9.0
RetriesAfterGivingUp reports whether s is started again after its automatic restarts gave up or its start failed.
type Size ¶ added in v0.10.0
Size is an amount of memory in bytes. Binary says it was written in binary units (KiB, MiB, GiB, TiB), so it is shown in them too.
func ParseSize ¶ added in v0.10.0
ParseSize reads a size such as "2GB", "1.5 GiB" or "512MB" (units in any case). "0" is the zero size; any other number needs a unit.
type Spec ¶
type Spec struct {
Name string `json:"name"`
Dir string `json:"dir"`
Cmd string `json:"cmd"`
Port *int `json:"port,omitempty"`
Group string `json:"group,omitempty"`
DependsOn []string `json:"dependsOn,omitempty"`
Projects []string `json:"projects,omitempty"`
Env map[string]string `json:"env,omitempty"`
Restart string `json:"restart,omitempty"`
Health string `json:"health,omitempty"`
Icon string `json:"icon,omitempty"`
Open string `json:"open,omitempty"`
Stop string `json:"stopTimeout,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
// Comment is written as # lines above the table header.
Comment string `json:"comment,omitempty"`
}
Spec describes a server as written to the file. Empty fields are omitted.
type Watch ¶ added in v0.10.0
type Watch struct {
// contains filtered or unexported fields
}
Watch is a server's watch setting: file patterns relative to its directory whose changes restart it. A pattern is a slash-separated path whose segments follow path.Match, and a segment "**" stands for any number of directories, none included. "**" never enters the directories in WatchSkipped; a pattern that names one in a segment of its own reaches inside it.
func ParseWatch ¶ added in v0.10.0
ParseWatch reads the patterns of a watch setting; nil for none. A pattern that is empty, malformed, absolute, starts at the home directory or contains ".." is an error, so watching never leaves the directory.
func (*Watch) Enter ¶ added in v0.10.0
Enter reports whether a file inside the directory at rel, relative to the server's directory, can be watched, so a walk descends into it.
func (*Watch) Match ¶ added in v0.10.0
Match reports whether the file at rel, a slash-separated path relative to the server's directory, is watched.