|
| 1 | +package apply |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +func resolveExecutableBySearch(name string) (string, error) { |
| 12 | + for _, candidate := range executableSearchCandidates(name) { |
| 13 | + if regularFile(candidate) { |
| 14 | + return candidate, nil |
| 15 | + } |
| 16 | + } |
| 17 | + return "", fmt.Errorf("executable not found: %s", name) |
| 18 | +} |
| 19 | + |
| 20 | +func executableSearchCandidates(name string) []string { |
| 21 | + name = strings.TrimSpace(name) |
| 22 | + if name == "" { |
| 23 | + return nil |
| 24 | + } |
| 25 | + var out []string |
| 26 | + seen := map[string]struct{}{} |
| 27 | + add := func(path string) { |
| 28 | + path = strings.TrimSpace(path) |
| 29 | + if path == "" { |
| 30 | + return |
| 31 | + } |
| 32 | + cleaned := filepath.Clean(path) |
| 33 | + if _, ok := seen[cleaned]; ok { |
| 34 | + return |
| 35 | + } |
| 36 | + seen[cleaned] = struct{}{} |
| 37 | + out = append(out, cleaned) |
| 38 | + } |
| 39 | + for _, dir := range executableSearchDirs() { |
| 40 | + for _, file := range executableCommandNames(name) { |
| 41 | + add(filepath.Join(dir, file)) |
| 42 | + } |
| 43 | + } |
| 44 | + return out |
| 45 | +} |
| 46 | + |
| 47 | +func executableCommandNames(name string) []string { |
| 48 | + name = strings.TrimSpace(name) |
| 49 | + if name == "" { |
| 50 | + return nil |
| 51 | + } |
| 52 | + if runtime.GOOS != "windows" { |
| 53 | + return []string{name} |
| 54 | + } |
| 55 | + lower := strings.ToLower(name) |
| 56 | + out := []string{name} |
| 57 | + for _, ext := range []string{".exe", ".cmd", ".ps1", ".bat"} { |
| 58 | + if strings.HasSuffix(lower, ext) { |
| 59 | + continue |
| 60 | + } |
| 61 | + out = append(out, name+ext) |
| 62 | + } |
| 63 | + return out |
| 64 | +} |
| 65 | + |
| 66 | +func executableSearchDirs() []string { |
| 67 | + var dirs []string |
| 68 | + seen := map[string]struct{}{} |
| 69 | + add := func(dir string) { |
| 70 | + dir = strings.TrimSpace(dir) |
| 71 | + if dir == "" { |
| 72 | + return |
| 73 | + } |
| 74 | + cleaned := filepath.Clean(dir) |
| 75 | + if _, ok := seen[cleaned]; ok { |
| 76 | + return |
| 77 | + } |
| 78 | + seen[cleaned] = struct{}{} |
| 79 | + dirs = append(dirs, cleaned) |
| 80 | + } |
| 81 | + for _, dir := range filepath.SplitList(os.Getenv("PATH")) { |
| 82 | + add(dir) |
| 83 | + } |
| 84 | + home, _ := os.UserHomeDir() |
| 85 | + if strings.TrimSpace(home) != "" { |
| 86 | + home = filepath.Clean(home) |
| 87 | + add(filepath.Join(home, "bin")) |
| 88 | + add(filepath.Join(home, ".local", "bin")) |
| 89 | + add(filepath.Join(home, ".npm-global", "bin")) |
| 90 | + add(filepath.Join(home, ".volta", "bin")) |
| 91 | + add(filepath.Join(home, ".local", "share", "fnm", "current", "bin")) |
| 92 | + add(filepath.Join(home, ".fnm", "current", "bin")) |
| 93 | + add(filepath.Join(home, "Library", "pnpm")) |
| 94 | + add(filepath.Join(home, ".opencode", "bin")) |
| 95 | + } |
| 96 | + for _, dir := range platformExecutableSearchDirs(home) { |
| 97 | + add(dir) |
| 98 | + } |
| 99 | + for _, dir := range loginShellExecutableSearchDirs() { |
| 100 | + add(dir) |
| 101 | + } |
| 102 | + if runtime.GOOS != "windows" { |
| 103 | + add("/opt/homebrew/bin") |
| 104 | + add("/opt/homebrew/sbin") |
| 105 | + add("/usr/local/bin") |
| 106 | + add("/usr/local/sbin") |
| 107 | + add("/usr/bin") |
| 108 | + add("/bin") |
| 109 | + } |
| 110 | + return dirs |
| 111 | +} |
0 commit comments