-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.go
More file actions
72 lines (66 loc) · 1.64 KB
/
Copy pathpath.go
File metadata and controls
72 lines (66 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"os"
"path/filepath"
"runtime"
"strings"
)
func configureExecutablePath() error {
home, _ := os.UserHomeDir()
path := mergeExecutablePath(os.Getenv("PATH"), defaultExecutablePaths(home))
return os.Setenv("PATH", path)
}
func defaultExecutablePaths(home string) []string {
paths := make([]string, 0, 10)
if home != "" {
paths = append(paths,
filepath.Join(home, ".local", "bin"),
filepath.Join(home, ".bun", "bin"),
filepath.Join(home, "go", "bin"),
filepath.Join(home, ".volta", "bin"),
filepath.Join(home, ".npm-global", "bin"),
)
}
switch runtime.GOOS {
case "darwin":
if home != "" {
paths = append(paths, filepath.Join(home, "Library", "pnpm"))
}
paths = append(paths, "/opt/homebrew/bin", "/usr/local/bin", "/opt/local/bin")
case "linux":
paths = append(paths, "/home/linuxbrew/.linuxbrew/bin", "/usr/local/bin", "/snap/bin")
}
return paths
}
func mergeExecutablePath(current string, extras []string) string {
paths := filepath.SplitList(current)
seen := make(map[string]struct{}, len(paths)+len(extras))
for _, path := range paths {
if key := executablePathKey(path); key != "" {
seen[key] = struct{}{}
}
}
for _, path := range extras {
key := executablePathKey(path)
if key == "" {
continue
}
if _, ok := seen[key]; ok {
continue
}
paths = append(paths, path)
seen[key] = struct{}{}
}
return strings.Join(paths, string(os.PathListSeparator))
}
func executablePathKey(path string) string {
path = strings.TrimSpace(path)
if path == "" {
return ""
}
path = filepath.Clean(path)
if runtime.GOOS == "windows" {
path = strings.ToLower(path)
}
return path
}