| 1 | package commandsscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.23 fmt/" |
| 5 | "osscip-go gomod github.com/golang/go/src go1.23 os/" |
| 6 | "stringsscip-go gomod github.com/golang/go/src go1.23 strings/" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | DefaultErrorExitCodescip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/DefaultErrorExitCode. = 1 |
| 11 | ) |
| 12 | |
| 13 | // ErrExit may be passed to CheckErr to instruct it to output nothing but exit with status code 1. |
| 14 | var ErrExitscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/ErrExit. = fmtscip-go gomod github.com/golang/go/src go1.23 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23 fmt/Errorf().("exit") |
| 15 | |
| 16 | func CheckErrscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/CheckErr().(err error) { |
| 17 | checkErrscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/checkErr().(err, fatalErrHandlerscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/fatalErrHandler.) |
| 18 | } |
| 19 | |
| 20 | // checkErr formats a given error as a string and calls the passed handleErr |
| 21 | // func with that string and an exit code. |
| 22 | func checkErrscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/checkErr().(err error, handleErr func(string, int)) { |
| 23 | |
| 24 | if err == nil { |
| 25 | return |
| 26 | } |
| 27 | |
| 28 | switch { |
| 29 | case err == ErrExitscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/ErrExit.: |
| 30 | handleErr("", DefaultErrorExitCodescip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/DefaultErrorExitCode.) |
| 31 | default: |
| 32 | switch err := err.(type) { |
| 33 | // other err type, can return different code |
| 34 | |
| 35 | default: |
| 36 | msg := err.Error() |
| 37 | if !stringsscip-go gomod github.com/golang/go/src go1.23 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.23 strings/HasPrefix().(msg, "error: ") { |
| 38 | msg = fmtscip-go gomod github.com/golang/go/src go1.23 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.23 fmt/Sprintf().("error: %s", msg) |
| 39 | } |
| 40 | handleErr(msg, DefaultErrorExitCodescip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/DefaultErrorExitCode.) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | var fatalErrHandlerscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/fatalErrHandler. = fatalscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/fatal(). |
| 46 | |
| 47 | // fatal prints the message (if provided) and then exits. |
| 48 | func fatalscip-go gomod github.com/bougou/go-ipmi . `github.com/bougou/go-ipmi/cmd/goipmi/commands`/fatal().(msg string, code int) { |
| 49 | if len(msg) > 0 { |
| 50 | // add newline if needed |
| 51 | if !stringsscip-go gomod github.com/golang/go/src go1.23 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.23 strings/HasSuffix().(msg, "\n") { |
| 52 | msg += "\n" |
| 53 | } |
| 54 | fmtscip-go gomod github.com/golang/go/src go1.23 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.23 fmt/Fprint().(osscip-go gomod github.com/golang/go/src go1.23 os/.Stderrscip-go gomod github.com/golang/go/src go1.23 os/Stderr., msg) |
| 55 | } |
| 56 | osscip-go gomod github.com/golang/go/src go1.23 os/.Exitscip-go gomod github.com/golang/go/src go1.23 os/Exit().(code) |
| 57 | } |