feat: add info command for JSON host details

Adds a jq-friendly `sshm info` subcommand with host completion and documentation, and makes home directory resolution testable for backup path tests.
This commit is contained in:
David Ibia
2026-01-12 23:53:35 +01:00
parent 58a9e6f40f
commit 7d9b794ceb
6 changed files with 572 additions and 10 deletions

View File

@@ -11,6 +11,18 @@ import (
"sync"
)
func getHomeDir() (string, error) {
home := os.Getenv("HOME")
if home != "" {
return home, nil
}
home = os.Getenv("USERPROFILE")
if home != "" {
return home, nil
}
return os.UserHomeDir()
}
// SSHHost represents an SSH host configuration
type SSHHost struct {
Name string
@@ -33,7 +45,7 @@ type SSHHost struct {
// GetDefaultSSHConfigPath returns the default SSH config path for the current platform
func GetDefaultSSHConfigPath() (string, error) {
homeDir, err := os.UserHomeDir()
homeDir, err := getHomeDir()
if err != nil {
return "", err
}
@@ -49,7 +61,7 @@ func GetDefaultSSHConfigPath() (string, error) {
// GetSSHMConfigDir returns the SSHM config directory
func GetSSHMConfigDir() (string, error) {
homeDir, err := os.UserHomeDir()
homeDir, err := getHomeDir()
if err != nil {
return "", err
}
@@ -88,7 +100,7 @@ func GetSSHMBackupDir() (string, error) {
// GetSSHDirectory returns the .ssh directory path
func GetSSHDirectory() (string, error) {
homeDir, err := os.UserHomeDir()
homeDir, err := getHomeDir()
if err != nil {
return "", err
}
@@ -386,7 +398,7 @@ func parseSSHConfigFileWithProcessedFiles(configPath string, processedFiles map[
func processIncludeDirective(pattern string, baseConfigPath string, processedFiles map[string]bool) ([]SSHHost, error) {
// Expand tilde to home directory
if strings.HasPrefix(pattern, "~") {
homeDir, err := os.UserHomeDir()
homeDir, err := getHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to get home directory: %w", err)
}
@@ -918,7 +930,7 @@ func quickHostSearchInFile(hostName string, configPath string, processedFiles ma
func quickSearchInclude(hostName, pattern, baseConfigPath string, processedFiles map[string]bool) (bool, error) {
// Expand tilde to home directory
if strings.HasPrefix(pattern, "~") {
homeDir, err := os.UserHomeDir()
homeDir, err := getHomeDir()
if err != nil {
return false, fmt.Errorf("failed to get home directory: %w", err)
}

View File

@@ -456,15 +456,21 @@ func TestBackupConfigToSSHMDirectory(t *testing.T) {
// Create temporary directory for test files
tempDir := t.TempDir()
// Override the home directory for this test
originalHome := os.Getenv("HOME")
if originalHome == "" {
originalHome = os.Getenv("USERPROFILE") // Windows
originalHome = os.Getenv("USERPROFILE")
}
originalXDG := os.Getenv("XDG_CONFIG_HOME")
originalAppData := os.Getenv("APPDATA")
// Set test home directory
os.Setenv("HOME", tempDir)
defer os.Setenv("HOME", originalHome)
os.Setenv("XDG_CONFIG_HOME", tempDir)
os.Setenv("APPDATA", tempDir)
defer func() {
os.Setenv("HOME", originalHome)
os.Setenv("XDG_CONFIG_HOME", originalXDG)
os.Setenv("APPDATA", originalAppData)
}()
// Create a test SSH config file
sshDir := filepath.Join(tempDir, ".ssh")