-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
75 lines (69 loc) · 1.49 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
package main
import (
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/setup"
_ "github.com/iwind/TeaGo/bootstrap"
"github.com/iwind/TeaGo/dbs"
"go/format"
"os"
"path/filepath"
"strconv"
)
func main() {
db, err := dbs.Default()
if err != nil {
fmt.Println("[ERROR]" + err.Error())
return
}
results, err := setup.NewSQLDump().Dump(db)
if err != nil {
fmt.Println("[ERROR]" + err.Error())
return
}
resultsJSON, err := json.Marshal(results)
if err != nil {
fmt.Println("[ERROR]" + err.Error())
return
}
dir, _ := os.Getwd()
var sqlFile string
for i := 0; i < 5; i++ {
lookupFile := dir + "/internal/setup/sql.go"
_, err = os.Stat(lookupFile)
if err != nil {
dir = filepath.Dir(dir)
continue
}
sqlFile = lookupFile
}
if len(sqlFile) == 0 {
fmt.Println("[ERROR]can not find sql.go")
return
}
content := []byte(`package setup
import (
"encoding/json"
"github.com/iwind/TeaGo/logs"
)
// 最新版本的数据库SQL语句,用来对比并升级已有的数据库
// 由 sql-dump/main.go 自动生成
func init() {
err := json.Unmarshal([]byte(` + strconv.Quote(string(resultsJSON)) + `), LatestSQLResult)
if err != nil {
logs.Println("[ERROR]load sql failed: " + err.Error())
}
}
`)
dst, err := format.Source(content)
if err != nil {
fmt.Println("[ERROR]format code failed: " + err.Error())
return
}
err = os.WriteFile(sqlFile, dst, 0666)
if err != nil {
fmt.Println("[ERROR]write file failed: " + err.Error())
return
}
fmt.Println("ok")
}