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
|
package env
import (
"errors"
"fmt"
"path/filepath"
"strings"
)
func formatENV() (string, error) {
if envPB == nil {
return "", errors.New("envPB not initialized")
}
var out string
uniques := make(map[string]*Key) // check to make sure there are no duplicate entries
// todo: remove this duplicate entry check once duplicates aren't a problem anymore
for c := range envPB.IterAll() {
key := strings.ToLower(c.Var)
if len(strings.Fields(key)) != 1 {
// fmt.Println("dropping invalid key = ", c.Var, "value =", c.Value)
continue
}
found := findByLower(key)
if found == nil {
// fmt.Println("findByKey() got nil for key:", key)
}
// todo: warn about duplicates?
uniques[key] = found
}
for key, c := range uniques {
_ = key
if c == nil {
// fmt.Println("key has nil c", key)
continue
}
if c.Global != "" {
// don't write out anything defined globally
continue
}
// turns "/home/user/" into ~/
relpath, err := filepath.Rel(envPB.HomeDir, c.Value)
if err == nil {
c.Value = filepath.Join("~", relpath)
}
line := fmt.Sprintf("%s=%s", c.Var, c.Value)
out += line + "\n"
}
return out, nil
}
func findByLower(lookingFor string) *Key {
for c := range envPB.IterAll() {
if strings.ToLower(c.Var) == strings.ToLower(lookingFor) {
return c
}
}
return nil
}
|