Skip to content

Commit

Permalink
fix: Set error exit code after a panic
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 15, 2024
1 parent c311f9f commit 7f4347f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cmd

import (
"context"
"fmt"
"os"
"runtime/debug"

tea "github.com/charmbracelet/bubbletea"
"github.com/gabe565/cli-of-life/internal/config"
Expand Down Expand Up @@ -55,7 +58,24 @@ func run(cmd *cobra.Command, _ []string) error {
}
}

g := game.New(game.WithPattern(pat), game.WithConfig(conf))
_, err := tea.NewProgram(g, tea.WithAltScreen(), tea.WithMouseAllMotion()).Run()
program := tea.NewProgram(
game.New(game.WithPattern(pat), game.WithConfig(conf)),
tea.WithAltScreen(),
tea.WithMouseAllMotion(),
tea.WithoutCatchPanics(),
)

defer func() {
if err := recover(); err != nil {
program.Kill()
_ = program.ReleaseTerminal()
//nolint:forbidigo
fmt.Printf("Caught panic:\n\n%s\n\nRestoring terminal...\n\n", err)
debug.PrintStack()
os.Exit(1)
}
}()

_, err := program.Run()
return err
}

0 comments on commit 7f4347f

Please sign in to comment.