From 9a77de31dc9759708ce9c2d14ea7c0876c214e71 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Wed, 16 Sep 2026 00:53:52 +0200 Subject: config and engine can load with unsaved text --- internal/config/load.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'internal/config/load.go') diff --git a/internal/config/load.go b/internal/config/load.go index cd4d97c..3811802 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -20,6 +20,15 @@ type Config struct { Dirs []*Dir } +// readSource is the text of file: the caller's override when it has one, +// else the file's own bytes. +func readSource(overrides map[string][]byte, file string) ([]byte, error) { + if src, ok := overrides[file]; ok { + return src, nil + } + return os.ReadFile(file) +} + // DefaultFile is krino.conf in the XDG config directory. func DefaultFile() string { return filepath.Join(xdg.ConfigHome(), "krino", "krino.conf") @@ -34,7 +43,16 @@ func DirFile(mainFile, name string) string { // With names, only those directories are read, and each must be included. // The Config is nil only when the main file itself cannot be read. func Load(mainFile string, names ...string) (*Config, []*Diag) { - src, err := os.ReadFile(mainFile) + return LoadWith(mainFile, nil, names...) +} + +// LoadWith is Load with some files' text supplied by the caller: overrides +// maps a file path - mainFile, or DirFile(mainFile, name) - to the text to +// read instead of that file's own, so an editor can have unsaved text +// checked exactly as a run would read it (GUI design ยง1.3). A nil map is +// Load. +func LoadWith(mainFile string, overrides map[string][]byte, names ...string) (*Config, []*Diag) { + src, err := readSource(overrides, mainFile) if errors.Is(err, fs.ErrNotExist) { return nil, []*Diag{{File: mainFile, Msg: "not found; create it with: krino init"}} } @@ -62,7 +80,7 @@ func Load(mainFile string, names ...string) (*Config, []*Diag) { } for _, name := range want { file := DirFile(mainFile, name) - src, err := os.ReadFile(file) + src, err := readSource(overrides, file) if err != nil { msg := err.Error() if errors.Is(err, fs.ErrNotExist) { -- cgit v1.3