For development mode, create database in woking dir

This commit is contained in:
Radhi Fadlillah 2018-05-01 10:23:38 +07:00
parent 997781f96e
commit ec53fe6113
2 changed files with 43 additions and 33 deletions

35
main.go
View file

@ -3,23 +3,15 @@
package main
import (
"os"
fp "path/filepath"
"github.com/RadhiFadlillah/shiori/cmd"
dt "github.com/RadhiFadlillah/shiori/database"
_ "github.com/mattn/go-sqlite3"
apppaths "github.com/muesli/go-app-paths"
"github.com/sirupsen/logrus"
)
var dbPath = "shiori.db"
func main() {
// Create database path
dbPath := createDatabasePath()
// Make sure directory exist
os.MkdirAll(fp.Dir(dbPath), os.ModePerm)
// Open database
sqliteDB, err := dt.OpenSQLiteDatabase(dbPath)
checkError(err)
@ -31,29 +23,6 @@ func main() {
}
}
func createDatabasePath() string {
// Try to look at environment variables
dbPath, found := os.LookupEnv("ENV_SHIORI_DB")
if found {
// If ENV_SHIORI_DB is directory, append "shiori.db" as filename
if f1, err := os.Stat(dbPath); err == nil && f1.IsDir() {
dbPath = fp.Join(dbPath, "shiori.db")
}
return dbPath
}
// Try to use platform specific app path
userScope := apppaths.NewScope(apppaths.User, "shiori", "shiori")
dataDir, err := userScope.DataDir()
if err == nil {
return fp.Join(dataDir, "shiori.db")
}
// When all fail, create database in working directory
return "shiori.db"
}
func checkError(err error) {
if err != nil {
panic(err)

41
path-generator.go Normal file
View file

@ -0,0 +1,41 @@
// +build !dev
package main
import (
"os"
fp "path/filepath"
apppaths "github.com/muesli/go-app-paths"
)
func init() {
// Set database path
dbPath = createDatabasePath()
// Make sure directory exist
os.MkdirAll(fp.Dir(dbPath), os.ModePerm)
}
func createDatabasePath() string {
// Try to look at environment variables
dbPath, found := os.LookupEnv("ENV_SHIORI_DB")
if found {
// If ENV_SHIORI_DB is directory, append "shiori.db" as filename
if f1, err := os.Stat(dbPath); err == nil && f1.IsDir() {
dbPath = fp.Join(dbPath, "shiori.db")
}
return dbPath
}
// Try to use platform specific app path
userScope := apppaths.NewScope(apppaths.User, "shiori", "shiori")
dataDir, err := userScope.DataDir()
if err == nil {
return fp.Join(dataDir, "shiori.db")
}
// When all fail, create database in working directory
return "shiori.db"
}