-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.go
More file actions
65 lines (55 loc) · 1.47 KB
/
env.go
File metadata and controls
65 lines (55 loc) · 1.47 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
package tinygo
import (
"os"
"path/filepath"
"strings"
)
// GetRoot returns the TINYGOROOT for a local install, or "" if tinygo is in PATH.
func GetRoot(opts ...Option) string {
c := newConfig(opts...)
if _, err := c.lookPath("tinygo"); err == nil {
return ""
}
return filepath.Join(c.installDir, "tinygo")
}
// GetEnv returns os.Environ() + TINYGOROOT + prepended PATH for local installs.
// Safe to assign directly to exec.Command.Env.
func GetEnv(opts ...Option) []string {
root := GetRoot(opts...)
if root == "" {
return os.Environ()
}
env := os.Environ()
// We might add TINYGOROOT if not present, and we definitely update PATH.
newEnv := make([]string, 0, len(env)+1)
foundTinyRoot := false
foundPath := false
pathKey := "PATH" // Default to PATH, but will match actual case from env
for _, e := range env {
key, val, found := strings.Cut(e, "=")
if !found {
newEnv = append(newEnv, e)
continue
}
upperKey := strings.ToUpper(key)
if upperKey == "TINYGOROOT" {
newEnv = append(newEnv, key+"="+root)
foundTinyRoot = true
} else if upperKey == "PATH" {
pathKey = key
foundPath = true
binDir := filepath.Join(root, "bin")
newEnv = append(newEnv, key+"="+binDir+string(os.PathListSeparator)+val)
} else {
newEnv = append(newEnv, e)
}
}
if !foundTinyRoot {
newEnv = append(newEnv, "TINYGOROOT="+root)
}
if !foundPath {
binDir := filepath.Join(root, "bin")
newEnv = append(newEnv, pathKey+"="+binDir)
}
return newEnv
}