ensureBinaryInstalled installs fssh to /usr/local/bin if not already installed
()
| 11 | |
| 12 | // ensureBinaryInstalled installs fssh to /usr/local/bin if not already installed |
| 13 | func ensureBinaryInstalled() error { |
| 14 | targetPath := "/usr/local/bin/fssh" |
| 15 | |
| 16 | // Get current executable path |
| 17 | currentPath, err := os.Executable() |
| 18 | if err != nil { |
| 19 | return fmt.Errorf("failed to get executable path: %w", err) |
| 20 | } |
| 21 | |
| 22 | // Resolve symlinks |
| 23 | currentPath, err = filepath.EvalSymlinks(currentPath) |
| 24 | if err != nil { |
| 25 | return fmt.Errorf("failed to resolve symlinks: %w", err) |
| 26 | } |
| 27 | |
| 28 | // Check if already installed at target location |
| 29 | if _, err := os.Stat(targetPath); err == nil { |
| 30 | // Target exists, check if it's the same file |
| 31 | existingPath, err := filepath.EvalSymlinks(targetPath) |
| 32 | if err == nil && existingPath == currentPath { |
| 33 | fmt.Printf("✓ fssh is already installed at %s\n", targetPath) |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | // Different file, ask if user wants to replace |
| 38 | fmt.Printf("⚠️ fssh already exists at %s\n", targetPath) |
| 39 | if !otp.PromptConfirm("Do you want to replace it?") { |
| 40 | fmt.Println("Skipped binary installation") |
| 41 | return nil |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // If we're already running from /usr/local/bin, no need to install |
| 46 | if currentPath == targetPath { |
| 47 | fmt.Printf("✓ Already running from %s\n", targetPath) |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | // Inform user about sudo requirement |
| 52 | fmt.Printf("Installing fssh to %s (requires sudo)...\n", targetPath) |
| 53 | |
| 54 | // Ensure /usr/local/bin exists |
| 55 | if err := runSudoCommand("mkdir", "-p", "/usr/local/bin"); err != nil { |
| 56 | return fmt.Errorf("failed to create /usr/local/bin: %w", err) |
| 57 | } |
| 58 | |
| 59 | // Copy binary |
| 60 | if err := runSudoCommand("cp", currentPath, targetPath); err != nil { |
| 61 | return fmt.Errorf("failed to copy binary: %w", err) |
| 62 | } |
| 63 | |
| 64 | // Set permissions |
| 65 | if err := runSudoCommand("chmod", "755", targetPath); err != nil { |
| 66 | return fmt.Errorf("failed to set permissions: %w", err) |
| 67 | } |
| 68 | |
| 69 | fmt.Printf("✓ Successfully installed fssh to %s\n", targetPath) |
| 70 | return nil |
no test coverage detected