-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.go
More file actions
38 lines (32 loc) · 793 Bytes
/
Copy pathenvironment.go
File metadata and controls
38 lines (32 loc) · 793 Bytes
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
package main
import (
"path/filepath"
"regexp"
)
var envExpandRegexp = regexp.MustCompile(`[\$@]\(.+?\)`)
// Expand references to evironment variables using the $(var) syntax in string.
func (env *Environment) Expand(s string) string {
f := func(m string) string {
k := m[2 : len(m)-1]
v := env.Get(k)
if v == "" {
return m
}
if m[0] == '@' && !filepath.IsAbs(v) {
if t, err := filepath.Abs(filepath.Join(env.Get(".cdir"), v)); err == nil {
v = t
}
}
return v
}
return envExpandRegexp.ReplaceAllStringFunc(s, f)
}
func (env *Environment) Get(name string) string {
return (*env)[name]
}
func (env *Environment) Set(name, value string) {
(*env)[name] = value
}
func (env *Environment) SetExpanded(name, value string) {
(*env)[name] = env.Expand(value)
}