-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
59 lines (48 loc) · 1.23 KB
/
writer.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
package dbexport
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
func replaceNewLine(content string) string {
strings.ReplaceAll(content, "^M", "")
lines := strings.Split(content, "\r\n")
return strings.Join(lines, "\n")
}
func SaveDbObjects(dbObjects []DbObject) []string {
var savedFiles []string
for i := range dbObjects {
dbObject := dbObjects[i]
filePath := makeDbObjectPath(dbObject)
fmt.Println("Salvando", dbObject.Type, ":", dbObject.Name, "->", i, "de", len(dbObjects))
dbObject.Content = GetSqlForObject(dbObject.Type, dbObject.Name)
if WriteSqlToFile(filePath, dbObject.Content) {
savedFiles = append(savedFiles, filePath)
}
}
return savedFiles
}
func makeDbObjectPath(dbObject DbObject) string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
return dir + "/export/" + dbObject.Type + "/" + dbObject.Name + ".sql"
}
func WriteSqlToFile(filePath, sql string) bool {
content := replaceNewLine(sql)
byteContent := []byte(content)
path := filepath.Dir(filePath)
if _, err := os.Stat(path); os.IsNotExist(err) {
os.MkdirAll(path, os.ModePerm)
}
err := ioutil.WriteFile(filePath, byteContent, os.ModePerm)
if err != nil {
log.Fatal(err)
return false
}
return true
}