-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotenv.go
More file actions
66 lines (61 loc) · 1.25 KB
/
dotenv.go
File metadata and controls
66 lines (61 loc) · 1.25 KB
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
package lambda
import (
"github.com/joho/godotenv"
"io/fs"
"log"
"os"
"path"
"strconv"
"strings"
)
func SetupEnv() {
const dirpath = "/opt"
err := fs.WalkDir(os.DirFS(dirpath), ".", func(fpath string, d fs.DirEntry, err error) error {
if err != nil {
log.Printf("WARN: on env file, path: %s, error: %s", fpath, err)
return nil
}
if d.IsDir() {
return nil
}
if !(strings.HasPrefix(fpath, ".env") || strings.HasSuffix(fpath, ".env")) {
return nil
}
// path: .env, .env-nft
fullpath := path.Join(dirpath, fpath)
if err := godotenv.Load(fullpath); err != nil {
log.Printf("ERROR: load env file: %s, error: %s", fullpath, err)
return err
}
log.Printf("INFO: load env file: %s", fullpath)
return nil
})
if err != nil {
log.Fatalf("ERROR: walk env dir: %s, error: %s", dirpath, err)
}
}
func RequiredEnv(key string) string {
v := os.Getenv(key)
if v == "" {
panic("Load env NOT-FOUND, key: " + key)
}
return v
}
func RequiredStrEnv(key string, def string) string {
v := os.Getenv(key)
if v == "" {
return def
}
return v
}
func RequiredIntEnv(key string, def int) int {
str := RequiredEnv(key)
if str == "" {
return def
}
if v, err := strconv.Atoi(str); err != nil {
return def
} else {
return v
}
}