feat: implement connect host command with native SSH

- Add connect command with native SSH (default) and direct Go SSH (--direct) modes
- Support host lookup by name or ID
- Build SSH arguments for system SSH client
- Include timeout configuration flag
- Add comprehensive tests for command and SSH arg building
- Update project state documentation
This commit is contained in:
swanadiva
2026-06-23 13:48:05 +07:00
parent 4087c5fdc7
commit 368b7cdadb
3 changed files with 278 additions and 10 deletions
+161
View File
@@ -0,0 +1,161 @@
package main
import (
"context"
"fmt"
"os"
"os/exec"
"time"
"github.com/spf13/cobra"
"git.tukangketik.id/swanadiva/hostkeeper/internal/errors"
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/config"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/ssh"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/storage"
)
var (
connectTimeout int
connectDirect bool
)
// connectCmd represents the connect command
var connectCmd = &cobra.Command{
Use: "connect <host-name-or-id>",
Short: "Connect to a saved SSH host",
Long: `Connect to a saved SSH host using native SSH client with stored credentials.
Examples:
# Connect to a host by name
hostkeeper connect myserver
# Connect with a specific timeout
hostkeeper connect myserver --timeout 60
# Use Go SSH client (direct mode) instead of system SSH
hostkeeper connect myserver --direct`,
Args: cobra.ExactArgs(1),
RunE: runConnect,
}
func init() {
connectCmd.Flags().IntVar(&connectTimeout, "timeout", 30, "Connection timeout in seconds")
connectCmd.Flags().BoolVar(&connectDirect, "direct", false, "Use direct SSH instead of native client")
rootCmd.AddCommand(connectCmd)
}
func runConnect(cmd *cobra.Command, args []string) error {
hostIdentifier := args[0]
cfg := appCfg
if cfg == nil {
var err error
cfg, err = config.New()
if err != nil {
return fmt.Errorf("failed to initialize config: %w", err)
}
}
// Initialize storage
store, err := storage.NewJSONStorage(cfg.GetDataDir())
if err != nil {
return fmt.Errorf("failed to initialize storage: %w", err)
}
ctx := context.Background()
// Find host by name or ID
host, err := findHost(ctx, store, hostIdentifier)
if err != nil {
return err
}
fmt.Printf("Connecting to %s (%s@%s:%d)...\n", host.Name, host.Username, host.Hostname, host.Port)
// Choose connection method
if connectDirect {
return connectDirectSSH(host)
}
return connectWithNativeSSH(host)
}
// findHost finds a host by ID first, then by name
func findHost(ctx context.Context, store storage.Storage, identifier string) (*models.Host, error) {
// Try to find by ID first
host, err := store.GetHost(ctx, identifier)
if err == nil {
return host, nil
}
// Try to find by name
hosts, err := store.ListHosts(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list hosts: %w", err)
}
for _, h := range hosts {
if h.Name == identifier {
return h, nil
}
}
// Host not found, provide helpful error
return nil, fmt.Errorf("host '%s' not found. Use 'hostkeeper list' to see available hosts", identifier)
}
// connectWithNativeSSH uses the system SSH client
func connectWithNativeSSH(host *models.Host) error {
sshArgs := buildSSHArgs(host)
cmd := exec.Command("ssh", sshArgs...)
// Set up standard I/O for interactive session
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Execute SSH command
if err := cmd.Run(); err != nil {
return errors.HandleSSHError(err)
}
return nil
}
// connectDirectSSH uses Go SSH client
func connectDirectSSH(host *models.Host) error {
timeout := time.Duration(connectTimeout) * time.Second
client := ssh.NewClient(host, timeout)
ctx := context.Background()
if err := client.Connect(ctx); err != nil {
return err
}
defer client.Close()
fmt.Printf("Connected to %s\n", host.Name)
fmt.Println("Interactive shell not yet implemented in direct mode")
fmt.Println("Use --direct=false (default) for native SSH experience")
return nil
}
// buildSSHArgs builds SSH command arguments for the system SSH client
func buildSSHArgs(host *models.Host) []string {
var args []string
// Add port if not default
if host.Port != 22 && host.Port != 0 {
args = append(args, "-p", fmt.Sprintf("%d", host.Port))
}
// Add connection string
connectionString := fmt.Sprintf("%s@%s", host.Username, host.Hostname)
args = append(args, connectionString)
return args
}
+95
View File
@@ -0,0 +1,95 @@
package main
import (
"testing"
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
)
func TestConnectCommandExists(t *testing.T) {
if connectCmd == nil {
t.Fatal("connectCmd should not be nil")
}
if connectCmd.Use != "connect <host-name-or-id>" {
t.Errorf("expected Use 'connect <host-name-or-id>', got '%s'", connectCmd.Use)
}
if connectCmd.Short == "" {
t.Error("Short description should not be empty")
}
}
func TestConnectCommandFlags(t *testing.T) {
expectedFlags := []string{"timeout", "direct"}
for _, flagName := range expectedFlags {
if connectCmd.Flags().Lookup(flagName) == nil {
t.Errorf("flag '%s' should be defined", flagName)
}
}
}
func TestConnectArgs(t *testing.T) {
if connectCmd.Args == nil {
t.Error("Args validator should not be nil")
}
}
func TestBuildSSHArgs(t *testing.T) {
tests := []struct {
name string
host *models.Host
want []string
}{
{
name: "default port 22",
host: &models.Host{
Name: "test",
Hostname: "192.168.1.1",
Port: 22,
Username: "admin",
},
want: []string{"admin@192.168.1.1"},
},
{
name: "non-default port 2222",
host: &models.Host{
Name: "test",
Hostname: "192.168.1.1",
Port: 2222,
Username: "admin",
},
want: []string{"-p", "2222", "admin@192.168.1.1"},
},
{
name: "key auth with KeyID",
host: &models.Host{
Name: "test",
Hostname: "10.0.0.1",
Port: 22,
Username: "root",
Auth: models.AuthConfig{
Type: "key",
KeyID: "~/.ssh/id_rsa",
},
},
want: []string{"root@10.0.0.1"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := buildSSHArgs(tt.host)
if len(got) != len(tt.want) {
t.Errorf("buildSSHArgs() = %v, want %v", got, tt.want)
return
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("buildSSHArgs() = %v, want %v", got, tt.want)
return
}
}
})
}
}