Files
coder/cli/cliui/log.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
1.8 KiB
Go
Raw Normal View History

2022-03-30 17:59:54 -05:00
package cliui
import (
"fmt"
"io"
"strings"
"github.com/coder/pretty"
2022-03-30 17:59:54 -05:00
)
// cliMessage provides a human-readable message for CLI errors and messages.
type cliMessage struct {
Style pretty.Style
2022-03-30 17:59:54 -05:00
Header string
2023-03-23 17:42:20 -05:00
Prefix string
2022-03-30 17:59:54 -05:00
Lines []string
}
// String formats the CLI message for consumption by a human.
func (m cliMessage) String() string {
var str strings.Builder
2023-03-23 17:42:20 -05:00
if m.Prefix != "" {
_, _ = str.WriteString(Bold(m.Prefix))
2023-03-23 17:42:20 -05:00
}
pretty.Fprint(&str, m.Style, m.Header)
2023-03-23 17:42:20 -05:00
_, _ = str.WriteString("\r\n")
2022-03-30 17:59:54 -05:00
for _, line := range m.Lines {
_, _ = fmt.Fprintf(&str, " %s %s\r\n", pretty.Sprint(m.Style, "|"), line)
2022-03-30 17:59:54 -05:00
}
return str.String()
}
// Warn writes a log to the writer provided.
func Warn(wtr io.Writer, header string, lines ...string) {
_, _ = fmt.Fprint(wtr, cliMessage{
Style: DefaultStyles.Warn,
2023-03-23 17:42:20 -05:00
Prefix: "WARN: ",
2022-03-30 17:59:54 -05:00
Header: header,
Lines: lines,
}.String())
}
2023-03-23 17:42:20 -05:00
// Warn writes a formatted log to the writer provided.
func Warnf(wtr io.Writer, fmtStr string, args ...interface{}) {
Warn(wtr, fmt.Sprintf(fmtStr, args...))
}
// Info writes a log to the writer provided.
func Info(wtr io.Writer, header string, lines ...string) {
_, _ = fmt.Fprint(wtr, cliMessage{
Header: header,
Lines: lines,
}.String())
}
// Infof writes a formatted log to the writer provided.
func Infof(wtr io.Writer, fmtStr string, args ...interface{}) {
Info(wtr, fmt.Sprintf(fmtStr, args...))
}
// Error writes a log to the writer provided.
func Error(wtr io.Writer, header string, lines ...string) {
_, _ = fmt.Fprint(wtr, cliMessage{
Style: DefaultStyles.Error,
2023-03-23 17:42:20 -05:00
Prefix: "ERROR: ",
Header: header,
Lines: lines,
}.String())
}
// Errorf writes a formatted log to the writer provided.
func Errorf(wtr io.Writer, fmtStr string, args ...interface{}) {
Error(wtr, fmt.Sprintf(fmtStr, args...))
}