This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package clog | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
// CLIMessage provides a human-readable message for CLI errors and messages. | ||
type CLIMessage struct { | ||
Level string | ||
Color color.Attribute | ||
Header string | ||
Lines []string | ||
} | ||
|
||
// CLIError wraps a CLIMessage and allows consumers to treat it as a normal error. | ||
type CLIError struct { | ||
CLIMessage | ||
error | ||
} | ||
|
||
// String formats the CLI message for consumption by a human. | ||
func (m CLIMessage) String() string { | ||
var str strings.Builder | ||
str.WriteString(fmt.Sprintf("%s: %s\n", | ||
color.New(m.Color).Sprint(m.Level), | ||
color.New(color.Bold).Sprint(m.Header)), | ||
) | ||
for _, line := range m.Lines { | ||
str.WriteString(fmt.Sprintf(" %s %s\n", color.New(m.Color).Sprint("|"), line)) | ||
} | ||
return str.String() | ||
} | ||
|
||
// Log logs the given error to stderr, defaulting to "fatal" if the error is not a CLIError. | ||
// If the error is a CLIError, the plain error chain is ignored and the CLIError | ||
// is logged on its own. | ||
func Log(err error) { | ||
var cliErr CLIError | ||
if !xerrors.As(err, &cliErr) { | ||
cliErr = Fatal(err.Error()) | ||
} | ||
fmt.Fprintln(os.Stderr, cliErr.String()) | ||
} | ||
|
||
// LogInfo prints the given info message to stderr. | ||
func LogInfo(header string, lines ...string) { | ||
fmt.Fprint(os.Stderr, CLIMessage{ | ||
Level: "info", | ||
Color: color.FgBlue, | ||
Header: header, | ||
Lines: lines, | ||
}.String()) | ||
} | ||
|
||
// LogSuccess prints the given info message to stderr. | ||
func LogSuccess(header string, lines ...string) { | ||
fmt.Fprint(os.Stderr, CLIMessage{ | ||
Level: "success", | ||
Color: color.FgGreen, | ||
Header: header, | ||
Lines: lines, | ||
}.String()) | ||
} | ||
|
||
// Warn creates an error with the level "warning". | ||
func Warn(header string, lines ...string) CLIError { | ||
return CLIError{ | ||
CLIMessage: CLIMessage{ | ||
Color: color.FgYellow, | ||
Level: "warning", | ||
Header: header, | ||
Lines: lines, | ||
}, | ||
error: errors.New(header), | ||
} | ||
} | ||
|
||
// Error creates an error with the level "error". | ||
func Error(header string, lines ...string) CLIError { | ||
return CLIError{ | ||
CLIMessage: CLIMessage{ | ||
Color: color.FgRed, | ||
Level: "error", | ||
Header: header, | ||
Lines: lines, | ||
}, | ||
error: errors.New(header), | ||
} | ||
} | ||
|
||
// Fatal creates an error with the level "fatal". | ||
func Fatal(header string, lines ...string) CLIError { | ||
return CLIError{ | ||
CLIMessage: CLIMessage{ | ||
Color: color.FgRed, | ||
Level: "fatal", | ||
Header: header, | ||
Lines: lines, | ||
}, | ||
error: errors.New(header), | ||
} | ||
} | ||
|
||
// Bold provides a convenience wrapper around color.New for brevity when logging. | ||
func Bold(a string) string { | ||
return color.New(color.Bold).Sprint(a) | ||
} | ||
|
||
// Tip formats according to the given format specifier and prepends a bolded "tip: " header. | ||
func Tip(format string, a ...interface{}) string { | ||
return fmt.Sprintf("%s %s", Bold("tip:"), fmt.Sprintf(format, a...)) | ||
} | ||
|
||
// Hint formats according to the given format specifier and prepends a bolded "hint: " header. | ||
func Hint(format string, a ...interface{}) string { | ||
return fmt.Sprintf("%s %s", Bold("hint:"), fmt.Sprintf(format, a...)) | ||
} | ||
|
||
// Cause formats according to the given format specifier and prepends a bolded "cause: " header. | ||
func Cause(format string, a ...interface{}) string { | ||
return fmt.Sprintf("%s %s", Bold("cause:"), fmt.Sprintf(format, a...)) | ||
} | ||
|
||
// BlankLine is an empty string meant to be used in CLIMessage and CLIError construction. | ||
const BlankLine = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused why
LogInfo
andLogSuccess
are the only ones prefixed withLog
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is definitely confusing. The issue I'm having is that
Error
,Warn
, andFatal
return errors. So it would potentially be even more confusing forInfo
andSuccess
to be the only ones with side-effects.It gets pretty tricky when we actually do want to log errors as they happen: for example when iterating through a series of
stop
actions.Any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh i see now, I was skimming over the returns. Considering the others return errors this makes more sense