diff --git a/cmd/config.go b/cmd/config.go index 94e6ac0..7015555 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -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 diff --git a/cmd/config_cmd.go b/cmd/config_cmd.go index c4e3faa..acfe3fe 100644 --- a/cmd/config_cmd.go +++ b/cmd/config_cmd.go @@ -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 }, } diff --git a/testdata/config-set.ct b/testdata/config-set.ct index 38cb572..f99bebb 100644 --- a/testdata/config-set.ct +++ b/testdata/config-set.ct @@ -17,6 +17,15 @@ false $ pda config set git.auto_commit yes --> FAIL FAIL cannot set 'git.auto_commit': expected bool (true/false), got 'yes' +# Invalid list format +$ pda config set list.default_list_format yaml --> FAIL +FAIL cannot set 'list.default_list_format': must be one of 'table', 'tsv', 'csv', 'html', 'markdown', 'ndjson', or 'json' + +# Valid list format +$ pda config set list.default_list_format json +$ pda config get list.default_list_format +json + # Unknown key $ pda config set git.auto_comit true --> FAIL FAIL unknown config key 'git.auto_comit'