fix: sshCmd closure bug + error display

CRITICAL BUG: sshConnectCmd wrapped tea.ExecProcess inside an extra
closure, so Bubble Tea never executed the SSH process — Enter appeared
to do nothing.

Fix:
- sshConnectCmd now returns tea.ExecProcess directly (no extra closure)
- tea.ExecProcess handles all process lifecycle (start, PTY, exit)
- sshExitMsg errors are shown in host list view (ErrorStyle)
- Error routed via Model.Error → HostListTab.err → View()
This commit is contained in:
swanadiva
2026-06-23 15:50:20 +07:00
parent 79f3917a66
commit daa20ac72b
3 changed files with 104 additions and 80 deletions
+7
View File
@@ -121,6 +121,13 @@ func (t *HostListTab) View() string {
var b strings.Builder var b strings.Builder
// Show error if any
if t.err != nil {
b.WriteString(ErrorStyle.Render(fmt.Sprintf(" Error: %v ", t.err)))
b.WriteString("\n")
t.err = nil
}
// Capped vertical padding // Capped vertical padding
b.WriteString(strings.Repeat("\n", 2)) b.WriteString(strings.Repeat("\n", 2))
+40 -34
View File
@@ -16,65 +16,65 @@ import (
// sshConnectCmd builds and runs a native SSH command via tea.ExecProcess. // sshConnectCmd builds and runs a native SSH command via tea.ExecProcess.
// Password auth: sshpass -e ssh user@host (SSHPASS env) // Password auth: sshpass -e ssh user@host (SSHPASS env)
// Key auth: ssh -i <tmpfile> user@host (SSH_ASKPASS for passphrase) // Key auth: ssh -i <tmpfile> user@host (SSH_ASKPASS for passphrase)
//
// Must return tea.ExecProcess directly (NOT wrapped in another closure)
// so Bubble Tea can execute the process command correctly.
func sshConnectCmd(host *models.Host, dataDir string) tea.Cmd { func sshConnectCmd(host *models.Host, dataDir string) tea.Cmd {
return func() tea.Msg {
port := host.Port port := host.Port
if port == 0 { if port == 0 {
port = 22 port = 22
} }
portStr := strconv.Itoa(port) portStr := strconv.Itoa(port)
target := fmt.Sprintf("%s@%s", host.Username, host.Hostname) target := fmt.Sprintf("%s@%s", host.Username, host.Hostname)
ctrlSock := fmt.Sprintf("/tmp/hk-%s", host.ID)
var cmd *exec.Cmd
env := os.Environ() env := os.Environ()
ctrlSock := fmt.Sprintf("/tmp/hk-%s", host.ID) // Common SSH args
sshArgs := []string{
switch host.Auth.Type {
case "password":
args := []string{
"-e",
"ssh",
"-p", portStr, "-p", portStr,
"-o", "StrictHostKeyChecking=accept-new", "-o", "StrictHostKeyChecking=accept-new",
"-o", "ServerAliveInterval=60", "-o", "ServerAliveInterval=60",
"-o", "ServerAliveCountMax=3", "-o", "ServerAliveCountMax=3",
"-S", ctrlSock, "-S", ctrlSock,
"-o", "ControlMaster=auto", "-o", "ControlMaster=auto",
target,
} }
cmd = exec.Command("sshpass", args...)
env = append(env, "SSHPASS="+host.Auth.Password) cleanup := func() {
exec.Command("ssh", "-S", ctrlSock, "-O", "exit", target).Run()
}
switch host.Auth.Type {
case "password":
allArgs := append([]string{"-e", "ssh"}, sshArgs...)
allArgs = append(allArgs, target)
cmd := exec.Command("sshpass", allArgs...)
cmd.Env = append(env, "SSHPASS="+host.Auth.Password)
return tea.ExecProcess(cmd, func(err error) tea.Msg {
cleanup()
return sshExitMsg{err: err}
})
case "key": case "key":
keyContent, err := loadKeyContent(host, dataDir) keyContent, err := loadKeyContent(host, dataDir)
if err != nil { if err != nil {
return sshExitMsg{err: err} return errorCmd(fmt.Errorf("load key: %w", err))
} }
tmpFile, err := os.CreateTemp("", "hk-key-*") tmpFile, err := os.CreateTemp("", "hk-key-*")
if err != nil { if err != nil {
return sshExitMsg{err: fmt.Errorf("create temp key: %w", err)} return errorCmd(fmt.Errorf("create temp key: %w", err))
} }
tmpPath := tmpFile.Name() tmpPath := tmpFile.Name()
if _, err := tmpFile.Write([]byte(keyContent)); err != nil { if _, err := tmpFile.Write([]byte(keyContent)); err != nil {
tmpFile.Close() tmpFile.Close()
os.Remove(tmpPath) os.Remove(tmpPath)
return sshExitMsg{err: fmt.Errorf("write temp key: %w", err)} return errorCmd(fmt.Errorf("write temp key: %w", err))
} }
tmpFile.Close() tmpFile.Close()
os.Chmod(tmpPath, 0600) os.Chmod(tmpPath, 0600)
args := []string{ keyArgs := append([]string{"-i", tmpPath}, sshArgs...)
"-i", tmpPath, keyArgs = append(keyArgs, target)
"-p", portStr, cmd := exec.Command("ssh", keyArgs...)
"-o", "StrictHostKeyChecking=accept-new",
"-o", "ServerAliveInterval=60",
"-o", "ServerAliveCountMax=3",
"-S", ctrlSock,
"-o", "ControlMaster=auto",
target,
}
cmd = exec.Command("ssh", args...)
if host.Auth.Password != "" { if host.Auth.Password != "" {
self, err := os.Executable() self, err := os.Executable()
@@ -94,23 +94,29 @@ func sshConnectCmd(host *models.Host, dataDir string) tea.Cmd {
env = append(env, "DISPLAY=:0") env = append(env, "DISPLAY=:0")
} }
if setsid, err := exec.LookPath("setsid"); err == nil { if setsid, err := exec.LookPath("setsid"); err == nil {
newArgs := append([]string{"ssh"}, args...) newArgs := append([]string{"ssh"}, keyArgs...)
cmd = exec.Command(setsid, newArgs...) cmd = exec.Command(setsid, newArgs...)
} }
} }
} }
} }
default:
return sshExitMsg{err: fmt.Errorf("unsupported auth type: %s", host.Auth.Type)}
}
cmd.Env = env cmd.Env = env
return tea.ExecProcess(cmd, func(err error) tea.Msg { return tea.ExecProcess(cmd, func(err error) tea.Msg {
// Cleanup ControlMaster socket os.Remove(tmpPath)
exec.Command("ssh", "-S", ctrlSock, "-O", "exit", target).Run() cleanup()
return sshExitMsg{err: err} return sshExitMsg{err: err}
}) })
default:
return errorCmd(fmt.Errorf("unsupported auth type: %s", host.Auth.Type))
}
}
// errorCmd returns a Cmd that sends an sshExitMsg with the given error.
func errorCmd(err error) tea.Cmd {
return func() tea.Msg {
return sshExitMsg{err: err}
} }
} }
+11
View File
@@ -65,6 +65,9 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, sshConnectCmd(msg.host, m.dataDir) return m, sshConnectCmd(msg.host, m.dataDir)
case sshExitMsg: case sshExitMsg:
if msg.err != nil {
m.Error = msg.err
}
return m, nil return m, nil
case openHostFormMsg: case openHostFormMsg:
@@ -203,6 +206,14 @@ func (m *Model) View() string {
return "No tabs open. Press 'q' to quit.\n" return "No tabs open. Press 'q' to quit.\n"
} }
// Pass error to host list tab for display
if m.Error != nil {
if ht := FindHostListTab(m.tabs.tabs); ht != nil {
ht.err = m.Error
}
m.Error = nil
}
return m.tabs.View() return m.tabs.View()
} }