|
| 1 | +package clog |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/fatih/color" |
| 10 | + "golang.org/x/xerrors" |
| 11 | +) |
| 12 | + |
| 13 | +// CLIMessage provides a human-readable message for CLI errors and messages. |
| 14 | +type CLIMessage struct { |
| 15 | + Level string |
| 16 | + Color color.Attribute |
| 17 | + Header string |
| 18 | + Lines []string |
| 19 | +} |
| 20 | + |
| 21 | +// CLIError wraps a CLIMessage and allows consumers to treat it as a normal error. |
| 22 | +type CLIError struct { |
| 23 | + CLIMessage |
| 24 | + error |
| 25 | +} |
| 26 | + |
| 27 | +// String formats the CLI message for consumption by a human. |
| 28 | +func (m CLIMessage) String() string { |
| 29 | + var str strings.Builder |
| 30 | + str.WriteString(fmt.Sprintf("%s: %s\n", |
| 31 | + color.New(m.Color).Sprint(m.Level), |
| 32 | + color.New(color.Bold).Sprint(m.Header)), |
| 33 | + ) |
| 34 | + for _, line := range m.Lines { |
| 35 | + str.WriteString(fmt.Sprintf(" %s %s\n", color.New(m.Color).Sprint("|"), line)) |
| 36 | + } |
| 37 | + return str.String() |
| 38 | +} |
| 39 | + |
| 40 | +// Log logs the given error to stderr, defaulting to "fatal" if the error is not a CLIError. |
| 41 | +// If the error is a CLIError, the plain error chain is ignored and the CLIError |
| 42 | +// is logged on its own. |
| 43 | +func Log(err error) { |
| 44 | + var cliErr CLIError |
| 45 | + if !xerrors.As(err, &cliErr) { |
| 46 | + cliErr = Fatal(err.Error()) |
| 47 | + } |
| 48 | + fmt.Fprintln(os.Stderr, cliErr.String()) |
| 49 | +} |
| 50 | + |
| 51 | +// LogInfo prints the given info message to stderr. |
| 52 | +func LogInfo(header string, lines ...string) { |
| 53 | + fmt.Fprint(os.Stderr, CLIMessage{ |
| 54 | + Level: "info", |
| 55 | + Color: color.FgBlue, |
| 56 | + Header: header, |
| 57 | + Lines: lines, |
| 58 | + }.String()) |
| 59 | +} |
| 60 | + |
| 61 | +// LogSuccess prints the given info message to stderr. |
| 62 | +func LogSuccess(header string, lines ...string) { |
| 63 | + fmt.Fprint(os.Stderr, CLIMessage{ |
| 64 | + Level: "success", |
| 65 | + Color: color.FgGreen, |
| 66 | + Header: header, |
| 67 | + Lines: lines, |
| 68 | + }.String()) |
| 69 | +} |
| 70 | + |
| 71 | +// Warn creates an error with the level "warning". |
| 72 | +func Warn(header string, lines ...string) CLIError { |
| 73 | + return CLIError{ |
| 74 | + CLIMessage: CLIMessage{ |
| 75 | + Color: color.FgYellow, |
| 76 | + Level: "warning", |
| 77 | + Header: header, |
| 78 | + Lines: lines, |
| 79 | + }, |
| 80 | + error: errors.New(header), |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Error creates an error with the level "error". |
| 85 | +func Error(header string, lines ...string) CLIError { |
| 86 | + return CLIError{ |
| 87 | + CLIMessage: CLIMessage{ |
| 88 | + Color: color.FgRed, |
| 89 | + Level: "error", |
| 90 | + Header: header, |
| 91 | + Lines: lines, |
| 92 | + }, |
| 93 | + error: errors.New(header), |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Fatal creates an error with the level "fatal". |
| 98 | +func Fatal(header string, lines ...string) CLIError { |
| 99 | + return CLIError{ |
| 100 | + CLIMessage: CLIMessage{ |
| 101 | + Color: color.FgRed, |
| 102 | + Level: "fatal", |
| 103 | + Header: header, |
| 104 | + Lines: lines, |
| 105 | + }, |
| 106 | + error: errors.New(header), |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Bold provides a convenience wrapper around color.New for brevity when logging. |
| 111 | +func Bold(a string) string { |
| 112 | + return color.New(color.Bold).Sprint(a) |
| 113 | +} |
| 114 | + |
| 115 | +// Tip formats according to the given format specifier and prepends a bolded "tip: " header. |
| 116 | +func Tip(format string, a ...interface{}) string { |
| 117 | + return fmt.Sprintf("%s %s", Bold("tip:"), fmt.Sprintf(format, a...)) |
| 118 | +} |
| 119 | + |
| 120 | +// Hint formats according to the given format specifier and prepends a bolded "hint: " header. |
| 121 | +func Hint(format string, a ...interface{}) string { |
| 122 | + return fmt.Sprintf("%s %s", Bold("hint:"), fmt.Sprintf(format, a...)) |
| 123 | +} |
| 124 | + |
| 125 | +// Cause formats according to the given format specifier and prepends a bolded "cause: " header. |
| 126 | +func Cause(format string, a ...interface{}) string { |
| 127 | + return fmt.Sprintf("%s %s", Bold("cause:"), fmt.Sprintf(format, a...)) |
| 128 | +} |
| 129 | + |
| 130 | +// BlankLine is an empty string meant to be used in CLIMessage and CLIError construction. |
| 131 | +const BlankLine = "" |
0 commit comments