This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.go
executable file
·167 lines (142 loc) · 3.42 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package env
import (
"embed"
"io/ioutil"
"os"
"path"
"sort"
"strings"
"github.com/flarco/g"
"github.com/samber/lo"
"github.com/spf13/cast"
"gopkg.in/yaml.v2"
)
var (
Env = &EnvFile{}
HomeDirs = map[string]string{}
)
//go:embed *
var EnvFolder embed.FS
type EnvFile struct {
Connections map[string]map[string]interface{} `json:"connections,omitempty" yaml:"connections,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
Path string `json:"-" yaml:"-"`
TopComment string `json:"-" yaml:"-"`
}
func SetHomeDir(name string) string {
envKey := strings.ToUpper(name) + "_HOME_DIR"
dir := os.Getenv(envKey)
if dir == "" {
dir = path.Join(g.UserHomeDir(), "."+name)
os.Setenv(envKey, dir)
}
HomeDirs[name] = dir
return dir
}
func (ef *EnvFile) WriteEnvFile() (err error) {
connsMap := yaml.MapSlice{}
// order connections names
names := lo.Keys(ef.Connections)
sort.Strings(names)
for _, name := range names {
keyMap := ef.Connections[name]
// order connection keys (type first)
cMap := yaml.MapSlice{}
keys := lo.Keys(keyMap)
sort.Strings(keys)
if v, ok := keyMap["type"]; ok {
cMap = append(cMap, yaml.MapItem{Key: "type", Value: v})
}
for _, k := range keys {
if k == "type" {
continue // already put first
}
k = cast.ToString(k)
cMap = append(cMap, yaml.MapItem{Key: k, Value: keyMap[k]})
}
// add to connection map
connsMap = append(connsMap, yaml.MapItem{Key: name, Value: cMap})
}
efMap := yaml.MapSlice{
{Key: "connections", Value: connsMap},
{Key: "variables", Value: ef.Variables},
}
envBytes, err := yaml.Marshal(efMap)
if err != nil {
return g.Error(err, "could not marshal into YAML")
}
output := []byte(ef.TopComment + string(envBytes))
err = ioutil.WriteFile(ef.Path, formatYAML(output), 0644)
if err != nil {
return g.Error(err, "could not write YAML file")
}
return
}
func formatYAML(input []byte) []byte {
newOutput := []byte{}
pIndent := 0
indent := 0
inIndent := true
prevC := byte('-')
for _, c := range input {
add := false
if c == ' ' && inIndent {
indent++
add = true
} else if c == '\n' {
pIndent = indent
indent = 0
add = true
inIndent = true
} else if prevC == '\n' {
newOutput = append(newOutput, '\n') // add extra space
add = true
} else if prevC == ' ' && pIndent > indent && inIndent {
newOutput = append(newOutput, '\n') // add extra space
for i := 0; i < indent; i++ {
newOutput = append(newOutput, ' ')
}
add = true
inIndent = false
} else {
add = true
inIndent = false
}
if add {
newOutput = append(newOutput, c)
}
prevC = c
}
return newOutput
}
func LoadEnvFile(path string) (ef EnvFile) {
bytes, _ := ioutil.ReadFile(path)
err := yaml.Unmarshal(bytes, &ef)
if err != nil {
err = g.Error(err, "error parsing yaml string")
_ = err
}
ef.Path = path
if ef.Connections == nil {
ef.Connections = map[string]map[string]interface{}{}
}
if ef.Variables == nil {
ef.Variables = map[string]interface{}{}
}
// set env vars
envMap := map[string]string{}
for _, tuple := range os.Environ() {
key := strings.Split(tuple, "=")[0]
val := strings.TrimPrefix(tuple, key+"=")
envMap[key] = val
}
for k, v := range ef.Variables {
if _, found := envMap[k]; !found {
os.Setenv(k, cast.ToString(v))
}
}
return ef
}
func GetEnvFilePath(dir string) string {
return path.Join(dir, "env.yaml")
}