From 6bab302785263e942ee50421fda500316fa253b0 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Mon, 12 Mar 2018 16:45:49 +0700 Subject: [PATCH] Change default database to home dir --- README.md | 5 ++++- main.go | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 491829d21..3d82718a3 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,10 @@ Use "shiori [command] --help" for more information about a command. ## Advanced -By default, `shiori` will create database in the location where you run it. For example, if you run `shiori`. To set the database to a specific location, you can set the environment variable `ENV_SHIORI_DB` to your desired path. +By default, `shiori` will create database in `$HOME/.shiori.db`. If you want to set the database to another location, you can set the environment variable `ENV_SHIORI_DB` to your desired path : + +- If `ENV_SHIORI_DB` points to a directory, it will create `.shiori.db` file inside that directory, so the final path for database is `$ENV_SHIORI_DB/.shiori.db`. +- Else, it will create a new database file in the specified path. ## Usage with Docker diff --git a/main.go b/main.go index 1369076b6..954cd8e2a 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "os" + "os/user" fp "path/filepath" "github.com/RadhiFadlillah/shiori/cmd" @@ -11,11 +12,11 @@ import ( ) func main() { - databasePath := "shiori.db" + databasePath := fp.Join(getHomeDir(), ".shiori.db") if value, found := os.LookupEnv("ENV_SHIORI_DB"); found { - // If ENV_SHIORI_DB is directory, append "shiori.db" as filename + // If ENV_SHIORI_DB is directory, append ".shiori.db" as filename if f1, err := os.Stat(value); err == nil && f1.IsDir() { - value = fp.Join(value, "shiori.db") + value = fp.Join(value, ".shiori.db") } databasePath = value @@ -28,6 +29,15 @@ func main() { cmd.Execute() } +func getHomeDir() string { + user, err := user.Current() + if err != nil { + return "" + } + + return user.HomeDir +} + func checkError(err error) { if err != nil { panic(err)