forked from digisan/gotk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
129 lines (108 loc) · 2.79 KB
/
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
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
121
122
123
124
125
126
127
128
129
package env
import (
"fmt"
"log"
"os"
"strings"
. "github.com/digisan/go-generics"
fd "github.com/digisan/gotk/file-dir"
"github.com/digisan/gotk/strs"
)
// ChunkToMap :
func ChunkToMap(fPath, markStart, markEnd, sep string, env, pathVal2abs bool) map[string]string {
m := make(map[string]string)
proc := false
_, err := fd.FileLineScan(fPath, func(ln string) (bool, string) {
ln = strings.Trim(ln, " \t")
if !strings.HasPrefix(ln, "#") {
ln = strings.Trim(strings.SplitN(ln, "#", 2)[0], " \t") // support comment
}
if ln == markStart && !proc {
proc = true
return false, ""
}
if ln == markEnd && proc {
proc = false
return false, ""
}
if proc && strings.Contains(ln, sep) {
ss := strings.SplitN(ln, sep, 2)
ss[0] = strings.Trim(ss[0], " \t")
ss[1] = strings.Trim(ss[1], " \t")
m["$"+ss[0]] = ss[1]
}
return false, ""
}, "")
if err != nil {
log.Fatalf("%v", err)
}
keyVars, _ := MapToKVs(m, func(i string, j string) bool { return len(i) > len(j) }, nil)
// replace '$XYZ $XY $X' to its value
AGAIN1:
for key, value := range m {
for _, variable := range keyVars {
if strings.Contains(value, variable) {
m[key] = strings.ReplaceAll(value, variable, m[variable])
goto AGAIN1
}
}
}
// remove map each key's prefix '$'
AGAIN2:
for key, value := range m {
if strings.HasPrefix(key, "$") {
m[key[1:]] = value
delete(m, key)
goto AGAIN2
}
}
keyVars, _ = MapToKVs(m, func(i string, j string) bool { return len(i) > len(j) }, nil)
// replace '${XYZ} ${XY} ${X}' to its value
AGAIN3:
for key, value := range m {
for _, variable := range keyVars {
valued := fmt.Sprintf("${%s}", variable)
if strings.Contains(value, valued) {
m[key] = strings.ReplaceAll(value, valued, m[variable])
goto AGAIN3
}
}
}
if pathVal2abs {
for key, value := range m {
if strs.HasAnyPrefix(value, "~/", "./", "../") {
absPath, _ := fd.AbsPath(value, false)
m[key] = absPath
}
}
}
if env {
for key, value := range m {
os.Setenv(key, value)
}
}
return m
}
// EnvValued : if m is empty, environment variables as m apply to s
func EnvValued(s string, m map[string]string) (valStr string) {
if len(m) == 0 {
m = make(map[string]string)
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
m[pair[0]] = pair[1]
}
}
keyVars, _ := MapToKVs(m, func(i string, j string) bool { return len(i) > len(j) }, nil)
valStr = s
// replace '$XYZ $XY $X' to its value
for _, variable := range keyVars {
valued := fmt.Sprintf("$%s", variable)
valStr = strings.ReplaceAll(valStr, valued, m[variable])
}
// replace '${XYZ} ${XY} ${X}' to its value
for _, variable := range keyVars {
valued := fmt.Sprintf("${%s}", variable)
valStr = strings.ReplaceAll(valStr, valued, m[variable])
}
return
}