-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (61 loc) · 1.36 KB
/
main.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
package main
import (
"errors"
"fmt"
"github.com/bayucandra/go-seed-pg/db"
file_operations "github.com/bayucandra/go-seed-pg/file-operations"
sql_operations "github.com/bayucandra/go-seed-pg/seed-operations"
"github.com/joho/godotenv"
"log"
"os"
)
func main() {
if os.Getenv("GO_ENV") == "development" || os.Getenv("GO_ENV") == "test" {
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
envFile := ".env"
if len(os.Args) > 1 {
envFile = os.Args[1]
}
err := loadEnv( envFile)
if err != nil {
log.Println("Not loading .env var")
}
_, err = checkEnv()
if err != nil {
log.Fatal(err)
}
db.Init()
files, err := file_operations.DirParse(os.Getenv("GO_SEED_SOURCE_PATH"))
sql_operations.SeedAll(files)
_ = db.DBConn.Close()
}
func loadEnv(fileName string) error {
err := godotenv.Load(fileName)
return err
}
func checkEnv(envNames ...string) (notfound string, err error) {
envVars := []string{
"GO_SEED_PG_HOST",
"GO_SEED_PG_PORT",
"GO_SEED_PG_USER",
"GO_SEED_PG_PASSWORD",
"GO_SEED_PG_DBNAME",
"GO_SEED_PG_SCHEMA",
"GO_SEED_PG_SSLMODE",
"GO_SEED_SOURCE_PATH",
}
if len(envNames) > 0 {
for _, val := range envNames {
envVars = append(envVars, val)
}
}
for _, val := range envVars {
if os.Getenv(val) == "" {
notfound = val
err = errors.New(fmt.Sprintf("Env variable %s is not found", val))
return
}
}
return
}