From ba7c3ac52dd0f740c31dbacb688724463ec4d79b Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 17 Jun 2024 11:04:20 +0100 Subject: [PATCH] print error messages from run using new printer --- cmd/enbas/main.go | 10 +++++++--- internal/printer/printer.go | 13 +++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index bbaf746..e5105dd 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -23,7 +23,6 @@ var ( func main() { if err := run(); err != nil { - fmt.Fprintf(os.Stderr, "ERROR: %v.\n", err) os.Exit(1) } } @@ -181,13 +180,18 @@ func run() error { exe, ok := executorMap[command] if !ok { + err := executor.UnknownCommandError{Command: command} + + printer.PrintFailure(err.Error() + ".") flag.Usage() - return executor.UnknownCommandError{Command: command} + return err } if err := executor.Execute(exe, args); err != nil { - return fmt.Errorf("(%s) %w", command, err) + printer.PrintFailure("(" + command + ") " + err.Error() + ".") + + return err } return nil diff --git a/internal/printer/printer.go b/internal/printer/printer.go index a71e8ce..72cc97a 100644 --- a/internal/printer/printer.go +++ b/internal/printer/printer.go @@ -23,6 +23,8 @@ type theme struct { green string boldgreen string grey string + red string + boldred string } type Printer struct { @@ -51,6 +53,8 @@ func NewPrinter( green: "\033[32m", boldgreen: "\033[32;1m", grey: "\033[90m", + red: "\033[31m", + boldred: "\033[31;1m", } if maxTerminalWidth < minTerminalWidth { @@ -73,7 +77,7 @@ func NewPrinter( } func (p Printer) PrintSuccess(text string) { - success := p.theme.green + p.successSymbol + p.theme.reset + success := p.theme.boldgreen + p.successSymbol + p.theme.reset if p.noColor { success = p.successSymbol } @@ -82,7 +86,12 @@ func (p Printer) PrintSuccess(text string) { } func (p Printer) PrintFailure(text string) { - printToStderr(p.failureSymbol + " " + text) + failure := p.theme.boldred + p.failureSymbol + p.theme.reset + if p.noColor { + failure = p.failureSymbol + } + + printToStderr(failure + " " + text + "\n") } func (p Printer) PrintInfo(text string) {