Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions internal/core/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
8 changes: 1 addition & 7 deletions internal/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
9 changes: 2 additions & 7 deletions internal/fetch/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 4 additions & 22 deletions internal/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
42 changes: 5 additions & 37 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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()
}
Loading