feat(config): validation on set, refusal to set incorrect values. warns when manually editing with incorrect values

This commit is contained in:
Lewis Wynne 2026-02-12 00:39:41 +00:00
parent 4bd45e7d3c
commit 32459b420b
3 changed files with 36 additions and 1 deletions

View file

@ -145,6 +145,11 @@ func loadConfig() (Config, []string, error) {
return cfg, undecoded, nil
}
// validateConfig checks invariants on a Config value before it is persisted.
func validateConfig(cfg Config) error {
return validListFormat(cfg.List.DefaultListFormat)
}
func configPath() (string, error) {
if override := os.Getenv("PDA_CONFIG"); override != "" {
return filepath.Join(override, "config.toml"), nil

View file

@ -96,7 +96,23 @@ var configEditCmd = &cobra.Command{
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
if err := c.Run(); err != nil {
return err
}
cfg, undecoded, err := loadConfig()
if err != nil {
warnf("config has errors: %v", err)
printHint("re-run 'pda config edit' to fix")
return nil
}
if len(undecoded) > 0 {
warnf("unrecognised key(s) will be ignored: %s", strings.Join(undecoded, ", "))
}
config = cfg
configUndecodedKeys = undecoded
configErr = nil
return nil
},
}
@ -177,12 +193,17 @@ var configSetCmd = &cobra.Command{
return fmt.Errorf("cannot set '%s': unsupported type %s", key, f.Kind)
}
if err := validateConfig(cfg); err != nil {
return fmt.Errorf("cannot set '%s': %w", key, err)
}
if err := writeConfigFile(cfg); err != nil {
return err
}
// Reload so subsequent commands in the same process see the change.
config = cfg
configUndecodedKeys = nil
return nil
},
}