2016-04-27 13:13:52 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-05-06 16:14:26 +00:00
|
|
|
"io"
|
2016-04-27 13:18:42 +00:00
|
|
|
"os"
|
2016-04-27 13:13:52 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// OsExiter is the function used when the app exits. If not set defaults to os.Exit.
|
2016-05-02 15:25:37 +00:00
|
|
|
var OsExiter = os.Exit
|
|
|
|
|
2016-05-06 16:14:26 +00:00
|
|
|
// ErrWriter is used to write errors to the user. This can be anything
|
2016-05-05 14:26:53 +00:00
|
|
|
// implementing the io.Writer interface and defaults to os.Stderr.
|
2016-05-06 16:14:26 +00:00
|
|
|
var ErrWriter io.Writer = os.Stderr
|
2016-05-05 14:26:53 +00:00
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// MultiError is an error that wraps multiple errors.
|
2016-05-25 16:05:14 +00:00
|
|
|
type MultiError interface {
|
|
|
|
error
|
|
|
|
// Errors returns a copy of the errors slice
|
|
|
|
Errors() []error
|
2016-04-27 13:13:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// NewMultiError creates a new MultiError. Pass in one or more errors.
|
2016-05-25 16:05:14 +00:00
|
|
|
func newMultiError(err ...error) MultiError {
|
|
|
|
ret := multiError(err)
|
|
|
|
return &ret
|
2016-04-27 13:13:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
type multiError []error
|
|
|
|
|
|
|
|
// Error implements the error interface.
|
|
|
|
func (m *multiError) Error() string {
|
|
|
|
errs := make([]string, len(*m))
|
|
|
|
for i, err := range *m {
|
2016-04-27 13:13:52 +00:00
|
|
|
errs[i] = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(errs, "\n")
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +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
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:54:08 +00:00
|
|
|
// ExitCoder is the interface checked by `App` and `Command` for a custom exit
|
|
|
|
// code
|
2016-04-27 13:13:52 +00:00
|
|
|
type ExitCoder interface {
|
2016-05-02 15:25:37 +00:00
|
|
|
error
|
2016-04-27 13:13:52 +00:00
|
|
|
ExitCode() int
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
type exitError struct {
|
2016-04-27 13:13:52 +00:00
|
|
|
exitCode int
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
// Exit wraps a message and exit code into an ExitCoder suitable for handling by
|
|
|
|
// HandleExitCoder
|
|
|
|
func Exit(message string, exitCode int) ExitCoder {
|
|
|
|
return &exitError{
|
2016-04-27 13:13:52 +00:00
|
|
|
exitCode: exitCode,
|
|
|
|
message: message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
func (ee *exitError) Error() string {
|
2016-04-27 13:13:52 +00:00
|
|
|
return ee.message
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:05:14 +00:00
|
|
|
func (ee *exitError) ExitCode() int {
|
2016-04-27 13:13:52 +00:00
|
|
|
return ee.exitCode
|
|
|
|
}
|
2016-04-27 13:18:42 +00:00
|
|
|
|
|
|
|
// HandleExitCoder checks if the error fulfills the ExitCoder interface, and if
|
2016-05-02 15:25:37 +00:00
|
|
|
// so prints the error to stderr (if it is non-empty) and calls OsExiter with the
|
2016-04-27 13:54:08 +00:00
|
|
|
// given exit code. If the given error is a MultiError, then this func is
|
|
|
|
// called on all members of the Errors slice.
|
2016-04-27 13:18:42 +00:00
|
|
|
func HandleExitCoder(err error) {
|
2016-05-02 15:25:37 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:18:42 +00:00
|
|
|
if exitErr, ok := err.(ExitCoder); ok {
|
2016-04-27 13:54:08 +00:00
|
|
|
if err.Error() != "" {
|
2016-05-06 16:14:26 +00:00
|
|
|
fmt.Fprintln(ErrWriter, err)
|
2016-04-27 13:54:08 +00:00
|
|
|
}
|
2016-05-02 15:25:37 +00:00
|
|
|
OsExiter(exitErr.ExitCode())
|
|
|
|
return
|
2016-04-27 13:18:42 +00:00
|
|
|
}
|
2016-04-27 13:54:08 +00:00
|
|
|
|
|
|
|
if multiErr, ok := err.(MultiError); ok {
|
2016-05-25 16:05:14 +00:00
|
|
|
for _, merr := range multiErr.Errors() {
|
2016-04-27 13:54:08 +00:00
|
|
|
HandleExitCoder(merr)
|
|
|
|
}
|
2016-07-24 15:57:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() != "" {
|
|
|
|
fmt.Fprintln(ErrWriter, err)
|
2016-04-27 13:54:08 +00:00
|
|
|
}
|
2016-07-24 16:09:05 +00:00
|
|
|
OsExiter(1)
|
2016-04-27 13:18:42 +00:00
|
|
|
}
|