Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathenv.go
More file actions
Latest commit
120 lines (114 loc) · 2.54 KB
/
Copy pathenv.go
File metadata and controls
120 lines (114 loc) · 2.54 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package devstatscode
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
"time"
)
vargEnvMapmap[string]string=make(map[string]string)
// EnvSyncer - support auto updating env variables via "env.env" file
funcEnvSyncer() {
for {
time.Sleep(30*time.Second)
UpdateEnv(true)
}
}
// UpdateEnv - update (eventually) env using env.env file
funcUpdateEnv(useLogbool) {
ef, err:=os.Open("env.env")
iferr!=nil {
return
}
deferef.Close()
sc:=bufio.NewScanner(ef)
forsc.Scan() {
line:=sc.Text()
ary:=strings.Split(line, "=")
iflen(ary) <2 {
continue
}
k:=strings.TrimSpace(ary[0])
ifk=="" {
continue
}
// v := strings.TrimSpace(ary[1])
v:=strings.TrimSpace(strings.Join(ary[1:], "="))
os.Setenv(k, v)
cv, ok:=gEnvMap[k]
if!ok||cv!=v {
ifuseLog {
ifIsLogInitialized() {
Printf("new environment overwrite: '%s' --> '%s'\n", k, v)
}
} else {
fmt.Printf("new environment overwrite: '%s' --> '%s'\n", k, v)
}
}
gEnvMap[k] =v
}
}
// EnvReplace - replace all environment variables starting with "prefix"
// with contents of variables with "suffix" added - if defined
// If prefix is "DB_" and suffix is "_SRC" then:
// So if there is "DB_HOST_SRC" variable defined - it will replace "DB_HOST" and so on
funcEnvReplace(prefix, suffixstring) map[string]string {
ifsuffix=="" {
returnmap[string]string{}
}
oldEnv:=make(map[string]string)
varenviron []string
for_, e:=rangeos.Environ() {
environ=append(environ, e)
}
sort.Strings(environ)
pLen:=len(prefix)
for_, e:=rangeenviron {
l:=pLen
eLen:=len(e)
ifl>eLen {
l=eLen
}
ifpLen==0||e[0:l] ==prefix {
pair:=strings.Split(e, "=")
eSuff:=os.Getenv(pair[0] +suffix)
ifeSuff!="" {
oldEnv[pair[0]] =pair[1]
FatalOnError(os.Setenv(pair[0], eSuff))
}
}
}
sLen:=len(suffix)
for_, e:=rangeenviron {
pair:=strings.Split(e, "=")
eLen:=len(pair[0])
lS:=eLen-sLen
iflS<=0 {
continue
}
lP:=pLen
iflP>eLen {
lP=eLen
}
if (pLen==0||e[0:lP] ==prefix) &&pair[0][lS:] ==suffix {
eName:=pair[0][:lS]
_, ok:=oldEnv[eName]
if!ok {
oldEnv[eName] =Unset
FatalOnError(os.Setenv(eName, pair[1]))
}
}
}
returnoldEnv
}
// EnvRestore - restores all environment variables given in the map
funcEnvRestore(envmap[string]string) {
forenvName, envValue:=rangeenv {
ifenvValue==Unset {
FatalOnError(os.Unsetenv(envName))
} else {
FatalOnError(os.Setenv(envName, envValue))
}
}
}