forked from slene/margo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_env.go
77 lines (66 loc) · 1.18 KB
/
m_env.go
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
73
74
75
76
77
package main
import (
"os"
"path/filepath"
"runtime"
"strings"
)
var (
mEnvVars = defaultEnv()
)
type mEnv struct {
List []string
Gopath string
}
func mEnvGetEnv(k string) string {
v := os.Getenv(k)
if v == "" {
v = mEnvVars[k]
}
return v
}
func (m *mEnv) Call() (interface{}, string) {
env := map[string]string{}
addLibPath := false
if len(m.List) == 0 {
addLibPath = true
for k, v := range mEnvVars {
env[k] = v
}
for _, s := range os.Environ() {
p := strings.SplitN(s, "=", 2)
if len(p) == 2 {
env[p[0]] = p[1]
} else {
env[p[0]] = ""
}
}
} else {
for _, k := range m.List {
if k == "GOSUBLIME_LIBPATH" {
addLibPath = true
} else {
env[k] = mEnvGetEnv(k)
}
}
}
if addLibPath {
p := []string{}
sep := string(os.PathListSeparator)
osArch := runtime.GOOS + "_" + runtime.GOARCH
gpath := m.Gopath
if gpath == "" {
gpath = mEnvGetEnv("GOPATH")
}
for _, s := range strings.Split(gpath, sep) {
p = append(p, filepath.Join(s, "pkg", osArch))
}
env["GOSUBLIME_LIBPATH"] = strings.Join(p, sep)
}
return env, ""
}
func init() {
registry.Register("env", func(_ *Broker) Caller {
return &mEnv{}
})
}