From 7d4a6fd282ec320a8f06f4bcfcc4e4d5474db173 Mon Sep 17 00:00:00 2001 From: Willie Date: Mon, 9 Mar 2026 16:29:34 +0800 Subject: [PATCH] fix(runbook): address code review findings - Guard TUI Init() against empty steps (prevents panic) - Fix plainFailReason for negated assertions (align with TUI logic) - Add pipefail to bash executor for pipeline error detection - Remove unused RunOptions.TUI field --- tools/runbook/executor.go | 2 +- tools/runbook/reporter_plain.go | 3 + tools/runbook/reporter_tui.go | 208 ++++++++++++++++++++++++++++++++ tools/runbook/runner.go | 1 - 4 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 tools/runbook/reporter_tui.go diff --git a/tools/runbook/executor.go b/tools/runbook/executor.go index 187f9d76e..6aeb8f4ec 100644 --- a/tools/runbook/executor.go +++ b/tools/runbook/executor.go @@ -18,7 +18,7 @@ func Execute(ctx context.Context, s Step) StepResult { // Merge consecutive code blocks separated by "---". command = strings.ReplaceAll(command, "\n---\n", "\n") - cmd := exec.CommandContext(ctx, "bash", "-e", "-c", command) + cmd := exec.CommandContext(ctx, "bash", "-eo", "pipefail", "-c", command) var stdout, stderr bytes.Buffer cmd.Stdout = &stdout diff --git a/tools/runbook/reporter_plain.go b/tools/runbook/reporter_plain.go index 5d9ef7685..667077cae 100644 --- a/tools/runbook/reporter_plain.go +++ b/tools/runbook/reporter_plain.go @@ -114,6 +114,9 @@ func fmtDur(ms int64) string { func plainFailReason(r StepResult) string { for _, a := range r.Assertions { if !a.Matched { + if a.Negated { + return fmt.Sprintf("unexpected match: %q", a.Pattern) + } return fmt.Sprintf("expected %q not found", a.Pattern) } } diff --git a/tools/runbook/reporter_tui.go b/tools/runbook/reporter_tui.go new file mode 100644 index 000000000..25b3b0153 --- /dev/null +++ b/tools/runbook/reporter_tui.go @@ -0,0 +1,208 @@ +package main + +import ( + "fmt" + "strings" + "time" + + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +// TUI color palette — matches skillshare CLI conventions. +var ( + tuiCyan = lipgloss.NewStyle().Foreground(lipgloss.Color("6")) + tuiGreen = lipgloss.NewStyle().Foreground(lipgloss.Color("2")) + tuiRed = lipgloss.NewStyle().Foreground(lipgloss.Color("1")) + tuiYellow = lipgloss.NewStyle().Foreground(lipgloss.Color("3")) + tuiDim = lipgloss.NewStyle().Faint(true) + tuiBold = lipgloss.NewStyle().Bold(true) +) + +// stepDoneMsg is sent when a step finishes execution. +type stepDoneMsg struct { + index int + result StepResult +} + +// tuiModel is the bubbletea model for real-time runbook progress. +type tuiModel struct { + name string + steps []Step + results []StepResult + execFn func(Step) StepResult + + done bool // all steps finished + start time.Time +} + +// newTUIModel creates a TUI model with injected execution function. +func newTUIModel(name string, steps []Step, execFn func(Step) StepResult) tuiModel { + results := make([]StepResult, len(steps)) + for i, s := range steps { + results[i] = StepResult{Step: s, Status: "pending"} + } + return tuiModel{ + name: name, + steps: steps, + results: results, + execFn: execFn, + start: time.Now(), + } +} + +func (m tuiModel) Init() tea.Cmd { + if len(m.steps) == 0 { + return tea.Quit + } + return m.runStep(0) +} + +func (m tuiModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "q", "ctrl+c": + return m, tea.Quit + } + + case stepDoneMsg: + m.results[msg.index] = msg.result + next := msg.index + 1 + if next < len(m.steps) { + return m, m.runStep(next) + } + m.done = true + return m, tea.Quit + } + + return m, nil +} + +func (m tuiModel) View() string { + var b strings.Builder + + // Header + b.WriteString(tuiBold.Render("Runbook: "+m.name) + "\n") + b.WriteString(tuiDim.Render(strings.Repeat("─", 50)) + "\n") + + // Step list + for _, r := range m.results { + icon := statusIcon(r.Status) + title := truncate(r.Step.Title, 38) + line := fmt.Sprintf(" %s %2d. %s", icon, r.Step.Number, title) + + if r.Status == "passed" || r.Status == "failed" { + line += tuiDim.Render(fmt.Sprintf(" %s", formatDuration(r.DurationMs))) + } + b.WriteString(line + "\n") + + // Show failure reason + if r.Status == "failed" { + reason := failureReason(r) + if reason != "" { + b.WriteString(tuiRed.Render(" "+reason) + "\n") + } + } + } + + // Footer + b.WriteString(tuiDim.Render(strings.Repeat("─", 50)) + "\n") + b.WriteString(m.summaryLine() + "\n") + + return b.String() +} + +// runStep returns a tea.Cmd that executes step at index i. +func (m tuiModel) runStep(i int) tea.Cmd { + step := m.steps[i] + execFn := m.execFn + return func() tea.Msg { + result := execFn(step) + return stepDoneMsg{index: i, result: result} + } +} + +// statusIcon returns the colored icon for a step status. +func statusIcon(status string) string { + switch status { + case "passed": + return tuiGreen.Render("✓") + case "failed": + return tuiRed.Render("✗") + case "running": + return tuiCyan.Render("●") + default: // pending + return tuiDim.Render("○") + } +} + +// failureReason extracts a concise failure reason from a StepResult. +func failureReason(r StepResult) string { + // Check for failed assertions first. + for _, a := range r.Assertions { + if !a.Matched { + if a.Negated { + return fmt.Sprintf("unexpected match: %s", a.Pattern) + } + return fmt.Sprintf("expected: %s", a.Pattern) + } + } + if r.Error != "" { + return r.Error + } + if r.ExitCode != 0 { + return fmt.Sprintf("exit code %d", r.ExitCode) + } + return "" +} + +// summaryLine returns the footer summary string. +func (m tuiModel) summaryLine() string { + var passed, failed, running int + for _, r := range m.results { + switch r.Status { + case "passed": + passed++ + case "failed": + failed++ + case "running": + running++ + } + } + + elapsed := time.Since(m.start) + parts := []string{ + fmt.Sprintf("%d/%d passed", passed, len(m.steps)), + } + if failed > 0 { + parts = append(parts, tuiRed.Render(fmt.Sprintf("%d failed", failed))) + } else { + parts = append(parts, fmt.Sprintf("%d failed", failed)) + } + if running > 0 { + parts = append(parts, tuiCyan.Render(fmt.Sprintf("%d running", running))) + } + parts = append(parts, tuiDim.Render(fmt.Sprintf("%.1fs", elapsed.Seconds()))) + + return strings.Join(parts, " ") +} + +// truncate shortens s to max characters, adding ellipsis if needed. +func truncate(s string, max int) string { + if len(s) <= max { + return s + } + if max <= 1 { + return "…" + } + return s[:max-1] + "…" +} + +// formatDuration formats milliseconds into a human-readable string. +func formatDuration(ms int64) string { + if ms < 1000 { + return fmt.Sprintf("%dms", ms) + } + return fmt.Sprintf("%.1fs", float64(ms)/1000) +} diff --git a/tools/runbook/runner.go b/tools/runbook/runner.go index 1be4c5be3..b57259264 100644 --- a/tools/runbook/runner.go +++ b/tools/runbook/runner.go @@ -10,7 +10,6 @@ import ( type RunOptions struct { DryRun bool JSONOutput io.Writer - TUI bool Timeout time.Duration }