addToSSHConfig adds fssh agent configuration to SSH config
()
| 12 | |
| 13 | // addToSSHConfig adds fssh agent configuration to SSH config |
| 14 | func addToSSHConfig() error { |
| 15 | home, err := os.UserHomeDir() |
| 16 | if err != nil { |
| 17 | return fmt.Errorf("failed to get home directory: %w", err) |
| 18 | } |
| 19 | |
| 20 | sshConfigPath := filepath.Join(home, ".ssh", "config") |
| 21 | socketPath := filepath.Join(home, ".fssh", "agent.sock") |
| 22 | |
| 23 | // Check if config file exists |
| 24 | var existingContent []byte |
| 25 | if _, err := os.Stat(sshConfigPath); err == nil { |
| 26 | existingContent, err = os.ReadFile(sshConfigPath) |
| 27 | if err != nil { |
| 28 | return fmt.Errorf("failed to read SSH config: %w", err) |
| 29 | } |
| 30 | |
| 31 | // Check if fssh agent is already configured |
| 32 | if strings.Contains(string(existingContent), socketPath) { |
| 33 | fmt.Println("✓ SSH config already contains fssh agent configuration") |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | // Check if there's already a global IdentityAgent |
| 38 | if strings.Contains(string(existingContent), "IdentityAgent") { |
| 39 | fmt.Println("⚠️ SSH config already contains IdentityAgent configuration") |
| 40 | if !otp.PromptConfirm("Do you want to add fssh agent configuration anyway?") { |
| 41 | fmt.Println("Skipped SSH config update") |
| 42 | displayManualInstructions(socketPath) |
| 43 | return nil |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Ask user if they want to update SSH config |
| 49 | fmt.Println("To use fssh agent automatically, we can add configuration to ~/.ssh/config") |
| 50 | if !otp.PromptConfirm("Do you want to update SSH config?") { |
| 51 | fmt.Println("Skipped SSH config update") |
| 52 | displayManualInstructions(socketPath) |
| 53 | return nil |
| 54 | } |
| 55 | |
| 56 | // Create backup |
| 57 | if len(existingContent) > 0 { |
| 58 | backupPath := fmt.Sprintf("%s.bak.%d", sshConfigPath, time.Now().Unix()) |
| 59 | if err := os.WriteFile(backupPath, existingContent, 0600); err != nil { |
| 60 | return fmt.Errorf("failed to create backup: %w", err) |
| 61 | } |
| 62 | fmt.Printf("✓ Created backup: %s\n", backupPath) |
| 63 | } |
| 64 | |
| 65 | // Prepare new configuration |
| 66 | fsshConfig := fmt.Sprintf(`# fssh agent configuration |
| 67 | Host * |
| 68 | IdentityAgent %s |
| 69 | |
| 70 | `, socketPath) |
| 71 |
no test coverage detected