diff --git a/internal/core/printer.go b/internal/core/printer.go index 40c7194..2d322bb 100644 --- a/internal/core/printer.go +++ b/internal/core/printer.go @@ -132,3 +132,52 @@ func (p *Printer) WriteString(s string) (int, error) { func (p *Printer) WriteRune(r rune) (int, error) { return p.buf.WriteRune(r) } + +// WriteErrorMsg writes the provided error to the printer. +func WriteErrorMsg(p *Printer, err error) { + WriteErrorMsgNoFlush(p, err) + p.Flush() +} + +// WriteErrorMsgNoFlush writes the provided error msg to the printer, but does +// not flush the printer. +func WriteErrorMsgNoFlush(p *Printer, err error) { + p.Set(Red) + p.Set(Bold) + p.WriteString("error") + p.Reset() + p.WriteString(": ") + + if pt, ok := err.(PrinterTo); ok { + pt.PrintTo(p) + } else { + p.WriteString(err.Error()) + } + p.WriteString("\n") +} + +// WriteWarningMsg writes the provided warning msg to the printer. +func WriteWarningMsg(p *Printer, msg string) { + p.Set(Bold) + p.Set(Yellow) + p.WriteString("warning") + p.Reset() + p.WriteString(": ") + + p.WriteString(msg) + p.WriteString("\n") + p.Flush() +} + +// WriteInfoMsg writes the provided info msg to the printer. +func WriteInfoMsg(p *Printer, msg string) { + p.Set(Bold) + p.Set(Green) + p.WriteString("info") + p.Reset() + p.WriteString(": ") + + p.WriteString(msg) + p.WriteString("\n") + p.Flush() +} diff --git a/internal/fetch/fetch.go b/internal/fetch/fetch.go index 55f6789..982df1a 100644 --- a/internal/fetch/fetch.go +++ b/internal/fetch/fetch.go @@ -72,13 +72,7 @@ func Fetch(ctx context.Context, r *Request) int { } p := r.PrinterHandle.Stderr() - p.Set(core.Red) - p.Set(core.Bold) - p.WriteString("error") - p.Reset() - p.WriteString(": ") - p.WriteString(err.Error()) - p.WriteString("\n") + core.WriteErrorMsgNoFlush(p, err) if isCertificateErr(err) { p.WriteString("\n") diff --git a/internal/fetch/print.go b/internal/fetch/print.go index 04cfad2..0f57e73 100644 --- a/internal/fetch/print.go +++ b/internal/fetch/print.go @@ -113,13 +113,8 @@ func printResponseHeaders(p *core.Printer, resp *http.Response) { } func printBinaryWarning(p *core.Printer) { - p.Set(core.Bold) - p.Set(core.Yellow) - p.WriteString("warning") - p.Reset() - p.WriteString(": the response body appears to be binary\n\n") - p.WriteString("To output to the terminal anyway, use '--output -'\n") - p.Flush() + msg := "the response body appears to be binary\n\nTo output to the terminal anyway, use '--output -'" + core.WriteWarningMsg(p, msg) } func colorForStatus(code int) core.Sequence { diff --git a/internal/update/update.go b/internal/update/update.go index 4a0d4b7..99449b0 100644 --- a/internal/update/update.go +++ b/internal/update/update.go @@ -27,14 +27,7 @@ func Update(ctx context.Context, p *core.Printer, timeout time.Duration, silent return 0 } - p.Set(core.Bold) - p.Set(core.Red) - p.WriteString("error") - p.Reset() - p.WriteString(": ") - p.WriteString(err.Error()) - p.WriteString("\n") - p.Flush() + core.WriteErrorMsg(p, err) return 1 } @@ -67,7 +60,8 @@ func update(ctx context.Context, p *core.Printer, timeout time.Duration, silent // Update the last updated time in the metadata file. err = updateLastUpdatedTime(cacheDir, time.Now()) if err != nil { - writeWarning(p, fmt.Sprintf("unable to update the 'last updated' timestamp: %s", err.Error())) + msg := fmt.Sprintf("unable to update the 'last updated' timestamp: %s", err.Error()) + core.WriteWarningMsg(p, msg) } return nil @@ -278,18 +272,6 @@ func writeInfo(p *core.Printer, silent bool, s string) { p.Flush() } -func writeWarning(p *core.Printer, s string) { - p.Set(core.Bold) - p.Set(core.Yellow) - p.WriteString("warning") - p.Reset() - p.WriteString(": ") - - p.WriteString(s) - p.WriteString("\n") - p.Flush() -} - // randomString returns a random string of lower-case letters of length "n". func randomString(n int) string { var sb strings.Builder @@ -446,7 +428,7 @@ func acquireLock(ctx context.Context, p *core.Printer, dir string, block bool) ( } if i == 0 { - writeWarning(p, "waiting on lock to begin updating\n") + core.WriteWarningMsg(p, "waiting on lock to begin updating\n") } mult := time.Duration(min(i, 10)) diff --git a/main.go b/main.go index d89d7e9..f06c276 100644 --- a/main.go +++ b/main.go @@ -41,14 +41,7 @@ func main() { file, err := config.GetFile(app.ConfigPath) if err != nil { p := core.NewHandle(app.Cfg.Color).Stderr() - writeErrPrefix(p) - if pt, ok := err.(core.PrinterTo); ok { - pt.PrintTo(p) - } else { - p.WriteString(err.Error()) - } - p.WriteString("\n") - p.Flush() + core.WriteErrorMsg(p, err) os.Exit(1) } if file != nil { @@ -173,7 +166,8 @@ func checkForUpdate(ctx context.Context, p *core.Printer, dur time.Duration) { // Check the metadata file to see if we should start an async update. ok, err := update.NeedsUpdate(ctx, p, dur) if err != nil { - writeWarning(p, fmt.Sprintf("unable to check if update is needed: %s", err.Error())) + msg := fmt.Sprintf("unable to check if update is needed: %s", err.Error()) + core.WriteWarningMsg(p, msg) return } if !ok { @@ -191,15 +185,9 @@ func checkForUpdate(ctx context.Context, p *core.Printer, dur time.Duration) { // writeCLIErr writes the provided CLI error to the Printer. func writeCLIErr(p *core.Printer, err error) { - writeErrPrefix(p) - - if pt, ok := err.(core.PrinterTo); ok { - pt.PrintTo(p) - } else { - p.WriteString(err.Error()) - } + core.WriteErrorMsgNoFlush(p, err) - p.WriteString("\n\nFor more information, try '") + p.WriteString("\nFor more information, try '") p.Set(core.Bold) p.WriteString("--help") @@ -208,23 +196,3 @@ func writeCLIErr(p *core.Printer, err error) { p.WriteString("'.\n") p.Flush() } - -func writeErrPrefix(p *core.Printer) { - p.Set(core.Bold) - p.Set(core.Red) - p.WriteString("error") - p.Reset() - p.WriteString(": ") -} - -func writeWarning(p *core.Printer, s string) { - p.Set(core.Bold) - p.Set(core.Yellow) - p.WriteString("warning") - p.Reset() - p.WriteString(": ") - - p.WriteString(s) - p.WriteString("\n") - p.Flush() -}