-
Notifications
You must be signed in to change notification settings - Fork 110
/
print.go
82 lines (70 loc) · 2.32 KB
/
print.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cli
import (
"fmt"
"io"
"log"
"os"
"unicode"
"unicode/utf8"
"github.com/fatih/color"
)
const asciiViam = `
@@BO.. "%@@B^%@@< .}j. !B@B$v'. 'nB$$$!
.*@$%l f$$@X %$$+ &@$$ l$$$$@@^ "B$$$$@!
(@$$0'M@$@: B$$~ ~B$$@$@1 !$$BQ@@@q.0@$$0@$$!
'WB$$B@p B$$~ qB$%-!@@@o. l$@B..B@$@$@%; @$@!
.u$$$! B$$~ ;@@@& ... oB$@~!!$$@. z$$$z'. $$$!
:h' B$$~L%@$|| -@@$bi$@B 'M' $$$!
`
// printf prints a message with no prefix.
func printf(w io.Writer, format string, a ...interface{}) {
fmt.Fprintf(w, format+"\n", a...)
}
// debugf prints a message prefixed with a bold grey "Debug: ".
func debugf(w io.Writer, debugMode bool, format string, a ...interface{}) {
if !debugMode {
return
}
if _, err := color.New(color.Bold, color.FgHiBlack).Fprint(w, "Debug: "); err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, format+"\n", a...)
}
// infof prints a message prefixed with a bold cyan "Info: ".
func infof(w io.Writer, format string, a ...interface{}) {
// NOTE(benjirewis): for some reason, both errcheck and gosec complain about
// Fprint's "unchecked error" here. Fatally log any errors write errors here
// and below.
if _, err := color.New(color.Bold, color.FgCyan).Fprint(w, "Info: "); err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, format+"\n", a...)
}
// warningf prints a message prefixed with a bold yellow "Warning: ".
func warningf(w io.Writer, format string, a ...interface{}) {
if _, err := color.New(color.Bold, color.FgYellow).Fprint(w, "Warning: "); err != nil {
log.Fatal(err)
}
fmt.Fprintf(w, format+"\n", a...)
}
// Errorf prints a message prefixed with a bold red "Error: " prefix and exits with 1.
// It also capitalizes the first letter of the message.
func Errorf(w io.Writer, format string, a ...interface{}) {
if _, err := color.New(color.Bold, color.FgRed).Fprint(w, "Error: "); err != nil {
log.Fatal(err)
}
toPrint := fmt.Sprintf(format+"\n", a...)
r, i := utf8.DecodeRuneInString(toPrint)
if r == utf8.RuneError {
log.Fatal("Malformed error message:", toPrint)
}
upperR := unicode.ToUpper(r)
fmt.Fprintf(w, string(upperR)+toPrint[i:])
os.Exit(1)
}
// viamLogo prints an ASCII Viam logo.
func viamLogo(w io.Writer) {
if _, err := color.New(color.Bold, color.FgWhite).Fprint(w, asciiViam); err != nil {
log.Fatal(err)
}
}