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:
parent
ed1a562c2c
commit
4d61a6913c
1 changed files with 18 additions and 2 deletions
20
cmd/root.go
20
cmd/root.go
|
|
@ -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:"})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue