Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#118): improve error message for --execute #119

Merged
merged 11 commits into from
Nov 15, 2024
17 changes: 13 additions & 4 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ import (
)

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)
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))
fmt.Printf("%s\n", errorDetails.Render(err.Error()))
fmt.Println(lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title))
fmt.Println(errorDetails.Render(err.Error()))
}

func printErrorFatal(title string, err error) {
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func main() {
if config.Execute != "" {
input, err = executeCommand(config)
AlejandroSuero marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if input != "" {
err = fmt.Errorf("%w\n%s", err, input)
}
printErrorFatal("Something went wrong", err)
}
if input == "" {
Expand Down
4 changes: 3 additions & 1 deletion pty.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ func executeCommand(config Config) (string, error) {
}
defer pty.Close() //nolint: errcheck
var out bytes.Buffer
var errorOut bytes.Buffer
go func() {
_, _ = io.Copy(&out, pty)
errorOut.Write(out.Bytes())
}()

err = cmd.Wait()
if err != nil {
return "", err //nolint: wrapcheck
return errorOut.String(), err //nolint: wrapcheck
}
return out.String(), nil
}