-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.go
47 lines (40 loc) · 972 Bytes
/
debug.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
//+build !prod
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"runtime"
"strings"
)
// Debug mode configs exposure
// binds Azure Functions local settings and FUNCTIONS_CUSTOMHANDLER_PORT via .env
func init() {
log.Println("Debug mode is initiated")
readDebugDotEnv("./functions/tmp/.env")
readDebugDotEnv("./tmp/.env")
}
func resolveCnfgPath(relativePath string) string {
_, filename, _, _ := runtime.Caller(1)
return path.Join(path.Dir(filename), relativePath)
}
func readDebugDotEnv(dotEnvPath string) {
envFilePath := resolveCnfgPath(dotEnvPath)
envFile, err := os.Open(envFilePath)
if err != nil {
return
}
defer func() { _ = envFile.Close() }()
byteValue, _ := ioutil.ReadAll(envFile)
keyVals := strings.Split(fmt.Sprintf("%s", byteValue), "\n")
for _, keyVal := range keyVals {
kv := strings.SplitN(keyVal, "=", 2)
if len(kv) == 2 {
if _, ok := os.LookupEnv(kv[0]); !ok {
_ = os.Setenv(kv[0], kv[1])
}
}
}
}