diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-16 00:53:52 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-16 00:53:52 +0200 |
| commit | 9a77de31dc9759708ce9c2d14ea7c0876c214e71 (patch) | |
| tree | 9bd8bab97206468bdd1eed3d77481e37d3fe75e7 /internal/config/load.go | |
| parent | 1b39b2683eb820560ba882936d4818af0dcfcec7 (diff) | |
| download | krino-9a77de31dc9759708ce9c2d14ea7c0876c214e71.tar.gz krino-9a77de31dc9759708ce9c2d14ea7c0876c214e71.zip | |
config and engine can load with unsaved text
Diffstat (limited to 'internal/config/load.go')
| -rw-r--r-- | internal/config/load.go | 22 |
1 files changed, 20 insertions, 2 deletions
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) { |
