feat: exempt config/doctor from config errors, run doctor on failure

When the config file is malformed, config and doctor commands now
proceed with defaults (showing a warning). All other commands print
the parse error and automatically run doctor to aid diagnosis.
This commit is contained in:
Lewis Wynne 2026-02-11 23:57:55 +00:00
parent ed1a562c2c
commit 4d61a6913c

View file

@ -39,8 +39,14 @@ var rootCmd = &cobra.Command{
func Execute() { func Execute() {
if configErr != nil { if configErr != nil {
printError(fmt.Errorf("cannot load config: %v", configErr)) cmd, _, _ := rootCmd.Find(os.Args[1:])
os.Exit(1) if configSafeCmd(cmd) {
warnf("config error: %v (using defaults)", configErr)
} else {
printError(fmt.Errorf("cannot load config: %v", configErr))
runDoctor(os.Stderr)
os.Exit(1)
}
} }
err := rootCmd.Execute() err := rootCmd.Execute()
if err != nil { if err != nil {
@ -49,6 +55,16 @@ func Execute() {
} }
} }
// configSafeCmd reports whether cmd can run with a broken config.
func configSafeCmd(cmd *cobra.Command) bool {
for c := cmd; c != nil; c = c.Parent() {
if c == configCmd || c == doctorCmd {
return true
}
}
return false
}
func init() { func init() {
rootCmd.AddGroup(&cobra.Group{ID: "keys", Title: "Key commands:"}) rootCmd.AddGroup(&cobra.Group{ID: "keys", Title: "Key commands:"})