From bf39c9a77dddf5f91bb205731c8f712f53cafc64 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Wed, 4 Sep 2024 21:25:03 +0200 Subject: [PATCH 1/8] feat(#118): improve error message for `--execute` --- error.go | 2 +- main.go | 3 +++ pty.go | 10 +++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/error.go b/error.go index 4bf7fd0..c1e9f84 100644 --- a/error.go +++ b/error.go @@ -8,7 +8,7 @@ import ( ) var errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR") -var errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).Margin(0, 0, 1, 2) +var errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")) func printError(title string, err error) { fmt.Printf("%s\n", lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title)) diff --git a/main.go b/main.go index 56f9892..a6f7390 100644 --- a/main.go +++ b/main.go @@ -85,6 +85,9 @@ func main() { if config.Execute != "" { input, err = executeCommand(config) if err != nil { + if input != "" { + printErrorFatal("Something went wrong", errors.New(input)) + } printErrorFatal("Something went wrong", err) } if input == "" { diff --git a/pty.go b/pty.go index 7c782b8..4979632 100644 --- a/pty.go +++ b/pty.go @@ -9,6 +9,7 @@ import ( "io" "os" "os/exec" + "strings" "syscall" "github.com/caarlos0/go-shellwords" @@ -41,13 +42,20 @@ func executeCommand(config Config) (string, error) { } defer pty.Close() var out bytes.Buffer + var errorOut bytes.Buffer go func() { _, _ = io.Copy(&out, pty) + splittedOut := strings.Split(out.String(), "\n") + if len(splittedOut) > 0 { + errorOut.WriteString(splittedOut[0]) + } else { + errorOut.WriteString(out.String()) + } }() err = cmd.Wait() if err != nil { - return "", err + return errorOut.String(), err } return out.String(), nil } From 25670267b8f8b7ca7220a8232d074ddeeacbb0c9 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Wed, 4 Sep 2024 21:39:24 +0200 Subject: [PATCH 2/8] fix: use previous styles --- error.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error.go b/error.go index c1e9f84..4bf7fd0 100644 --- a/error.go +++ b/error.go @@ -8,7 +8,7 @@ import ( ) var errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR") -var errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")) +var errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).Margin(0, 0, 1, 2) func printError(title string, err error) { fmt.Printf("%s\n", lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title)) From d125cdc0ee779817846476400ebd67033651a025 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Thu, 5 Sep 2024 11:44:36 -0700 Subject: [PATCH 3/8] docs: add comment to clarify change --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index a6f7390..c01a7d4 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,6 @@ func main() { if len(ctx.Args) > 0 { switch ctx.Args[0] { - case "version": if Version == "" { if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" { @@ -86,6 +85,7 @@ func main() { input, err = executeCommand(config) if err != nil { if input != "" { + // show the full error message output printErrorFatal("Something went wrong", errors.New(input)) } printErrorFatal("Something went wrong", err) From d340702ffb0ba8f6db82877a321f164e58e0932f Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 8 Sep 2024 13:10:33 +0200 Subject: [PATCH 4/8] fix: print full error message --- error.go | 12 +++++++----- pty.go | 8 +------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/error.go b/error.go index 82b96d9..8796ef0 100644 --- a/error.go +++ b/error.go @@ -3,18 +3,20 @@ package main import ( "fmt" "os" + "strings" "github.com/charmbracelet/lipgloss" ) -var ( - errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR") - errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).Margin(0, 0, 1, 2) -) +var errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR") +var errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).MarginLeft(2) func printError(title string, err error) { fmt.Printf("%s\n", lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title)) - fmt.Printf("%s\n", errorDetails.Render(err.Error())) + splittedError := strings.Split(err.Error(), "\n") + for _, line := range splittedError { + fmt.Printf("%s\n", errorDetails.Render(line)) + } } func printErrorFatal(title string, err error) { diff --git a/pty.go b/pty.go index ab6dd55..27e7f7c 100644 --- a/pty.go +++ b/pty.go @@ -9,7 +9,6 @@ import ( "io" "os" "os/exec" - "strings" "syscall" "github.com/caarlos0/go-shellwords" @@ -46,12 +45,7 @@ func executeCommand(config Config) (string, error) { var errorOut bytes.Buffer go func() { _, _ = io.Copy(&out, pty) - splittedOut := strings.Split(out.String(), "\n") - if len(splittedOut) > 0 { - errorOut.WriteString(splittedOut[0]) - } else { - errorOut.WriteString(out.String()) - } + errorOut.Write(out.Bytes()) }() err = cmd.Wait() From ce901c1815abe902a62a3f91b931c6ab5009ec10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?AoMe=20=C2=B7=20=E9=9D=92=E7=9B=AE?= <71392160+AlejandroSuero@users.noreply.github.com> Date: Sun, 8 Sep 2024 14:43:13 +0200 Subject: [PATCH 5/8] feat: change to error format Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com> --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 9442aa8..f6dbd4e 100644 --- a/main.go +++ b/main.go @@ -87,7 +87,7 @@ func main() { if err != nil { if input != "" { // show the full error message output - printErrorFatal("Something went wrong", errors.New(input)) + err = fmt.Errorf("%w %s", err, input) } printErrorFatal("Something went wrong", err) } From 001b55f73aa5fdf5d5f10e14589f45abafa57a6d Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 8 Sep 2024 15:11:15 +0200 Subject: [PATCH 6/8] refactor: `input` as `out` to match `executeCommand` Also prints new line after error status. --- main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index f6dbd4e..881a8d4 100644 --- a/main.go +++ b/main.go @@ -83,15 +83,15 @@ func main() { // Copy the pty output to buffer if config.Execute != "" { - input, err = executeCommand(config) + out, err := executeCommand(config) if err != nil { - if input != "" { + if out != "" { // show the full error message output - err = fmt.Errorf("%w %s", err, input) + err = fmt.Errorf("%w\n%s", err, out) } printErrorFatal("Something went wrong", err) } - if input == "" { + if out == "" { printErrorFatal("Something went wrong", errors.New("no command output")) } } From a87cc86aa603726cd8f54aa4784214a1c2ec77d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?AoMe=20=C2=B7=20=E9=9D=92=E7=9B=AE?= <71392160+AlejandroSuero@users.noreply.github.com> Date: Sun, 8 Sep 2024 15:25:00 +0200 Subject: [PATCH 7/8] feat: use `Println` Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com> --- error.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error.go b/error.go index 8796ef0..a0d947d 100644 --- a/error.go +++ b/error.go @@ -15,7 +15,7 @@ func printError(title string, err error) { fmt.Printf("%s\n", lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title)) splittedError := strings.Split(err.Error(), "\n") for _, line := range splittedError { - fmt.Printf("%s\n", errorDetails.Render(line)) + fmt.Println(errorDetails.Render(line)) } } From 07ff573bf43b6e8de35f91f84c7f264caae69df9 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Fri, 18 Oct 2024 22:02:03 -0700 Subject: [PATCH 8/8] refactor: include entire error msg --- error.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/error.go b/error.go index a0d947d..611bd65 100644 --- a/error.go +++ b/error.go @@ -3,20 +3,27 @@ package main import ( "fmt" "os" - "strings" "github.com/charmbracelet/lipgloss" ) -var errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR") -var errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).MarginLeft(2) +var ( + errorHeader = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#F1F1F1")). + Background(lipgloss.Color("#FF5F87")). + Bold(true). + Padding(0, 1). + Margin(1). + MarginLeft(2). + SetString("ERROR") + errorDetails = lipgloss.NewStyle(). + Foreground(lipgloss.Color("#757575")). + MarginLeft(2) +) func printError(title string, err error) { - fmt.Printf("%s\n", lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title)) - splittedError := strings.Split(err.Error(), "\n") - for _, line := range splittedError { - fmt.Println(errorDetails.Render(line)) - } + fmt.Println(lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title)) + fmt.Println(errorDetails.Render(err.Error())) } func printErrorFatal(title string, err error) {