runconfig

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package runconfig models named run/debug configurations for a project — the equivalent of Qt Creator's "Run Settings", where one project owns several launch targets (run the app, debug it, run the package tests) and one of them is the active default.

A Config is a pure value: it says what to launch (Target), how (Mode), with which toolchain (KitName), what to build first (PreRun) and in which environment (Args / WorkDir / Env). It never launches anything itself — the IDE host turns a Config into a process. Store owns the named collection and its two invariants: names are unique, and at most one Config carries the Default flag.

Index

Constants

View Source
const (
	ModeRun   = "run"
	ModeDebug = "debug"
	ModeTest  = "test"
	ModeBench = "bench"
)

Launch modes. Mode selects which command shape the host builds for a configuration: run the target, run it under the debugger, run its tests, or run its benchmarks.

Variables

View Source
var (
	ErrEmptyName     = errors.New("runconfig: configuration name is empty")
	ErrEmptyTarget   = errors.New("runconfig: configuration target is empty")
	ErrInvalidMode   = errors.New("runconfig: unknown launch mode")
	ErrDuplicateName = errors.New("runconfig: duplicate configuration name")
	ErrNotFound      = errors.New("runconfig: configuration not found")
)

Validation errors. Callers can match these with errors.Is.

Functions

func Modes

func Modes() []string

Modes returns the launch modes in UI order. Used by editors that cycle or list the mode field.

func ParseEnv

func ParseEnv(lines []string) map[string]string

ParseEnv is the inverse of EnvSlice: it turns "KEY=value" entries into a map. Blank entries are dropped, surrounding whitespace is trimmed, an entry with no '=' becomes a key with an empty value, and a repeated key keeps the last value. Returns nil when nothing survives.

func StorePath

func StorePath(projectDir string) string

StorePath returns the conventional location of a project's run configurations, given the project's root directory.

func ValidMode

func ValidMode(mode string) bool

ValidMode reports whether mode is one of the four launch modes. The empty string is NOT a valid mode here — Validate accepts it separately as "unset, treat as ModeRun".

Types

type Config

type Config struct {
	Name    string            `json:"name"`              // unique within a Store; shown in the configuration list
	Target  string            `json:"target"`            // package path ("." / "./cmd/app") or a single file
	Args    []string          `json:"args,omitempty"`    // command-line arguments passed to the target
	WorkDir string            `json:"workDir,omitempty"` // process working directory; empty means the project root
	Env     map[string]string `json:"env,omitempty"`     // extra environment variables
	Mode    string            `json:"mode,omitempty"`    // one of the Mode* constants; empty means ModeRun
	KitName string            `json:"kit,omitempty"`     // toolchain / kit to build and launch with; empty means the default kit
	PreRun  []string          `json:"preRun,omitempty"`  // task names to run before launching (e.g. a build task)
	Default bool              `json:"default,omitempty"` // the configuration launched when none is named
}

Config is one named launch configuration.

func (Config) Clone

func (c Config) Clone() Config

Clone deep-copies the configuration so the caller can mutate the result without touching the original's slices or map.

func (Config) EffectiveMode

func (c Config) EffectiveMode() string

EffectiveMode returns Mode, substituting ModeRun when it is unset.

func (Config) EnvSlice

func (c Config) EnvSlice() []string

EnvSlice returns Env as "KEY=value" entries — the form os/exec wants — sorted by key so callers get a stable order out of the map. Returns nil when Env is empty.

func (Config) Validate

func (c Config) Validate() error

Validate reports whether the configuration is launchable: it needs a name (it is the Store key and the list label) and a target (there is nothing to launch without one). An empty Mode is accepted and means ModeRun; any other unknown mode is rejected.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the ordered collection of a project's named configurations. It enforces two invariants on every mutation: names are unique (compared after trimming, case-sensitively), and at most one Config carries the Default flag.

The zero Store is an empty, usable store.

func Load

func Load(path string) (*Store, error)

Load reads a store previously written by Save. Every entry is replayed through Add, so a hand-edited file that violates validation or repeats a name is reported instead of silently loaded half-broken.

func NewStore

func NewStore() *Store

NewStore returns an empty store.

func (*Store) Add

func (s *Store) Add(c Config) error

Add appends c. It fails with a validation error from Config.Validate, or ErrDuplicateName when the name is already taken. When c is flagged Default the flag is cleared on every other configuration.

func (*Store) Default

func (s *Store) Default() (Config, bool)

Default returns the configuration to launch when the caller does not name one: the flagged one, or the first in list order when no flag is set. Reports false only for an empty store.

func (*Store) Get

func (s *Store) Get(name string) (Config, bool)

Get returns a copy of the configuration named name.

func (*Store) Len

func (s *Store) Len() int

Len returns the number of configurations.

func (*Store) List

func (s *Store) List() []Config

List returns every configuration in store order, deep-copied so the caller cannot mutate the store through the result.

func (*Store) Remove

func (s *Store) Remove(name string) bool

Remove deletes the configuration named name, reporting whether one was removed. Removing the default leaves no explicit default, so Default falls back to the first remaining configuration.

func (*Store) Save

func (s *Store) Save(path string) error

Save writes the store to path as indented JSON, creating the parent directory when needed (path is normally under a project-local dot directory that may not exist yet).

func (*Store) SetDefault

func (s *Store) SetDefault(name string) bool

SetDefault makes name the default configuration, clearing the flag everywhere else. Reports whether name exists.

func (*Store) Update

func (s *Store) Update(name string, c Config) error

Update replaces the configuration named name with c, keeping its position in the list. c may carry a different Name — a rename — as long as the new name is free. Returns ErrNotFound when name is unknown, ErrDuplicateName when the rename collides, or a validation error from Config.Validate; the store is untouched in every failure case.

Clearing c.Default drops the explicit default; Default then falls back to the first configuration.

Jump to

Keyboard shortcuts

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