-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloading.go
45 lines (39 loc) · 1.1 KB
/
loading.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
package commonUtils
import (
"github.com/joho/godotenv"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
)
func LoadEnv(envFilePath string) (env map[string]string, err error) {
_, err = os.Stat(envFilePath)
if err != nil {
return
}
// ::envFilePath:: root should be where you execute the program from
env, err = godotenv.Read(envFilePath)
if err != nil {
pc, file, line, _ := runtime.Caller(1) // using 1 to log where the error happened
file = filepath.Base(file)
n := runtime.FuncForPC(pc).Name()
var msg = fmt.Sprintf("[ERROR] %s (%s:%d)\n%+v\n", n, file, line, err)
err = errors.New(msg)
}
return
}
func LoadEnvVar(envFilePath, varName string) (v string, err error) {
// ::envFilePath:: root should be where you execute the program from
var env map[string]string
env, err = godotenv.Read(envFilePath)
if err != nil {
pc, file, line, _ := runtime.Caller(1) // using 1 to log where the error happened
file = filepath.Base(file)
n := runtime.FuncForPC(pc).Name()
var msg = fmt.Sprintf("[ERROR] %s (%s:%d)\n%+v\n", n, file, line, err)
err = errors.New(msg)
}
v = env[varName]
return
}