Files
go-ssh-app/tui/interface.go
2025-01-01 15:03:03 +01:00

84 lines
2.1 KiB
Go

package tui
import (
"log"
"tui-ssh-app/ssh"
"github.com/rivo/tview"
)
func StartTUI() {
app := tview.NewApplication()
// Input fields for SSH configuration
userField := tview.NewInputField().SetLabel("User: ")
hostField := tview.NewInputField().SetLabel("Host: ")
portField := tview.NewInputField().SetLabel("Port: ").SetText("22")
passwordField := tview.NewInputField().SetLabel("Password: ")
privateKeyField := tview.NewInputField().SetLabel("Private Key Path: ")
// Output box for connection status
outputBox := tview.NewTextView().SetDynamicColors(true)
// Layout
form := tview.NewForm().
AddFormItem(userField).
AddFormItem(hostField).
AddFormItem(portField).
AddFormItem(passwordField).
AddFormItem(privateKeyField).
AddButton("Connect", func() {
// Read inputs
user := userField.GetText()
host := hostField.GetText()
port := portField.GetText()
password := passwordField.GetText()
privateKey := privateKeyField.GetText()
// Validate inputs
if user == "" || host == "" || port == "" {
outputBox.SetText("[red]Error: All fields except Password/Private Key are required!").ScrollToEnd()
return
}
// Display connection status
outputBox.SetText("[yellow]Connecting...").ScrollToEnd()
// Perform SSH connection in a goroutine to avoid blocking the TUI
go func() {
err := ssh.ConnectSSH(ssh.SSHConfig{
User: user,
Host: host,
Port: port,
Password: password,
PrivateKey: privateKey,
})
if err != nil {
log.Printf("SSH connection failed: %v", err)
app.QueueUpdateDraw(func() {
outputBox.SetText("[red]Connection failed: " + err.Error()).ScrollToEnd()
})
return
}
app.QueueUpdateDraw(func() {
outputBox.SetText("[green]Connection successful!").ScrollToEnd()
})
}()
}).
AddButton("Quit", func() {
app.Stop()
})
layout := tview.NewFlex().
SetDirection(tview.FlexRow).
AddItem(form, 0, 1, true).
AddItem(outputBox, 0, 1, false)
// Run the application
if err := app.SetRoot(layout, true).Run(); err != nil {
log.Fatalf("Error running application: %v", err)
}
}