| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "os" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // OsExiter is the function used when the app exits. If not set defaults to os.Exit. |
| 11 | var OsExiter = os.Exit |
| 12 | |
| 13 | // ErrWriter is used to write errors to the user. This can be anything |
| 14 | // implementing the io.Writer interface and defaults to os.Stderr. |
| 15 | var ErrWriter io.Writer = os.Stderr |
| 16 | |
| 17 | // MultiError is an error that wraps multiple errors. |
| 18 | type MultiError interface { |
| 19 | error |
| 20 | Errors() []error |
| 21 | } |
| 22 | |
| 23 | // newMultiError creates a new MultiError. Pass in one or more errors. |
| 24 | func newMultiError(err ...error) MultiError { |
| 25 | ret := multiError(err) |
| 26 | return &ret |
| 27 | } |
| 28 | |
| 29 | type multiError []error |
| 30 | |
| 31 | // Error implements the error interface. |
| 32 | func (m *multiError) Error() string { |
| 33 | errs := make([]string, len(*m)) |
| 34 | for i, err := range *m { |
| 35 | errs[i] = err.Error() |
| 36 | } |
| 37 | |
| 38 | return strings.Join(errs, "\n") |
| 39 | } |
| 40 | |
| 41 | // Errors returns a copy of the errors slice |
| 42 | func (m *multiError) Errors() []error { |
| 43 | errs := make([]error, len(*m)) |
| 44 | copy(errs, *m) |
| 45 | return errs |
| 46 | } |
| 47 | |
| 48 | type requiredFlagsErr interface { |
| 49 | error |
| 50 | } |
| 51 | |
| 52 | type errRequiredFlags struct { |
| 53 | missingFlags []string |
| 54 | } |
| 55 | |
| 56 | func (e *errRequiredFlags) Error() string { |
| 57 | if len(e.missingFlags) == 1 { |
| 58 | return fmt.Sprintf("Required flag %q not set", e.missingFlags[0]) |
| 59 | } |
| 60 | joinedMissingFlags := strings.Join(e.missingFlags, ", ") |
| 61 | return fmt.Sprintf("Required flags %q not set", joinedMissingFlags) |
| 62 | } |
| 63 | |
| 64 | type mutuallyExclusiveGroup struct { |
| 65 | flag1Name string |
| 66 | flag2Name string |
| 67 | } |
| 68 | |
| 69 | func (e *mutuallyExclusiveGroup) Error() string { |
| 70 | return fmt.Sprintf("option %s cannot be set along with option %s", e.flag1Name, e.flag2Name) |
| 71 | } |
| 72 | |
| 73 | type mutuallyExclusiveGroupRequiredFlag struct { |
| 74 | flags *MutuallyExclusiveFlags |
| 75 | } |
| 76 | |
| 77 | func (e *mutuallyExclusiveGroupRequiredFlag) Error() string { |
| 78 | var missingFlags []string |
| 79 | for _, grpf := range e.flags.Flags { |
| 80 | var grpString []string |
| 81 | for _, f := range grpf { |
| 82 | grpString = append(grpString, f.Names()...) |
| 83 | } |
| 84 | missingFlags = append(missingFlags, strings.Join(grpString, " ")) |
| 85 | } |
| 86 | |
| 87 | return fmt.Sprintf("one of these flags needs to be provided: %s", strings.Join(missingFlags, ", ")) |
| 88 | } |
| 89 | |
| 90 | // ErrorFormatter is the interface that will suitably format the error output |
| 91 | type ErrorFormatter interface { |
| 92 | Format(s fmt.State, verb rune) |
| 93 | } |
| 94 | |
| 95 | // ExitCoder is the interface checked by `Command` for a custom exit code. |
| 96 | type ExitCoder interface { |
| 97 | error |
| 98 | ExitCode() int |
| 99 | } |
| 100 | |
| 101 | type exitError struct { |
| 102 | exitCode int |
| 103 | err error |
| 104 | } |
| 105 | |
| 106 | // Exit wraps a message and exit code into an error, which by default is |
| 107 | // handled with a call to os.Exit during default error handling. |
| 108 | // |
| 109 | // This is the simplest way to trigger a non-zero exit code for a Command without |
| 110 | // having to call os.Exit manually. During testing, this behavior can be avoided |
| 111 | // by overriding the ExitErrHandler function on a Command or the package-global |
| 112 | // OsExiter function. |
| 113 | func Exit(message any, exitCode int) ExitCoder { |
| 114 | var err error |
| 115 | |
| 116 | switch e := message.(type) { |
| 117 | case ErrorFormatter: |
| 118 | err = fmt.Errorf("%+v", message) |
| 119 | case error: |
| 120 | err = e |
| 121 | default: |
| 122 | err = fmt.Errorf("%+v", message) |
| 123 | } |
| 124 | |
| 125 | return &exitError{ |
| 126 | err: err, |
| 127 | exitCode: exitCode, |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func (ee *exitError) Error() string { |
| 132 | return ee.err.Error() |
| 133 | } |
| 134 | |
| 135 | func (ee *exitError) ExitCode() int { |
| 136 | return ee.exitCode |
| 137 | } |
| 138 | |
| 139 | // HandleExitCoder handles errors implementing ExitCoder by printing their |
| 140 | // message and calling OsExiter with the given exit code. |
| 141 | // |
| 142 | // If the given error instead implements MultiError, each error will be checked |
| 143 | // for the ExitCoder interface, and OsExiter will be called with the last exit |
| 144 | // code found, or exit code 1 if no ExitCoder is found. |
| 145 | // |
| 146 | // This function is the default error-handling behavior for a Command. |
| 147 | func HandleExitCoder(err error) { |
| 148 | if err == nil { |
| 149 | return |
| 150 | } |
| 151 | |
| 152 | if exitErr, ok := err.(ExitCoder); ok { |
| 153 | if msg := err.Error(); msg != "" { |
| 154 | if _, ok := exitErr.(ErrorFormatter); ok { |
| 155 | _, _ = fmt.Fprintf(ErrWriter, "%+v\n", err) |
| 156 | } else { |
| 157 | _, _ = fmt.Fprintln(ErrWriter, err) |
| 158 | } |
| 159 | } |
| 160 | OsExiter(exitErr.ExitCode()) |
| 161 | return |
| 162 | } |
| 163 | |
| 164 | if multiErr, ok := err.(MultiError); ok { |
| 165 | code := handleMultiError(multiErr) |
| 166 | OsExiter(code) |
| 167 | return |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func handleMultiError(multiErr MultiError) int { |
| 172 | code := 1 |
| 173 | for _, merr := range multiErr.Errors() { |
| 174 | if multiErr2, ok := merr.(MultiError); ok { |
| 175 | code = handleMultiError(multiErr2) |
| 176 | } else if merr != nil { |
| 177 | fmt.Fprintln(ErrWriter, merr) |
| 178 | if exitErr, ok := merr.(ExitCoder); ok { |
| 179 | code = exitErr.ExitCode() |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return code |
| 184 | } |