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-04-27 13:13:52 +00:00
|
|
|
type MultiError struct {
|
2016-05-24 00:15:35 +00:00
|
|
|
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-04-27 13:13:52 +00:00
|
|
|
func NewMultiError(err ...error) MultiError {
|
2016-05-24 00:15:35 +00:00
|
|
|
return MultiError{errors: err}
|
2016-04-27 13:13:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 14:12:59 +00:00
|
|
|
// Error implents the error interface.
|
2016-04-27 13:13:52 +00:00
|
|
|
func (m MultiError) Error() string {
|
2016-05-24 00:15:35 +00:00
|
|
|
errs := make([]string, len(m.errors))
|
|
|
|
for i, err := range m.errors {
|
2016-04-27 13:13:52 +00:00
|
|
|
errs[i] = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(errs, "\n")
|
|
|
|
}
|
|
|
|
|
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-04-27 13:54:08 +00:00
|
|
|
// ExitError fulfills both the builtin `error` interface and `ExitCoder`
|
2016-04-27 13:13:52 +00:00
|
|
|
type ExitError struct {
|
|
|
|
exitCode int
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:54:08 +00:00
|
|
|
// NewExitError makes a new *ExitError
|
2016-04-27 13:13:52 +00:00
|
|
|
func NewExitError(message string, exitCode int) *ExitError {
|
|
|
|
return &ExitError{
|
|
|
|
exitCode: exitCode,
|
|
|
|
message: message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:54:08 +00:00
|
|
|
// Error returns the string message, fulfilling the interface required by
|
|
|
|
// `error`
|
2016-04-27 13:13:52 +00:00
|
|
|
func (ee *ExitError) Error() string {
|
|
|
|
return ee.message
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:54:08 +00:00
|
|
|
// ExitCode returns the exit code, fulfilling the interface required by
|
|
|
|
// `ExitCoder`
|
2016-04-27 13:13:52 +00:00
|
|
|
func (ee *ExitError) ExitCode() int {
|
|
|
|
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-24 00:15:35 +00:00
|
|
|
for _, merr := range multiErr.errors {
|
2016-04-27 13:54:08 +00:00
|
|
|
HandleExitCoder(merr)
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 13:18:42 +00:00
|
|
|
}
|