shiori/main.go

62 lines
1.3 KiB
Go
Raw Normal View History

2018-04-28 22:02:36 +08:00
//go:generate go run assets-generator.go
2018-01-26 18:40:01 +08:00
package main
import (
2018-03-05 08:14:58 +08:00
"os"
fp "path/filepath"
2018-03-05 08:14:58 +08:00
"github.com/RadhiFadlillah/shiori/cmd"
2018-04-28 22:02:36 +08:00
dt "github.com/RadhiFadlillah/shiori/database"
2018-03-05 08:14:58 +08:00
_ "github.com/mattn/go-sqlite3"
2018-05-01 11:12:55 +08:00
apppaths "github.com/muesli/go-app-paths"
"github.com/sirupsen/logrus"
)
2018-01-26 18:40:01 +08:00
func main() {
2018-05-01 11:12:55 +08:00
// Create database path
dbPath := createDatabasePath()
2018-05-01 11:12:55 +08:00
// Make sure directory exist
os.MkdirAll(fp.Dir(dbPath), os.ModePerm)
2018-03-05 08:14:58 +08:00
2018-05-01 11:12:55 +08:00
// Open database
sqliteDB, err := dt.OpenSQLiteDatabase(dbPath)
2018-03-05 08:14:58 +08:00
checkError(err)
2018-05-01 11:12:55 +08:00
// Start cmd
2018-04-28 22:02:36 +08:00
shioriCmd := cmd.NewShioriCmd(sqliteDB)
if err := shioriCmd.Execute(); err != nil {
logrus.Fatalln(err)
}
2018-03-05 08:14:58 +08:00
}
2018-05-01 11:12:55 +08:00
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")
2018-03-12 17:45:49 +08:00
}
2018-05-01 11:12:55 +08:00
// When all fail, create database in working directory
return "shiori.db"
2018-03-12 17:45:49 +08:00
}
2018-03-05 08:14:58 +08:00
func checkError(err error) {
if err != nil {
panic(err)
}
}