diff --git a/main.go b/main.go index 7bf47e0a..55aac03f 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/path-generator.go b/path-generator.go new file mode 100644 index 00000000..5a50a94f --- /dev/null +++ b/path-generator.go @@ -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" +}