Encapsulate ExitCoder check into a lil func
This commit is contained in:
parent
f3e55a0783
commit
f688d47415
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
|
||||
}
|
||||
}
|
||||
|
21
command.go
21
command.go
@ -3,7 +3,6 @@ package cli
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@ -125,10 +124,7 @@ func (c Command) Run(ctx *Context) (err error) {
|
||||
if err != nil {
|
||||
if c.OnUsageError != nil {
|
||||
err := c.OnUsageError(ctx, err, false)
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
HandleExitCoder(err)
|
||||
return err
|
||||
} else {
|
||||
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
|
||||
@ -160,10 +156,7 @@ func (c Command) Run(ctx *Context) (err error) {
|
||||
defer func() {
|
||||
afterErr := c.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 {
|
||||
@ -179,10 +172,7 @@ func (c Command) Run(ctx *Context) (err error) {
|
||||
fmt.Fprintln(ctx.App.Writer, err)
|
||||
fmt.Fprintln(ctx.App.Writer)
|
||||
ShowCommandHelp(ctx, c.Name)
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
HandleExitCoder(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -190,10 +180,7 @@ func (c Command) Run(ctx *Context) (err error) {
|
||||
context.Command = c
|
||||
err = c.Action(context)
|
||||
if err != nil {
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
panic("unreachable")
|
||||
}
|
||||
HandleExitCoder(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -49,3 +50,11 @@ func (ee *ExitError) String() string {
|
||||
func (ee *ExitError) ExitCode() int {
|
||||
return ee.exitCode
|
||||
}
|
||||
|
||||
// HandleExitCoder checks if the error fulfills the ExitCoder interface, and if
|
||||
// so calls os.Exit with the given exit code.
|
||||
func HandleExitCoder(err error) {
|
||||
if exitErr, ok := err.(ExitCoder); ok {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user