-
Notifications
You must be signed in to change notification settings - Fork 1
/
exit.go
49 lines (41 loc) · 960 Bytes
/
exit.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
/*
© 2020–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package pos
import (
"errors"
"fmt"
"os"
)
const (
StatusCodeErr = 1
StatusCodeUsage = 2
)
// Exit0 terminates the process successfully
func Exit0() {
OsExit(0)
}
// Exit1 terminate the command echoing a failure to stderr and returning status code 1
func Exit1(err error) {
Exit(StatusCodeErr, err)
}
// Exit1OneLine terminate the command echoing to stderr returning status code 1
func Exit1OneLine(err error) {
if err == nil {
err = errors.New("Exit1OneLine with err nil")
}
fmt.Fprintf(os.Stderr, "%v\n", err)
OsExit(StatusCodeErr)
}
// Exit terminate the command echoing to stderr returning status code
func Exit(code int, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
}
OsExit(code)
}
// OsExit does os.Exit
func OsExit(code int) {
os.Exit(code) // prints "exit status 1" to stderr
}