Debian Codegraph @

die/die.go

raw
1 // Package die contains utilities for fatal error handling.
2 package die
3
4 import (
5 "fmt"
6 "os"
7 )
8
9 // If prints the error to stderr and exits if err != nil.
10 func If(err error) {
11 if err != nil {
12 fmt.Fprintf(os.Stderr, "[!] %v\n", err)
13 os.Exit(1)
14 }
15 }
16
17 // With prints the message to stderr, appending a newline, and exits.
18 func With(fstr string, args ...interface{}) {
19 out := fmt.Sprintf("[!] %s\n", fstr)
20 fmt.Fprintf(os.Stderr, out, args...)
21 os.Exit(1)
22 }
23
24 // When prints the error to stderr and exits if cond is true.
25 func When(cond bool, fstr string, args ...interface{}) {
26 if cond {
27 With(fstr, args...)
28 }
29 }