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