/
exit.go
45 lines (37 loc) · 1001 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
package exit
// Inspired by
// http://stackoverflow.com/questions/26225513/how-to-test-os-exit-scenarios-in-go
import "os"
// Func takes a code as exit status
type Func func(int)
// Exit has an exit func, and will memorize the exit status code
type Exit struct {
exit Func
status int
}
// Exit calls the exiter, and then returns code as status.
// If e was declared, but never set (since only a test would set e),
// simply calls os.Exit()
func (e *Exit) Exit(code int) {
if e != nil {
e.status = code
e.exit(code)
} else {
os.Exit(code)
}
}
// Status get the exit status code as memorized
// after the call to the exit func.
func (e *Exit) Status() int {
return e.status
}
// Default returns an Exit with default os.Exit() call.
// That means the status will never be visible,
// since os.Exit() stops everything.
func Default() *Exit {
return &Exit{exit: os.Exit}
}
// NewExiter returns an exiter with a custom function
func New(exit Func) *Exit {
return &Exit{exit: exit}
}