feat(config): validation on set, refusal to set incorrect values. warns when manually editing with incorrect values
This commit is contained in:
parent
4bd45e7d3c
commit
32459b420b
3 changed files with 36 additions and 1 deletions
|
|
@ -145,6 +145,11 @@ func loadConfig() (Config, []string, error) {
|
||||||
return cfg, undecoded, nil
|
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) {
|
func configPath() (string, error) {
|
||||||
if override := os.Getenv("PDA_CONFIG"); override != "" {
|
if override := os.Getenv("PDA_CONFIG"); override != "" {
|
||||||
return filepath.Join(override, "config.toml"), nil
|
return filepath.Join(override, "config.toml"), nil
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,23 @@ var configEditCmd = &cobra.Command{
|
||||||
c.Stdin = os.Stdin
|
c.Stdin = os.Stdin
|
||||||
c.Stdout = os.Stdout
|
c.Stdout = os.Stdout
|
||||||
c.Stderr = os.Stderr
|
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)
|
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 {
|
if err := writeConfigFile(cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reload so subsequent commands in the same process see the change.
|
// Reload so subsequent commands in the same process see the change.
|
||||||
config = cfg
|
config = cfg
|
||||||
|
configUndecodedKeys = nil
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
testdata/config-set.ct
vendored
9
testdata/config-set.ct
vendored
|
|
@ -17,6 +17,15 @@ false
|
||||||
$ pda config set git.auto_commit yes --> FAIL
|
$ pda config set git.auto_commit yes --> FAIL
|
||||||
FAIL cannot set 'git.auto_commit': expected bool (true/false), got 'yes'
|
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
|
# Unknown key
|
||||||
$ pda config set git.auto_comit true --> FAIL
|
$ pda config set git.auto_comit true --> FAIL
|
||||||
FAIL unknown config key 'git.auto_comit'
|
FAIL unknown config key 'git.auto_comit'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue