update go ssh app
This commit is contained in:
@@ -1,27 +1,83 @@
|
||||
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().
|
||||
AddInputField("User", "", 20, nil, nil).
|
||||
AddInputField("Host", "", 20, nil, nil).
|
||||
AddInputField("Port", "22", 5, nil, nil).
|
||||
AddPasswordField("Password", "", 20, '*', nil).
|
||||
AddInputField("Private Key", "", 20, nil, nil).
|
||||
AddFormItem(userField).
|
||||
AddFormItem(hostField).
|
||||
AddFormItem(portField).
|
||||
AddFormItem(passwordField).
|
||||
AddFormItem(privateKeyField).
|
||||
AddButton("Connect", func() {
|
||||
// Handle SSH connection logic
|
||||
// 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()
|
||||
})
|
||||
|
||||
form.SetBorder(true).SetTitle("SSH Connection").SetTitleAlign(tview.AlignLeft)
|
||||
if err := app.SetRoot(form, true).Run(); err != nil {
|
||||
panic(err)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user