Commit 10d1fc70 authored by Simon Schürg's avatar Simon Schürg 🚀
Browse files

Use XDG_CACHE_HOME for boltdb location

parent 5475f2fa
......@@ -3,11 +3,14 @@ package internal
import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/asdine/storm/v3"
"github.com/mitchellh/go-homedir"
"github.com/pascaldekloe/jwt"
)
......@@ -25,12 +28,30 @@ const SchemaVersion = "0.0.1"
// db references the local database
var db *storm.DB
var dbPath string = "actl.boltdb"
// getDbPath $HOME/.cache
func getDbPath() string {
dbName := "actl.boltdb"
xdgCacheHome := os.Getenv("XDG_CACHE_HOME")
if xdgCacheHome == "" {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// ${HOME}/.cache should be the default XDG_CACHE_HOME
xdgCacheHome = fmt.Sprintf("%s/.cache", home)
}
dbPath := fmt.Sprintf("%s/actl", xdgCacheHome)
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
os.Mkdir(dbPath, 0700)
}
return filepath.FromSlash(fmt.Sprintf("%s/%s", dbPath, dbName))
}
// InitDb ensures the existence of the local database and the schema.
func InitDb() {
var err error
db, err = storm.Open(dbPath)
db, err = storm.Open(getDbPath())
FatalOnError(err)
if !doesSchemaVersionMatch() {
log.Fatalln("Database schma version mismatch")
......@@ -82,7 +103,7 @@ func UpsertJWT(encodedToken string) *Token {
// Be careful with this method, it is dangerous!
func ClearDb() {
CloseDb()
err := os.Remove(dbPath)
err := os.Remove(getDbPath())
FatalOnError(err)
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment