-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (89 loc) · 2.26 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Package main for creating dump
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"path/filepath"
"time"
"github.com/joho/godotenv"
)
var env map[string]string
var currentDir string
var createEnv bool
var dumpAll bool
var importSQLFile string
var logFile *os.File
func main() {
if logFile != nil {
defer logFile.Close()
}
if createEnv {
return
}
if importSQLFile != "" {
Insert(importSQLFile)
return
}
createDump()
}
func init() {
flag.BoolVar(&createEnv, "make-env", false, "create .env boilerplate file")
flag.BoolVar(&dumpAll, "all", false, "create all dump files")
flag.StringVar(&importSQLFile, "import", "", "import dump file")
flag.Parse()
ex, err := os.Executable()
checkErr(err, "Executeble")
currentDir = filepath.Dir(ex)
if createEnv {
makeEnv()
return
}
envFile := path.Join(currentDir, ".env")
env, err = godotenv.Read(envFile)
checkErr(err, "Error loading .env file variables")
loggerInit()
validKeys := []string{"TITLE", "DB_HOST", "DB_PORT", "DB_DATABASE", "DB_USERNAME", "DB_PASSWORD"}
checkParams(&env, validKeys)
env["DUMP_DIR"] = path.Join(currentDir, env["DUMP_DIR"])
env["DUMP_SUB_DIR"] = path.Join(env["DUMP_DIR"], time.Now().Format("2006-01-02"))
}
// createDump function for create dump files
func createDump() {
// create dump/date folder
dumpDir(env["DUMP_SUB_DIR"])
dump := GetDump()
if dumpAll || env["DUMP_CREATE"] != "false" {
MakeDumpFiles(dump, false)
}
if dumpAll || env["DUMP_INSERT"] != "false" {
MakeDumpFiles(dump, true)
}
if !dumpAll && (env["DUMP_INSERT"] == "false" && env["DUMP_CREATE"] == "false") {
fmt.Println("⚠️ None of the files are not created")
}
dirSize, err := DirSize(env["DUMP_DIR"])
checkErr(err, "Dir size error")
fmt.Println("Dumps total size:", SizeToString(dirSize))
}
// loggerInit write logs
func loggerInit() {
if env["LOG"] != "true" {
return
}
var logFileName string
if env["LOG_ERROR_FILE"] == "" {
logFileName = "error.log"
} else {
logFileName = env["LOG_ERROR_FILE"]
}
logFileName = path.Join(currentDir, logFileName)
logFile, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
// defer logFile.Close() // close in the main()
log.SetOutput(logFile)
}