Load finds the file through paths (no ./vcom.toml), returns defaults plus an error for a broken file instead of exiting, warns about unknown and deprecated keys (strict second pass), resets invalid values with a warning, and aliases center_width_percent to ui.preview_width_percent. New keys from design §9.2 get defaults. SetKey edits one line keeping comments, order and unknown keys, appends missing keys/sections, creates the XDG file, refuses a file that does not parse and verifies the result before an atomic write. The old UI shows config errors in the status line and survives an unknown theme name; the reference vcom.toml is documented and current. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
3.9 KiB
Go
133 lines
3.9 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
"strings"
|
|
|
|
toml "github.com/pelletier/go-toml/v2"
|
|
|
|
"vcom/internal/paths"
|
|
)
|
|
|
|
// LoadInfo describes where the config came from.
|
|
type LoadInfo struct {
|
|
// Path is the file that was read, or — when Found is false — where
|
|
// SetKey would create one.
|
|
Path string
|
|
Found bool
|
|
// Warnings: unknown/deprecated keys, replaced invalid values, a stray
|
|
// ./vcom.toml that is no longer loaded.
|
|
Warnings []string
|
|
}
|
|
|
|
// deprecated keys: accepted, ignored (or aliased), reported.
|
|
var deprecated = map[string]string{
|
|
"ui.show_title_bar": "ui.show_title_bar is no longer used and is ignored",
|
|
"ui.center_width_percent": "ui.center_width_percent is renamed to ui.preview_width_percent",
|
|
"behavior.auto_refresh": "behavior.auto_refresh is not implemented and is ignored",
|
|
"behavior.auto_refresh_interval": "behavior.auto_refresh_interval is not implemented and is ignored",
|
|
}
|
|
|
|
// Load reads the config found by paths.ConfigFile(explicit). It never fails
|
|
// hard: on a read or parse error it returns Default() and the error, and the
|
|
// caller starts anyway and shows the error.
|
|
func Load(explicit string) (Config, LoadInfo, error) {
|
|
cfg := Default()
|
|
path, found, err := paths.ConfigFile(explicit)
|
|
info := LoadInfo{Path: path, Found: found}
|
|
if err != nil {
|
|
return cfg, info, fmt.Errorf("config: %w", err)
|
|
}
|
|
if !found {
|
|
if stray, ok := paths.StrayCwdConfig(); ok {
|
|
info.Warnings = append(info.Warnings, fmt.Sprintf(
|
|
"%s is not loaded any more; move it to %s or pass -config", stray, path))
|
|
}
|
|
return cfg, info, nil
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return cfg, info, fmt.Errorf("config: %w", err)
|
|
}
|
|
c, warns, err := Parse(data)
|
|
info.Warnings = append(info.Warnings, warns...)
|
|
if err != nil {
|
|
return cfg, info, fmt.Errorf("config %s: %w (using defaults)", path, err)
|
|
}
|
|
return c, info, nil
|
|
}
|
|
|
|
// Parse decodes a config file on top of Default(), applies aliases, reports
|
|
// unknown and deprecated keys and validates values.
|
|
func Parse(data []byte) (Config, []string, error) {
|
|
cfg := Default()
|
|
if err := toml.Unmarshal(data, &cfg); err != nil {
|
|
return Default(), nil, describe(err)
|
|
}
|
|
var raw map[string]any
|
|
_ = toml.Unmarshal(data, &raw) // cannot fail after the decode above
|
|
|
|
var warns []string
|
|
present := func(key string) (any, bool) {
|
|
var cur any = raw
|
|
for _, part := range strings.Split(key, ".") {
|
|
m, ok := cur.(map[string]any)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
if cur, ok = m[part]; !ok {
|
|
return nil, false
|
|
}
|
|
}
|
|
return cur, true
|
|
}
|
|
if v, ok := present("ui.center_width_percent"); ok {
|
|
if _, set := present("ui.preview_width_percent"); !set {
|
|
if n, ok := v.(int64); ok {
|
|
cfg.UI.PreviewWidthPercent = int(n)
|
|
}
|
|
}
|
|
}
|
|
if _, ok := present("ui.app_title"); ok {
|
|
warns = append(warns, "ui.app_title is deprecated and will be ignored by the new UI")
|
|
}
|
|
|
|
// Strict pass: only for the list of unknown keys.
|
|
var strict Config
|
|
dec := toml.NewDecoder(bytes.NewReader(data)).DisallowUnknownFields()
|
|
var sme *toml.StrictMissingError
|
|
if err := dec.Decode(&strict); errors.As(err, &sme) {
|
|
var unknown []string
|
|
for _, e := range sme.Errors {
|
|
key := strings.Join(e.Key(), ".")
|
|
if msg, ok := deprecated[key]; ok {
|
|
warns = append(warns, msg)
|
|
continue
|
|
}
|
|
unknown = append(unknown, key)
|
|
}
|
|
sort.Strings(unknown)
|
|
for _, k := range unknown {
|
|
warns = append(warns, fmt.Sprintf("unknown key %s (ignored)", k))
|
|
}
|
|
}
|
|
warns = append(warns, cfg.Validate()...)
|
|
return cfg, warns, nil
|
|
}
|
|
|
|
// describe adds the line/column go-toml knows to a decode error.
|
|
func describe(err error) error {
|
|
var de *toml.DecodeError
|
|
if errors.As(err, &de) {
|
|
row, col := de.Position()
|
|
return fmt.Errorf("line %d, column %d: %w", row, col, err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// DefaultUserPath is where a new config file is created.
|
|
func DefaultUserPath() (string, error) { return paths.DefaultConfigFile() }
|