/
console.go
100 lines (82 loc) · 1.83 KB
/
console.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package console
import (
"fmt"
"os"
"strings"
"github.com/coldbrewcloud/coldbrew-cli/core"
)
func stdout(format string, a ...interface{}) (int, error) {
return fmt.Fprintf(os.Stdout, format, a...)
}
func stderr(format string, a ...interface{}) (int, error) {
return fmt.Fprintf(os.Stderr, format, a...)
}
func noop(string, ...interface{}) (int, error) {
return 0, nil
}
var (
debugfFn = noop
debugLogPrefix = ""
printfFn = stdout
errorfFn = stderr
)
func EnablePrintf(enable bool) {
if enable {
printfFn = stdout
} else {
printfFn = noop
}
}
func EnableErrorf(enable bool) {
if enable {
errorfFn = stderr
} else {
errorfFn = noop
}
}
func EnableDebugf(enable bool, prefix string) {
if enable {
debugfFn = stdout
debugLogPrefix = prefix
} else {
debugfFn = noop
debugLogPrefix = ""
}
}
func Debug(tokens ...string) (int, error) {
return debugfFn(debugLogPrefix + strings.Join(tokens, " "))
}
func Debugln(tokens ...string) (int, error) {
return debugfFn(debugLogPrefix + strings.Join(tokens, " ") + "\n")
}
func Debugf(format string, a ...interface{}) (int, error) {
if debugLogPrefix != "" {
return debugfFn(debugLogPrefix+format, a...)
} else {
return debugfFn(format, a...)
}
}
func ExitWithErrorString(format string, a ...interface{}) error {
return ExitWithError(fmt.Errorf(format, a...))
}
func ExitWithError(err error) error {
errorfFn("\n")
if ei, ok := err.(*core.Error); ok {
errorfFn("%s %s\n %s\n",
ColorFnErrorHeader("Error:"),
ColorFnErrorMessage(ei.Error()),
ColorFnSideNote("(See: "+ei.ExtraInfo()+")"))
} else {
errorfFn("%s %s\n",
ColorFnErrorHeader("Error:"),
ColorFnErrorMessage(err.Error()))
}
errorfFn("\n")
os.Exit(100)
return nil
}
func Error(message string) {
errorfFn("%s %s\n",
ColorFnErrorHeader("Error:"),
ColorFnErrorMessage(message))
}