-
Notifications
You must be signed in to change notification settings - Fork 786
/
cli.go
58 lines (50 loc) · 1.36 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package util
import (
"fmt"
"os"
"strings"
)
const (
// DefaultErrorExitCode is the default exit code in case of an error
DefaultErrorExitCode = 1
)
var (
fatalErrHandler = fatal
// ErrExit can be used to exit with a non 0 exit code without any error message
ErrExit = fmt.Errorf("exit")
)
// InvalidOptionf returns an error that shows the invalid option.
func InvalidOptionf(option string, value interface{}, message string, a ...interface{}) error {
text := fmt.Sprintf(message, a...)
return fmt.Errorf("invalid option: --%s %v\n%s", option, value, text)
}
// MissingOption reports a missing command line option using the full name expression.
func MissingOption(name string) error {
return fmt.Errorf("missing option: --%s", name)
}
// CheckErr prints a user friendly error to STDERR and exits with a non-zero exit code.
func CheckErr(err error) {
checkErr(err, fatalErrHandler)
}
// checkErr formats a given error as a string and calls the passed handleErr func.
func checkErr(err error, handleErr func(string, int)) {
switch {
case err == nil:
return
case err == ErrExit:
handleErr("", DefaultErrorExitCode)
return
default:
handleErr(err.Error(), DefaultErrorExitCode)
}
}
func fatal(msg string, code int) {
if len(msg) > 0 {
// add newline if needed
if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
fmt.Fprint(os.Stderr, msg)
}
os.Exit(code)
}