fix(dev): address CLI review feedback

This commit is contained in:
Thomas Camlong committed 2026-07-31 14:53:43 +02:00
1 parent e97eb7fc58
commit c8485ed7fc
18 files changed
+307 -160

No files matched your search

+29 -29
View File
@@ -1,6 +1,7 @@
package tui
import (
"context"
"fmt"
"os/exec"
"strings"
@@ -40,6 +41,7 @@ type dashModel struct {
type tickMsg time.Time
type actionMsg string
type rowsMsg []table.Row
type logsMsg struct {
name string
content string
@@ -54,7 +56,9 @@ func fetchLogs(name string) tea.Cmd {
if name == "" {
return logsMsg{}
}
out, err := exec.Command("docker", "logs", "--tail", "200", name).CombinedOutput()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
out, err := exec.CommandContext(ctx, "docker", "logs", "--tail", "200", name).CombinedOutput()
if err != nil {
return logsMsg{name: name, content: "logs unavailable: " + err.Error()}
}
@@ -62,6 +66,10 @@ func fetchLogs(name string) tea.Cmd {
}
}
func loadRows() tea.Cmd {
return func() tea.Msg { return rowsMsg(refreshRows()) }
}
func refreshRows() []table.Row {
containers, _ := docker.List()
rows := make([]table.Row, 0, len(containers))
@@ -103,7 +111,6 @@ func dashboardColumns(width int) []table.Column {
func newDashModel() dashModel {
t := table.New(
table.WithColumns(dashboardColumns(104)),
table.WithRows(refreshRows()),
table.WithFocused(true),
table.WithHeight(10),
table.WithWidth(104),
@@ -122,8 +129,7 @@ func newDashModel() dashModel {
styles.Focused.Prompt = styles.Focused.Prompt.Foreground(lipgloss.Color("212")).Bold(true)
styles.Focused.Text = styles.Focused.Text.Foreground(lipgloss.Color("229"))
filter.SetStyles(styles)
rows := refreshRows()
return dashModel{table: t, allRows: rows, filter: filter, logs: logs, showLogs: true, follow: true, width: 104, height: 30}
return dashModel{table: t, filter: filter, logs: logs, showLogs: true, follow: true, width: 104, height: 30}
}
func (m *dashModel) applyFilter() {
@@ -141,11 +147,6 @@ func (m *dashModel) applyFilter() {
m.table.SetRows(rows)
}
func (m *dashModel) refresh() {
m.allRows = refreshRows()
m.applyFilter()
}
func (m dashModel) selected() string {
row := m.table.SelectedRow()
if len(row) == 0 {
@@ -154,8 +155,16 @@ func (m dashModel) selected() string {
return row[0]
}
func (m dashModel) selectedPort() string {
row := m.table.SelectedRow()
if len(row) < 4 {
return ""
}
return row[3]
}
func (m dashModel) Init() tea.Cmd {
return tea.Batch(tick(), fetchLogs(m.selected()))
return tea.Batch(tick(), loadRows(), fetchLogs(m.selected()))
}
func (m dashModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -170,8 +179,11 @@ func (m dashModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.layout()
return m, nil
case tickMsg:
m.refresh()
return m, tea.Batch(tick(), fetchLogs(m.selected()))
return m, tea.Batch(tick(), loadRows())
case rowsMsg:
m.allRows = []table.Row(msg)
m.applyFilter()
return m, fetchLogs(m.selected())
case logsMsg:
if msg.name != "" && msg.name != m.selected() {
return m, nil
@@ -184,8 +196,7 @@ func (m dashModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case actionMsg:
m.status = string(msg)
m.confirmRemove = ""
m.refresh()
return m, nil
return m, loadRows()
case tea.KeyPressMsg:
if m.filtering {
switch msg.Key().Code {
@@ -278,7 +289,7 @@ func (m dashModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case "o":
if name != "" {
if port := portOf(name); port != "" {
if port := m.selectedPort(); port != "" {
if err := platform.OpenURL("http://localhost:" + port); err != nil {
m.status = "open failed: " + err.Error()
}
@@ -286,7 +297,7 @@ func (m dashModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case "c":
if name != "" {
if port := portOf(name); port != "" {
if port := m.selectedPort(); port != "" {
url := "http://localhost:" + port
if err := platform.CopyText(url); err != nil {
m.status = "copy failed: " + err.Error()
@@ -296,8 +307,7 @@ func (m dashModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}
case "r":
m.refresh()
return m, fetchLogs(name)
return m, loadRows()
case "k":
m.table.MoveUp(1)
return m, fetchLogs(m.selected())
@@ -348,16 +358,6 @@ func (m dashModel) selectedDetailView() string {
return title + "\n" + helpStyle.Render(meta)
}
func portOf(name string) string {
containers, _ := docker.List()
for _, c := range containers {
if c.Name == name {
return c.HostPort()
}
}
return ""
}
func (m dashModel) View() tea.View {
var b strings.Builder
running := 0
@@ -372,7 +372,7 @@ func (m dashModel) View() tea.View {
b.WriteString(m.filter.View() + "\n\n")
}
if len(m.table.Rows()) == 0 {
message := "no homarr containers — start one with `homarr <tag>` or `homarr --pr N`"
message := "no homarr containers — start one with `homarr run <tag>` or `homarr run --pr N`"
if len(m.allRows) > 0 {
message = "no instances match " + m.filter.Value()
}
@@ -16,6 +16,7 @@ func TestDashboardRendersRowsAndLogs(t *testing.T) {
"homarr-test:pr-6441",
"7575",
"http://localhost:7575",
"Up 2 minutes",
}})
m.logs.SetWidth(104)
m.logs.SetHeight(5)
@@ -35,8 +36,8 @@ func TestDashboardRendersRowsAndLogs(t *testing.T) {
func TestDashboardArrowNavigation(t *testing.T) {
m := newDashModel()
m.table.SetRows([]table.Row{
{"homarr_first", "running", "homarr:first", "7575", ""},
{"homarr_second", "running", "homarr:second", "7576", ""},
{"homarr_first", "running", "homarr:first", "7575", "", "Up 1 minute"},
{"homarr_second", "running", "homarr:second", "7576", "", "Up 1 minute"},
})
updated, _ := m.Update(tea.KeyPressMsg(tea.Key{Code: tea.KeyDown}))
@@ -48,7 +49,7 @@ func TestDashboardArrowNavigation(t *testing.T) {
func TestDashboardIgnoresStaleLogs(t *testing.T) {
m := newDashModel()
m.table.SetRows([]table.Row{{"homarr_current", "running", "homarr:current", "7575", ""}})
m.table.SetRows([]table.Row{{"homarr_current", "running", "homarr:current", "7575", "", "Up 1 minute"}})
m.logs.SetWidth(104)
m.logs.SetHeight(5)
m.logs.SetContent("current logs")
+51 -28
View File
@@ -109,6 +109,10 @@ type rebuildDoneMsg struct {
err error
canceled bool
}
type runningRefreshedMsg struct {
rows []prRow
generation int
}
type pullReadyMsg struct{}
type prLogTickMsg struct{}
type pullEvent struct {
@@ -129,7 +133,7 @@ func splitDockerProgress(data []byte, atEOF bool) (advance int, token []byte, er
return 0, nil, nil
}
func startPull(cmd *exec.Cmd, events chan<- pullEvent) tea.Cmd {
func startPull(ctx context.Context, cmd *exec.Cmd, events chan<- pullEvent) tea.Cmd {
return func() tea.Msg {
reader, writer, err := os.Pipe()
if err != nil {
@@ -150,7 +154,7 @@ func startPull(cmd *exec.Cmd, events chan<- pullEvent) tea.Cmd {
scanner.Split(splitDockerProgress)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line != "" {
if line != "" && ctx.Err() == nil && len(events) < cap(events)-1 {
events <- pullEvent{line: line}
}
}
@@ -180,8 +184,10 @@ func prLogTick() tea.Cmd {
func loadDev(includeBots bool) tea.Cmd {
return func() tea.Msg {
prs, prErr := gh.ListPRs(50, includeBots)
images, imgErr := docker.ListLocalImages()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
prs, prErr := gh.ListPRs(ctx, 50, includeBots)
images, imgErr := docker.ListLocalImages(ctx)
return prsLoadedMsg{prs: prs, images: images, prErr: prErr, imgErr: imgErr}
}
}
@@ -422,7 +428,7 @@ func (m *prsModel) refreshSelectedLogs() tea.Cmd {
}
func (m *prsModel) beginPull(row prRow) tea.Cmd {
plan, err := run.BuildPlan(run.Options{PR: row.pr.Number, Demo: m.demo})
plan, err := run.BuildPlan(run.Options{Context: context.Background(), PR: row.pr.Number, Demo: m.demo})
if err != nil {
m.status = err.Error()
return nil
@@ -433,7 +439,7 @@ func (m *prsModel) beginPull(row prRow) tea.Cmd {
}
}
ctx, cancel := context.WithCancel(context.Background())
events := make(chan pullEvent)
events := make(chan pullEvent, 128)
m.pulling = true
m.pullPlan = plan
m.pullCancel = cancel
@@ -450,7 +456,7 @@ func (m *prsModel) beginPull(row prRow) tea.Cmd {
m.status = fmt.Sprintf("pulling latest image for PR #%d…", row.pr.Number)
}
m.layout()
return tea.Batch(startPull(docker.PullCommandContext(ctx, plan.Image, plan.Platform), events), m.spinner.Tick)
return tea.Batch(startPull(ctx, docker.PullCommandContext(ctx, plan.Image, plan.Platform), events), m.spinner.Tick)
}
func (m *prsModel) appendPullLine(line string) {
@@ -488,24 +494,27 @@ func (m prsModel) Init() tea.Cmd {
return tea.Batch(loadDev(m.includeBots), m.spinner.Tick)
}
func refreshRunning(rows []prRow) []prRow {
containers, _ := docker.List()
for i, r := range rows {
name := fmt.Sprintf("homarr_pr_%d", r.pr.Number)
if r.kind == "local" {
name = "homarr_" + r.local.Tag
}
r.running = false
r.port = ""
for _, c := range containers {
if c.Name == name && c.Running() {
r.running = true
r.port = c.HostPort()
func refreshRunning(rows []prRow, generation int) tea.Cmd {
return func() tea.Msg {
containers, _ := docker.List()
updated := append([]prRow(nil), rows...)
for index, row := range updated {
name := fmt.Sprintf("homarr_pr_%d", row.pr.Number)
if row.kind == "local" {
name = "homarr_" + row.local.Tag
}
row.running = false
row.port = ""
for _, container := range containers {
if container.Name == name && container.Running() {
row.running = true
row.port = container.HostPort()
}
}
updated[index] = row
}
rows[i] = r
return runningRefreshedMsg{rows: updated, generation: generation}
}
return rows
}
func (m prsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -542,9 +551,8 @@ func (m prsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
for _, image := range msg.images {
m.rows = append(m.rows, prRow{kind: "local", local: image, imageState: "yes"})
}
m.rows = refreshRunning(m.rows)
m.applyFilter()
return m, m.startImageChecks()
return m, tea.Batch(m.startImageChecks(), refreshRunning(m.rows, m.imageGen))
case imageCheckedMsg:
if msg.gen != m.imageGen {
return m, nil
@@ -566,11 +574,19 @@ func (m prsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, m.startImageChecks()
case prActionMsg:
m.status = msg.text
m.rows = refreshRunning(m.rows)
return m, refreshRunning(m.rows, m.imageGen)
case runningRefreshedMsg:
if msg.generation != m.imageGen {
return m, nil
}
m.rows = msg.rows
m.applyFilter()
return m, m.refreshSelectedLogs()
case rebuildDoneMsg:
m.rebuilding = false
if m.rebuildCancel != nil {
m.rebuildCancel()
}
m.rebuildCancel = nil
m.loading = true
if msg.canceled {
@@ -604,6 +620,9 @@ func (m prsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
m.pulling = false
m.pullEvents = nil
if m.pullCancel != nil {
m.pullCancel()
}
m.pullCancel = nil
m.layout()
if msg.err != nil {
@@ -619,6 +638,9 @@ func (m prsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.pullCanceled = false
plan := m.pullPlan
m.pullPlan = nil
if plan == nil {
return m, nil
}
for i := range m.rows {
if m.rows[i].pr.Number == plan.PRNumber {
m.rows[i].imageState = "yes"
@@ -731,7 +753,7 @@ func (m prsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m, m.beginPull(*row)
}
plan, err := run.BuildPlan(run.Options{Tag: row.local.Tag, Demo: m.demo})
plan, err := run.BuildPlan(run.Options{Context: context.Background(), Tag: row.local.Tag, Demo: m.demo})
if err != nil {
m.status = err.Error()
return m, nil
@@ -897,10 +919,11 @@ func (m prsModel) selectedDetailView() string {
}
func truncateText(value string, length int) string {
if len(value) <= length {
runes := []rune(value)
if len(runes) <= length {
return value
}
return value[:length]
return string(runes[:length])
}
func (m prsModel) View() tea.View {
+18
View File
@@ -115,3 +115,21 @@ func TestRebuildCanBeCanceled(t *testing.T) {
t.Fatalf("cancel state: canceled=%v status=%q", canceled, got.status)
}
}
func TestCompletedPullWithoutPlanDoesNotPanic(t *testing.T) {
m := newPRsModel()
m.pulling = true
m.pullCancel = func() {}
updated, cmd := m.Update(pullEvent{done: true})
got := updated.(prsModel)
if got.pulling || cmd != nil {
t.Fatalf("pulling=%v cmd=%v", got.pulling, cmd)
}
}
func TestTruncateTextPreservesUTF8(t *testing.T) {
if got := truncateText("🦞homarr", 2); got != "🦞h" {
t.Fatalf("truncateText() = %q", got)
}
}