gosuki/db.go

58 lines
1.1 KiB
Go
Raw Normal View History

2017-11-13 18:14:59 +00:00
package main
import (
2017-11-17 14:18:53 +00:00
"path/filepath"
2020-08-12 18:13:01 +00:00
2020-11-06 17:50:36 +00:00
"git.sp4ke.xyz/sp4ke/gomark/database"
"git.sp4ke.xyz/sp4ke/gomark/utils"
)
2018-11-09 17:25:50 +00:00
type DB = database.DB
2017-11-17 14:18:53 +00:00
func initDB() {
var err error
2017-11-17 14:18:53 +00:00
// Check and initialize local db as last step
// browser bookmarks should already be in cache
2017-11-17 14:18:53 +00:00
dbdir := utils.GetDefaultDBPath()
2018-11-09 17:25:50 +00:00
dbpath := filepath.Join(dbdir, database.DBFileName)
// Verifiy that local db directory path is writeable
err = utils.CheckWriteable(dbdir)
2018-10-28 19:19:12 +00:00
if err != nil {
log.Critical(err)
}
2017-11-17 14:18:53 +00:00
2017-11-19 16:00:37 +00:00
// If local db exists load it to cacheDB
var exists bool
if exists, err = utils.CheckFileExists(dbpath); exists {
2018-10-28 19:19:12 +00:00
if err != nil {
log.Warning(err)
}
2018-10-28 19:26:38 +00:00
log.Infof("<%s> exists, preloading to cache", dbpath)
er := database.Cache.DB.SyncFromDisk(dbpath)
2018-11-13 16:11:16 +00:00
if er != nil {
log.Critical(er)
}
} else {
2018-10-28 19:19:12 +00:00
if err != nil {
log.Error(err)
}
// Else initialize it
initLocalDB(database.Cache.DB, dbpath)
}
}
// Initialize the local database file
func initLocalDB(db *DB, dbpath string) {
2017-11-19 16:00:37 +00:00
log.Infof("Initializing local db at '%s'", dbpath)
err := db.SyncToDisk(dbpath)
2018-10-28 19:19:12 +00:00
if err != nil {
log.Critical(err)
}
2017-11-13 18:14:59 +00:00
}