-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexit.go
64 lines (53 loc) · 1.5 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Package exit provides a few utility functions to exit a program with a message.
package exit
import (
"strings"
"path"
"fmt"
"os"
)
// Die prints a message @a to stderr and exits with 1 - fmt.Println variant.
func Die(a ...interface{}) {
if len(a) > 0 {
fmt.Fprintln(os.Stderr, a...)
} else {
fmt.Fprintln(os.Stderr, "Program terminated.")
}
os.Exit(1)
}
// Dief is like Die(), but with a format string - fmt.Printf variant.
func Dief(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, strings.TrimSpace(format) + "\n", a...)
os.Exit(1)
}
// PDie is like Die(), but preprends the name of the program.
func PDie(a ...interface{}) {
fmt.Fprint(os.Stderr, path.Base(os.Args[0]) + ": ")
if len(a) > 0 {
Die(a...)
} else {
Die("program terminated.")
}
}
// PDief is like Dief(), but preprend the name of the program.
func PDief(format string, a ...interface{}) {
Dief(path.Base(os.Args[0]) + ": " + format, a...)
}
// Fatal calls Die with a FATAL message - fmt.Println variant.
func Fatal(a ...interface{}) {
fmt.Fprint(os.Stderr, "FATAL: ")
Die(a...)
}
// Fatalf calls Dief with a FATAL message - fmt.Printf / log.Fatalf variant.
func Fatalf(format string, a ...interface{}) {
Dief("FATAL: " + format, a...)
}
// Error calls Die with an ERROR message - fmt.Println variant.
func Error(a ...interface{}) {
fmt.Fprint(os.Stderr, "ERROR: ")
Die(a...)
}
// Errorf cals Dief with an ERROR message - fmt.Printf variant.
func Errorf(format string, a ...interface{}) {
Dief("ERROR: " + format, a...)
}