diff --git a/cmd/root.go b/cmd/root.go index 4f4a99f..cf75c2d 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -39,8 +39,14 @@ var rootCmd = &cobra.Command{ func Execute() { if configErr != nil { - printError(fmt.Errorf("cannot load config: %v", configErr)) - os.Exit(1) + cmd, _, _ := rootCmd.Find(os.Args[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() 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() { rootCmd.AddGroup(&cobra.Group{ID: "keys", Title: "Key commands:"})