diff --git a/README.md b/README.md index 5e97457..7b3eeb6 100644 --- a/README.md +++ b/README.md @@ -168,11 +168,16 @@ I will be glad to accept your PR. :) By default, it will try to create a db file in you home directory. But as a geek, you may try to put it different location (e,g, in your dropbox for syncing). -In that case, just mention `DB_FILE` as an environment variable. - +In that case, just mention `DB_FILE` as an environment variable. ```bash DB_FILE=~/dropbox/geek-life/default.db geek-life ``` +**UPDATE:** For Windows users, setting ENV variable is not so straight forward. +So, added a flag `--db-file` or `-d` to specify DB file path from command line easily. + ```bash +geek-life --db-file=D:\a-writable-dir\tasks.db +``` + #### :question: How can I suggest a feature? diff --git a/app/cli.go b/app/cli.go index 4937a6e..91b2d2f 100644 --- a/app/cli.go +++ b/app/cli.go @@ -7,6 +7,7 @@ import ( "github.com/asdine/storm/v3" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" + flag "github.com/spf13/pflag" "github.com/ajaxray/geek-life/model" "github.com/ajaxray/geek-life/repository" @@ -27,20 +28,29 @@ var ( db *storm.DB projectRepo repository.ProjectRepository taskRepo repository.TaskRepository + + // Flag variables + dbFile string ) +func init() { + flag.StringVarP(&dbFile, "db-file", "d", "", "Specify DB file path manually.") +} + func main() { app = tview.NewApplication() + flag.Parse() - db = util.ConnectStorm() + db = util.ConnectStorm(dbFile) defer func() { if err := db.Close(); err != nil { util.LogIfError(err, "Error in closing storm Db") } }() - if len(os.Args) > 1 && os.Args[1] == "migrate" { + if flag.NArg() > 0 && flag.Arg(0) == "migrate" { migrate(db) + fmt.Println("Database migrated successfully!") } else { projectRepo = repo.NewProjectRepository(db) taskRepo = repo.NewTaskRepository(db) diff --git a/go.mod b/go.mod index 32638f8..333f117 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/pgavlin/femto v0.0.0-20201224065653-0c9d20f9cac4 github.com/rivo/tview v0.0.0-20210111184519-c818a0c789ee + github.com/spf13/pflag v1.0.5 // indirect github.com/stretchr/testify v1.7.0 // indirect go.etcd.io/bbolt v1.3.5 // indirect golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect diff --git a/go.sum b/go.sum index 44543bf..38a6228 100644 --- a/go.sum +++ b/go.sum @@ -81,6 +81,8 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= diff --git a/util/util.go b/util/util.go index bbc7de8..486872b 100644 --- a/util/util.go +++ b/util/util.go @@ -15,10 +15,22 @@ import ( ) // ConnectStorm Create database connection -func ConnectStorm() *storm.DB { - dbPath := GetEnvStr("DB_FILE", "") - var err error +func ConnectStorm(dbFilePath string) *storm.DB { + var dbPath string + if dbFilePath != "" { + info, err := os.Stat(dbFilePath) + if err == nil && info.IsDir() { + fmt.Println("Mentioned DB path is a directory. Please specify a file or ignore to create automatically in home directory.") + os.Exit(1) + } + + dbPath = dbFilePath + } else { + dbPath = GetEnvStr("DB_FILE", "") + } + + var err error if dbPath == "" { // Try in home dir dbPath, err = homedir.Expand("~/.geek-life/default.db")