1
1
package clog
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"strings"
6
7
7
8
"github.com/fatih/color"
9
+ "golang.org/x/xerrors"
8
10
)
9
11
10
12
// RichCLIMessage provides a human-readable message for CLI errors and messages.
@@ -13,10 +15,11 @@ type RichCLIMessage struct {
13
15
Color color.Attribute
14
16
Header string
15
17
Lines []string
18
+ error
16
19
}
17
20
18
21
// Error formats the CLI message for consumption by a human as an error.
19
- func (r RichCLIMessage ) Error () string {
22
+ func (r RichCLIMessage ) String () string {
20
23
var str strings.Builder
21
24
str .WriteString (fmt .Sprintf ("%s: %s\n " ,
22
25
color .New (r .Color ).Sprint (r .Level ),
@@ -29,37 +32,75 @@ func (r RichCLIMessage) Error() string {
29
32
}
30
33
31
34
// String gives the same formatting as Error.
32
- func (r RichCLIMessage ) String () string {
33
- return r .Error ()
35
+ func (r RichCLIMessage ) Error () string {
36
+ return r .error .Error ()
37
+ }
38
+
39
+ func Wrap (err error , msg string ) error {
40
+ var cliError RichCLIMessage
41
+ if xerrors .As (err , & cliError ) {
42
+ return xerrors .Errorf ("%s: %w" , msg , cliError .error )
43
+ }
44
+ return xerrors .Errorf ("%s: %w" , msg , err )
45
+ }
46
+
47
+ func Log (err error ) {
48
+ var cliErr RichCLIMessage
49
+ if ! xerrors .As (err , & cliErr ) {
50
+ cliErr = Fatal (err .Error ())
51
+ }
52
+ fmt .Println (cliErr .String ())
53
+ }
54
+
55
+
56
+ // Info formats according to a format specifier and prints the resulting CLI message.
57
+ func Info (format string , args ... interface {}) {
58
+ fmt .Println (RichCLIMessage {
59
+ Level : "info" ,
60
+ Color : color .FgBlue ,
61
+ Header : fmt .Sprintf (format , args ... ),
62
+ }.String ())
63
+ }
64
+
65
+ // Success formats according to a format specifier and prints the resulting CLI message.
66
+ func Success (format string , args ... interface {}) {
67
+ fmt .Println (RichCLIMessage {
68
+ Level : "success" ,
69
+ Color : color .FgGreen ,
70
+ Header : fmt .Sprintf (format , args ... ),
71
+ }.String ())
34
72
}
35
73
36
74
// Warn creates an error with the level "warning".
37
- func Warn (header string , lines ... string ) error {
75
+ func Warn (header string , lines ... string ) RichCLIMessage {
38
76
return RichCLIMessage {
39
77
Color : color .FgYellow ,
40
78
Level : "warning" ,
41
79
Header : header ,
42
80
Lines : lines ,
81
+ error : errors .New (header ),
43
82
}
44
83
}
45
84
46
85
// Error creates an error with the level "error".
47
- func Error (header string , lines ... string ) error {
86
+ func Error (header string , lines ... string ) RichCLIMessage {
48
87
return RichCLIMessage {
49
88
Color : color .FgRed ,
50
89
Level : "error" ,
51
90
Header : header ,
52
91
Lines : lines ,
92
+ error : errors .New (header ),
53
93
}
54
94
}
55
95
56
96
// Fatal creates an error with the level "fatal".
57
- func Fatal (header string , lines ... string ) error {
97
+ func Fatal (header string , lines ... string ) RichCLIMessage {
58
98
return RichCLIMessage {
59
99
Color : color .FgRed ,
60
100
Level : "fatal" ,
61
101
Header : header ,
62
102
Lines : lines ,
103
+ error : errors .New (header ),
63
104
}
64
105
}
65
106
0 commit comments