Encapsulate ExitCoder check into a lil func

This commit is contained in:
Dan Buch 2016-04-27 09:18:42 -04:00
parent f3e55a0783
commit f688d47415
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
3 changed files with 19 additions and 60 deletions

49
app.go
View File

@ -157,10 +157,7 @@ func (a *App) Run(arguments []string) (err error) {
if a.OnUsageError != nil { if a.OnUsageError != nil {
err := a.OnUsageError(context, err, false) err := a.OnUsageError(context, err, false)
if err != nil { if err != nil {
if exitErr, ok := err.(ExitCoder); ok { HandleExitCoder(err)
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
} }
return err return err
} else { } else {
@ -198,12 +195,7 @@ func (a *App) Run(arguments []string) (err error) {
if err != nil { if err != nil {
fmt.Fprintf(a.Writer, "%v\n\n", err) fmt.Fprintf(a.Writer, "%v\n\n", err)
ShowAppHelp(context) ShowAppHelp(context)
HandleExitCoder(err)
if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return err return err
} }
} }
@ -219,31 +211,11 @@ func (a *App) Run(arguments []string) (err error) {
// Run default Action // Run default Action
err = a.Action(context) err = a.Action(context)
HandleExitCoder(err)
if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return 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 // Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
func (a *App) RunAsSubcommand(ctx *Context) (err error) { func (a *App) RunAsSubcommand(ctx *Context) (err error) {
// append help to commands // append help to commands
@ -295,10 +267,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
if err != nil { if err != nil {
if a.OnUsageError != nil { if a.OnUsageError != nil {
err = a.OnUsageError(context, err, true) err = a.OnUsageError(context, err, true)
if exitErr, ok := err.(ExitCoder); ok { HandleExitCoder(err)
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return nil return nil
} else { } else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
@ -321,10 +290,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
defer func() { defer func() {
afterErr := a.After(context) afterErr := a.After(context)
if afterErr != nil { if afterErr != nil {
if exitErr, ok := afterErr.(ExitCoder); ok { HandleExitCoder(err)
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
if err != nil { if err != nil {
err = NewMultiError(err, afterErr) err = NewMultiError(err, afterErr)
} else { } else {
@ -337,10 +303,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
if a.Before != nil { if a.Before != nil {
err = a.Before(context) err = a.Before(context)
if err != nil { if err != nil {
if exitErr, ok := err.(ExitCoder); ok { HandleExitCoder(err)
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return err return err
} }
} }

View File

@ -3,7 +3,6 @@ package cli
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"sort" "sort"
"strings" "strings"
) )
@ -125,10 +124,7 @@ func (c Command) Run(ctx *Context) (err error) {
if err != nil { if err != nil {
if c.OnUsageError != nil { if c.OnUsageError != nil {
err := c.OnUsageError(ctx, err, false) err := c.OnUsageError(ctx, err, false)
if exitErr, ok := err.(ExitCoder); ok { HandleExitCoder(err)
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return err return err
} else { } else {
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.") fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
@ -160,10 +156,7 @@ func (c Command) Run(ctx *Context) (err error) {
defer func() { defer func() {
afterErr := c.After(context) afterErr := c.After(context)
if afterErr != nil { if afterErr != nil {
if exitErr, ok := afterErr.(ExitCoder); ok { HandleExitCoder(err)
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
if err != nil { if err != nil {
err = NewMultiError(err, afterErr) err = NewMultiError(err, afterErr)
} else { } else {
@ -179,10 +172,7 @@ func (c Command) Run(ctx *Context) (err error) {
fmt.Fprintln(ctx.App.Writer, err) fmt.Fprintln(ctx.App.Writer, err)
fmt.Fprintln(ctx.App.Writer) fmt.Fprintln(ctx.App.Writer)
ShowCommandHelp(ctx, c.Name) ShowCommandHelp(ctx, c.Name)
if exitErr, ok := err.(ExitCoder); ok { HandleExitCoder(err)
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return err return err
} }
} }
@ -190,10 +180,7 @@ func (c Command) Run(ctx *Context) (err error) {
context.Command = c context.Command = c
err = c.Action(context) err = c.Action(context)
if err != nil { if err != nil {
if exitErr, ok := err.(ExitCoder); ok { HandleExitCoder(err)
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
} }
return err return err
} }

View File

@ -2,6 +2,7 @@ package cli
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
) )
@ -49,3 +50,11 @@ func (ee *ExitError) String() string {
func (ee *ExitError) ExitCode() int { func (ee *ExitError) ExitCode() int {
return ee.exitCode 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())
}
}