Prepared for first release

- Creating db file in home dir or DB_FILE env variable
- Updated README with usages details
- Added GIF screencast
This commit is contained in:
Anis Ahmad
2020-06-06 02:13:04 +06:00
parent 89624edcba
commit 1cb95b3ce2
15 changed files with 373 additions and 114 deletions

View File

@@ -2,22 +2,51 @@ package util
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/asdine/storm/v3"
"github.com/mitchellh/go-homedir"
)
// ConnectStorm Create database connection
func ConnectStorm() *storm.DB {
db, err := storm.Open(GetEnvStr("DB_FILE", "geek-life.db"))
FatalIfError(err, "Could not connect Embedded Database File")
dbPath := GetEnvStr("DB_FILE", "")
var err error
if dbPath == "" {
// Try in home dir
dbPath, err = homedir.Expand("~/.geek-life/default.db")
// If home dir is not detected, try in system tmp dir
if err != nil {
f, _ := ioutil.TempFile("geek-life", "default.db")
dbPath = f.Name()
}
}
CreateDirIfNotExist(path.Dir(dbPath))
db, openErr := storm.Open(dbPath)
FatalIfError(openErr, "Could not connect Embedded Database File")
return db
}
func CreateDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}
}
// UnixToTime create time.Time from string timestamp
func UnixToTime(timestamp string) time.Time {
parts := strings.Split(timestamp, ".")