DetectShell detects the current shell from environment variables
()
| 33 | |
| 34 | // DetectShell detects the current shell from environment variables |
| 35 | func DetectShell() ShellType { |
| 36 | shellCompletionLog.Print("Detecting current shell") |
| 37 | |
| 38 | // Check shell-specific version variables first (most reliable) |
| 39 | if os.Getenv("ZSH_VERSION") != "" { |
| 40 | shellCompletionLog.Print("Detected zsh from ZSH_VERSION") |
| 41 | return ShellZsh |
| 42 | } |
| 43 | if os.Getenv("BASH_VERSION") != "" { |
| 44 | shellCompletionLog.Print("Detected bash from BASH_VERSION") |
| 45 | return ShellBash |
| 46 | } |
| 47 | if os.Getenv("FISH_VERSION") != "" { |
| 48 | shellCompletionLog.Print("Detected fish from FISH_VERSION") |
| 49 | return ShellFish |
| 50 | } |
| 51 | |
| 52 | // Fall back to $SHELL environment variable |
| 53 | shell := os.Getenv("SHELL") |
| 54 | if shell == "" { |
| 55 | shellCompletionLog.Print("SHELL environment variable not set, checking platform") |
| 56 | // On Windows, check for PowerShell |
| 57 | if runtime.GOOS == "windows" { |
| 58 | shellCompletionLog.Print("Detected Windows, assuming PowerShell") |
| 59 | return ShellPowerShell |
| 60 | } |
| 61 | shellCompletionLog.Print("Could not detect shell") |
| 62 | return ShellUnknown |
| 63 | } |
| 64 | |
| 65 | shellCompletionLog.Printf("SHELL environment variable: %s", shell) |
| 66 | |
| 67 | // Extract shell name from path |
| 68 | shellName := filepath.Base(shell) |
| 69 | shellCompletionLog.Printf("Shell base name: %s", shellName) |
| 70 | |
| 71 | switch { |
| 72 | case strings.Contains(shellName, "bash"): |
| 73 | shellCompletionLog.Print("Detected bash from SHELL") |
| 74 | return ShellBash |
| 75 | case strings.Contains(shellName, "zsh"): |
| 76 | shellCompletionLog.Print("Detected zsh from SHELL") |
| 77 | return ShellZsh |
| 78 | case strings.Contains(shellName, "fish"): |
| 79 | shellCompletionLog.Print("Detected fish from SHELL") |
| 80 | return ShellFish |
| 81 | case strings.Contains(shellName, "pwsh") || strings.Contains(shellName, "powershell"): |
| 82 | shellCompletionLog.Print("Detected PowerShell from SHELL") |
| 83 | return ShellPowerShell |
| 84 | default: |
| 85 | shellCompletionLog.Printf("Unknown shell: %s", shellName) |
| 86 | return ShellUnknown |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // InstallShellCompletion installs shell completion for the detected shell |
| 91 | func InstallShellCompletion(verbose bool, rootCmd CommandProvider) error { |