diff --git a/internal/connector/ssh/cmd_nix.go b/internal/connector/ssh/cmd_nix.go deleted file mode 100644 index 95e3f70..0000000 --- a/internal/connector/ssh/cmd_nix.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build !windows - -package ssh - -func SSHCmd() []string { - return []string{"ssh"} -} diff --git a/internal/connector/ssh/cmd_win.go b/internal/connector/ssh/cmd_win.go deleted file mode 100644 index 5a604c7..0000000 --- a/internal/connector/ssh/cmd_win.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build windows - -package ssh - -func SSHCmd() []string { - return []string{"cmd", "/c", "ssh"} -} diff --git a/internal/connector/ssh/connect.go b/internal/connector/ssh/connect.go deleted file mode 100644 index f16fe2d..0000000 --- a/internal/connector/ssh/connect.go +++ /dev/null @@ -1,41 +0,0 @@ -package ssh - -import ( - "os/exec" - "strings" - - "github.com/grafviktor/goto/internal/model" -) - -const ( - optionPrivateKey = "-i" - optionRemotePort = "-p" - optionLoginName = "-l" -) - -func ConnectCmd(h model.Host) *exec.Cmd { - sshCmd := SSHCmd() - args := []string{} - - privateKeyPath := strings.Trim(h.PrivateKeyPath, " ") - if privateKeyPath != "" { - args = append(args, optionPrivateKey) - args = append(args, privateKeyPath) - } - - remotePort := strings.Trim(h.RemotePort, " ") - if remotePort != "" { - args = append(args, optionRemotePort) - args = append(args, remotePort) - } - - loginName := strings.Trim(h.LoginName, " ") - if loginName != "" { - args = append(args, optionLoginName) - args = append(args, loginName) - } - - args = append(sshCmd[1:], args...) - - return exec.Command(sshCmd[0], append(args, h.Address)...) -} diff --git a/internal/ui/component/hostlist/list.go b/internal/ui/component/hostlist/list.go index ec61ea1..8b7b57a 100644 --- a/internal/ui/component/hostlist/list.go +++ b/internal/ui/component/hostlist/list.go @@ -11,11 +11,12 @@ import ( "github.com/charmbracelet/lipgloss" "golang.org/x/exp/slices" - "github.com/grafviktor/goto/internal/connector/ssh" "github.com/grafviktor/goto/internal/model" "github.com/grafviktor/goto/internal/state" "github.com/grafviktor/goto/internal/storage" "github.com/grafviktor/goto/internal/ui/message" + "github.com/grafviktor/goto/internal/utils" + "github.com/grafviktor/goto/internal/utils/ssh" ) var ( @@ -236,24 +237,10 @@ func (m ListModel) executeCmd(_ tea.Msg) (ListModel, tea.Cmd) { return m, message.TeaCmd(msgErrorOccured{err}) } - connectSSHCmd := ssh.ConnectCmd(host) - return m, tea.ExecProcess(connectSSHCmd, func(err error) tea.Msg { - // return m, tea.ExecProcess(exec.Command("ping", "-t", "localhost"), func(err error) tea.Msg { + command := ssh.ConstructCMD(ssh.BaseCMD(), utils.HostModelToOptionsAdaptor(host)) + process := utils.BuildProcess(command) + return m, tea.ExecProcess(process, func(err error) tea.Msg { if err != nil { - /* - * That's to attempt to restore windows terminal when user pressed ctrl+c when using SSH connection. - * It works, when we close SSH, however it breaks all subsequent ssh connections - */ - /* - if runtime.GOOS == "windows" { - // If try to connect to a remote host and instead of typing a password, type "CTRL+C", - // the application UI will be broken. Flushing terminal window, helps to resolve the problem. - cmd := exec.Command("cmd", "/c", "cls") - cmd.Stdout = os.Stdout - cmd.Run() - } - */ - return msgErrorOccured{err} } diff --git a/internal/utils/ssh/cmd_nix.go b/internal/utils/ssh/cmd_nix.go new file mode 100644 index 0000000..8135a43 --- /dev/null +++ b/internal/utils/ssh/cmd_nix.go @@ -0,0 +1,7 @@ +//go:build !windows + +package ssh + +func BaseCMD() string { + return "ssh" +} diff --git a/internal/utils/ssh/cmd_win.go b/internal/utils/ssh/cmd_win.go new file mode 100644 index 0000000..fa1ee0c --- /dev/null +++ b/internal/utils/ssh/cmd_win.go @@ -0,0 +1,7 @@ +//go:build windows + +package ssh + +func BaseCMD() string { + return "cmd /c ssh" +}