mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 06:40:26 +08:00
78 lines
1.5 KiB
Go
78 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
|
_ "github.com/iwind/TeaGo/bootstrap"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
"github.com/iwind/TeaGo/logs"
|
|
"go/format"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
)
|
|
|
|
func main() {
|
|
db, err := dbs.Default()
|
|
if err != nil {
|
|
logs.Println("[ERROR]" + err.Error())
|
|
return
|
|
}
|
|
results, err := setup.NewSQLDump().Dump(db)
|
|
if err != nil {
|
|
logs.Println("[ERROR]" + err.Error())
|
|
return
|
|
}
|
|
resultsJSON, err := json.Marshal(results)
|
|
if err != nil {
|
|
logs.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 {
|
|
logs.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 {
|
|
logs.Println("[ERROR]format code failed: " + err.Error())
|
|
return
|
|
}
|
|
|
|
err = ioutil.WriteFile(sqlFile, dst, 0666)
|
|
if err != nil {
|
|
logs.Println("[ERROR]write file failed: " + err.Error())
|
|
return
|
|
}
|
|
logs.Println("ok")
|
|
}
|