Encapsulate ExitCoder check into a lil func
This commit is contained in:
49
app.go
49
app.go
@@ -157,10 +157,7 @@ func (a *App) Run(arguments []string) (err error) {
|
||||
if a.OnUsageError != nil {
|
||||
err := a.OnUsageError(context, err, false)
|
||||
if err != nil {
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
HandleExitCoder(err)
|
||||
}
|
||||
return err
|
||||
} else {
|
||||
@@ -198,12 +195,7 @@ func (a *App) Run(arguments []string) (err error) {
|
||||
if err != nil {
|
||||
fmt.Fprintf(a.Writer, "%v\n\n", err)
|
||||
ShowAppHelp(context)
|
||||
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
HandleExitCoder(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -219,31 +211,11 @@ func (a *App) Run(arguments []string) (err error) {
|
||||
|
||||
// Run default Action
|
||||
err = a.Action(context)
|
||||
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
HandleExitCoder(err)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Another entry point to the cli app, takes care of passing arguments and error handling
|
||||
func (a *App) RunAndExitOnError() {
|
||||
err := a.Run(os.Args)
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
os.Exit(DefaultErrorExitCode)
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
os.Exit(DefaultSuccessExitCode)
|
||||
}
|
||||
|
||||
// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
|
||||
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||
// append help to commands
|
||||
@@ -295,10 +267,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||
if err != nil {
|
||||
if a.OnUsageError != nil {
|
||||
err = a.OnUsageError(context, err, true)
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
HandleExitCoder(err)
|
||||
return nil
|
||||
} else {
|
||||
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
|
||||
@@ -321,10 +290,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||
defer func() {
|
||||
afterErr := a.After(context)
|
||||
if afterErr != nil {
|
||||
if exitErr, ok := afterErr.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
HandleExitCoder(err)
|
||||
if err != nil {
|
||||
err = NewMultiError(err, afterErr)
|
||||
} else {
|
||||
@@ -337,10 +303,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||
if a.Before != nil {
|
||||
err = a.Before(context)
|
||||
if err != nil {
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
HandleExitCoder(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user