-
Notifications
You must be signed in to change notification settings - Fork 0
/
gormseeder.go
62 lines (48 loc) · 1.06 KB
/
gormseeder.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
package gormseeder
import (
"fmt"
"os"
"gorm.io/gorm"
)
const Version = "0.1.0"
var Testing bool = false
func InitSeeder(db *gorm.DB, tablename string) {
SeederStore := NewSeederStore(db, tablename)
// If version show
if getEnv("VERSION", "") == "1" {
fmt.Println("Gorseeder version: ", Version)
}
// If history show
if getEnv("HISTORY", "") == "1" {
showHistory(SeederStore)
}
tag := getEnv("TAG", "")
user := getEnv("USER", "")
if tag == "" {
fmt.Println("TAG is missing")
showExample()
return
}
if user == "" {
fmt.Println("no USER set")
showExample()
return
}
err := performSeeder(tag, user, db, SeederStore)
if err != nil {
fmt.Println("\033[;31mSeeder ERROR! Keep last Seeder step\033[0m")
fmt.Println(err)
return
}
}
func showExample() {
fmt.Println("Examples:")
fmt.Println("HISTORY=1 VERSION=1 TAG=[<tag>|all] USER=tester [go run ./...|<build>]")
}
// Get env variable as string
func getEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}