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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
12 changes: 7 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.Println(errorDetails.Render(line))
}
}

func printErrorFatal(title string, err error) {
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +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 out != "" {
// show the full error message output
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"))
}
}
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
}