2019-04-25 17:06:53 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OsExiter is the function used when the app exits. If not set defaults to os.Exit.
|
|
|
|
var OsExiter = os.Exit
|
|
|
|
|
|
|
|
// ErrWriter is used to write errors to the user. This can be anything
|
|
|
|
// implementing the io.Writer interface and defaults to os.Stderr.
|
|
|
|
var ErrWriter io.Writer = os.Stderr
|
|
|
|
|
|
|
|
// MultiError is an error that wraps multiple errors.
|
2020-01-04 17:44:25 +00:00
|
|
|
type MultiError interface {
|
|
|
|
error
|
|
|
|
Errors() []error
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 15:25:54 +00:00
|
|
|
// newMultiError creates a new MultiError. Pass in one or more errors.
|
2020-01-04 17:44:25 +00:00
|
|
|
func newMultiError(err ...error) MultiError {
|
|
|
|
ret := multiError(err)
|
|
|
|
return &ret
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
type multiError []error
|
|
|
|
|
2019-04-25 17:06:53 +00:00
|
|
|
// Error implements the error interface.
|
2020-01-04 17:44:25 +00:00
|
|
|
func (m *multiError) Error() string {
|
|
|
|
errs := make([]string, len(*m))
|
|
|
|
for i, err := range *m {
|
2019-04-25 17:06:53 +00:00
|
|
|
errs[i] = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(errs, "\n")
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
// Errors returns a copy of the errors slice
|
|
|
|
func (m *multiError) Errors() []error {
|
|
|
|
errs := make([]error, len(*m))
|
|
|
|
for _, err := range *m {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
return errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorFormatter is the interface that will suitably format the error output
|
2019-04-25 17:06:53 +00:00
|
|
|
type ErrorFormatter interface {
|
|
|
|
Format(s fmt.State, verb rune)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExitCoder is the interface checked by `App` and `Command` for a custom exit
|
|
|
|
// code
|
|
|
|
type ExitCoder interface {
|
|
|
|
error
|
|
|
|
ExitCode() int
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
type exitError struct {
|
2019-04-25 17:06:53 +00:00
|
|
|
exitCode int
|
|
|
|
message interface{}
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:25:54 +00:00
|
|
|
// NewExitError calls Exit to create a new ExitCoder.
|
|
|
|
//
|
|
|
|
// Deprecated: This function is a duplicate of Exit and will eventually be removed.
|
2020-01-04 17:44:25 +00:00
|
|
|
func NewExitError(message interface{}, exitCode int) ExitCoder {
|
|
|
|
return Exit(message, exitCode)
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:25:54 +00:00
|
|
|
// Exit wraps a message and exit code into an error, which by default is
|
|
|
|
// handled with a call to os.Exit during default error handling.
|
|
|
|
//
|
|
|
|
// This is the simplest way to trigger a non-zero exit code for an App without
|
|
|
|
// having to call os.Exit manually. During testing, this behavior can be avoided
|
|
|
|
// by overiding the ExitErrHandler function on an App or the package-global
|
|
|
|
// OsExiter function.
|
2020-01-04 17:44:25 +00:00
|
|
|
func Exit(message interface{}, exitCode int) ExitCoder {
|
|
|
|
return &exitError{
|
2019-04-25 17:06:53 +00:00
|
|
|
message: message,
|
2020-01-04 17:44:25 +00:00
|
|
|
exitCode: exitCode,
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func (ee *exitError) Error() string {
|
2019-04-25 17:06:53 +00:00
|
|
|
return fmt.Sprintf("%v", ee.message)
|
|
|
|
}
|
|
|
|
|
2020-01-04 17:44:25 +00:00
|
|
|
func (ee *exitError) ExitCode() int {
|
2019-04-25 17:06:53 +00:00
|
|
|
return ee.exitCode
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:25:54 +00:00
|
|
|
// HandleExitCoder handles errors implementing ExitCoder by printing their
|
|
|
|
// message and calling OsExiter with the given exit code.
|
|
|
|
//
|
|
|
|
// If the given error instead implements MultiError, each error will be checked
|
|
|
|
// for the ExitCoder interface, and OsExiter will be called with the last exit
|
|
|
|
// code found, or exit code 1 if no ExitCoder is found.
|
|
|
|
//
|
|
|
|
// This function is the default error-handling behavior for an App.
|
2019-04-25 17:06:53 +00:00
|
|
|
func HandleExitCoder(err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if exitErr, ok := err.(ExitCoder); ok {
|
|
|
|
if err.Error() != "" {
|
|
|
|
if _, ok := exitErr.(ErrorFormatter); ok {
|
2020-01-04 17:44:25 +00:00
|
|
|
_, _ = fmt.Fprintf(ErrWriter, "%+v\n", err)
|
2019-04-25 17:06:53 +00:00
|
|
|
} else {
|
2020-01-04 17:44:25 +00:00
|
|
|
_, _ = fmt.Fprintln(ErrWriter, err)
|
2019-04-25 17:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
OsExiter(exitErr.ExitCode())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if multiErr, ok := err.(MultiError); ok {
|
|
|
|
code := handleMultiError(multiErr)
|
|
|
|
OsExiter(code)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMultiError(multiErr MultiError) int {
|
|
|
|
code := 1
|
2020-01-04 17:44:25 +00:00
|
|
|
for _, merr := range multiErr.Errors() {
|
2019-04-25 17:06:53 +00:00
|
|
|
if multiErr2, ok := merr.(MultiError); ok {
|
|
|
|
code = handleMultiError(multiErr2)
|
2020-01-04 17:44:25 +00:00
|
|
|
} else if merr != nil {
|
2019-04-25 17:06:53 +00:00
|
|
|
fmt.Fprintln(ErrWriter, merr)
|
|
|
|
if exitErr, ok := merr.(ExitCoder); ok {
|
|
|
|
code = exitErr.ExitCode()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return code
|
|
|
|
}
|