diff options
author | Bas van Kervel <bas@ethdev.com> | 2016-06-09 17:44:42 +0800 |
---|---|---|
committer | Bas van Kervel <bas@ethdev.com> | 2016-06-09 21:37:13 +0800 |
commit | 861add3d72bcfc6c6a8976eb82dc3e7b5288883e (patch) | |
tree | 88b062eafe5acc8ed8028d04638ed8bf2e7acfaf /Godeps/_workspace/src/gopkg.in/urfave/cli.v1/errors.go | |
parent | c75d3b0ede005afc30aaa61f27bb5bbe2cf6a3f1 (diff) | |
download | go-tangerine-861add3d72bcfc6c6a8976eb82dc3e7b5288883e.tar go-tangerine-861add3d72bcfc6c6a8976eb82dc3e7b5288883e.tar.gz go-tangerine-861add3d72bcfc6c6a8976eb82dc3e7b5288883e.tar.bz2 go-tangerine-861add3d72bcfc6c6a8976eb82dc3e7b5288883e.tar.lz go-tangerine-861add3d72bcfc6c6a8976eb82dc3e7b5288883e.tar.xz go-tangerine-861add3d72bcfc6c6a8976eb82dc3e7b5288883e.tar.zst go-tangerine-861add3d72bcfc6c6a8976eb82dc3e7b5288883e.zip |
cmd/geth: codegansta/cli package renamed to urfave/cli
Diffstat (limited to 'Godeps/_workspace/src/gopkg.in/urfave/cli.v1/errors.go')
-rw-r--r-- | Godeps/_workspace/src/gopkg.in/urfave/cli.v1/errors.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/gopkg.in/urfave/cli.v1/errors.go b/Godeps/_workspace/src/gopkg.in/urfave/cli.v1/errors.go new file mode 100644 index 000000000..ea551be16 --- /dev/null +++ b/Godeps/_workspace/src/gopkg.in/urfave/cli.v1/errors.go @@ -0,0 +1,92 @@ +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. +type MultiError struct { + Errors []error +} + +// NewMultiError creates a new MultiError. Pass in one or more errors. +func NewMultiError(err ...error) MultiError { + return MultiError{Errors: err} +} + +// Error implents the error interface. +func (m MultiError) Error() string { + errs := make([]string, len(m.Errors)) + for i, err := range m.Errors { + errs[i] = err.Error() + } + + return strings.Join(errs, "\n") +} + +// ExitCoder is the interface checked by `App` and `Command` for a custom exit +// code +type ExitCoder interface { + error + ExitCode() int +} + +// ExitError fulfills both the builtin `error` interface and `ExitCoder` +type ExitError struct { + exitCode int + message string +} + +// NewExitError makes a new *ExitError +func NewExitError(message string, exitCode int) *ExitError { + return &ExitError{ + exitCode: exitCode, + message: message, + } +} + +// Error returns the string message, fulfilling the interface required by +// `error` +func (ee *ExitError) Error() string { + return ee.message +} + +// ExitCode returns the exit code, fulfilling the interface required by +// `ExitCoder` +func (ee *ExitError) ExitCode() int { + return ee.exitCode +} + +// HandleExitCoder checks if the error fulfills the ExitCoder interface, and if +// so prints the error to stderr (if it is non-empty) and calls OsExiter with the +// given exit code. If the given error is a MultiError, then this func is +// called on all members of the Errors slice. +func HandleExitCoder(err error) { + if err == nil { + return + } + + if exitErr, ok := err.(ExitCoder); ok { + if err.Error() != "" { + fmt.Fprintln(ErrWriter, err) + } + OsExiter(exitErr.ExitCode()) + return + } + + if multiErr, ok := err.(MultiError); ok { + for _, merr := range multiErr.Errors { + HandleExitCoder(merr) + } + } +} |