Added flag for setting DB file path
This commit is contained in:
@@ -169,10 +169,15 @@ 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).
|
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
|
```bash
|
||||||
DB_FILE=~/dropbox/geek-life/default.db geek-life
|
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?
|
#### :question: How can I suggest a feature?
|
||||||
|
|
||||||
|
|||||||
14
app/cli.go
14
app/cli.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/asdine/storm/v3"
|
"github.com/asdine/storm/v3"
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
|
flag "github.com/spf13/pflag"
|
||||||
|
|
||||||
"github.com/ajaxray/geek-life/model"
|
"github.com/ajaxray/geek-life/model"
|
||||||
"github.com/ajaxray/geek-life/repository"
|
"github.com/ajaxray/geek-life/repository"
|
||||||
@@ -27,20 +28,29 @@ var (
|
|||||||
db *storm.DB
|
db *storm.DB
|
||||||
projectRepo repository.ProjectRepository
|
projectRepo repository.ProjectRepository
|
||||||
taskRepo repository.TaskRepository
|
taskRepo repository.TaskRepository
|
||||||
|
|
||||||
|
// Flag variables
|
||||||
|
dbFile string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVarP(&dbFile, "db-file", "d", "", "Specify DB file path manually.")
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app = tview.NewApplication()
|
app = tview.NewApplication()
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
db = util.ConnectStorm()
|
db = util.ConnectStorm(dbFile)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := db.Close(); err != nil {
|
if err := db.Close(); err != nil {
|
||||||
util.LogIfError(err, "Error in closing storm Db")
|
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)
|
migrate(db)
|
||||||
|
fmt.Println("Database migrated successfully!")
|
||||||
} else {
|
} else {
|
||||||
projectRepo = repo.NewProjectRepository(db)
|
projectRepo = repo.NewProjectRepository(db)
|
||||||
taskRepo = repo.NewTaskRepository(db)
|
taskRepo = repo.NewTaskRepository(db)
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -15,6 +15,7 @@ require (
|
|||||||
github.com/mitchellh/go-homedir v1.1.0
|
github.com/mitchellh/go-homedir v1.1.0
|
||||||
github.com/pgavlin/femto v0.0.0-20201224065653-0c9d20f9cac4
|
github.com/pgavlin/femto v0.0.0-20201224065653-0c9d20f9cac4
|
||||||
github.com/rivo/tview v0.0.0-20210111184519-c818a0c789ee
|
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
|
github.com/stretchr/testify v1.7.0 // indirect
|
||||||
go.etcd.io/bbolt v1.3.5 // indirect
|
go.etcd.io/bbolt v1.3.5 // indirect
|
||||||
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
|
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
|
||||||
|
|||||||
2
go.sum
2
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/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 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
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/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.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
|
|||||||
18
util/util.go
18
util/util.go
@@ -15,10 +15,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ConnectStorm Create database connection
|
// ConnectStorm Create database connection
|
||||||
func ConnectStorm() *storm.DB {
|
func ConnectStorm(dbFilePath string) *storm.DB {
|
||||||
dbPath := GetEnvStr("DB_FILE", "")
|
var dbPath string
|
||||||
var err error
|
|
||||||
|
|
||||||
|
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 == "" {
|
if dbPath == "" {
|
||||||
// Try in home dir
|
// Try in home dir
|
||||||
dbPath, err = homedir.Expand("~/.geek-life/default.db")
|
dbPath, err = homedir.Expand("~/.geek-life/default.db")
|
||||||
|
|||||||
Reference in New Issue
Block a user