From 40a692f67403d49764e0d84a6868a2d395123ea1 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 4 Nov 2023 02:01:15 +0000 Subject: [PATCH] Move store to $XDG_DATA_HOME/lmcli Default to ~/.local/share/lmcli --- store.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/store.go b/store.go index 2712edb..182ae3b 100644 --- a/store.go +++ b/store.go @@ -2,6 +2,8 @@ package main import ( "database/sql" + "os" + "path/filepath" "gorm.io/gorm" "gorm.io/driver/sqlite" sqids "github.com/sqids/sqids-go" @@ -26,12 +28,25 @@ type Conversation struct { Title string } -const ( - DATABASE_FILE string = "./data.db" -) + +func getDataDir() string { + var dataDir string; + + xdgDataHome := os.Getenv("XDG_DATA_HOME") + if xdgDataHome != "" { + dataDir = filepath.Join(xdgDataHome, "lmcli") + } else { + userHomeDir, _ := os.UserHomeDir() + dataDir = filepath.Join(userHomeDir, ".local/share/lmcli") + } + + os.MkdirAll(dataDir, 0755) + return dataDir +} func InitializeStore() (*Store, error) { - db, err := gorm.Open(sqlite.Open(DATABASE_FILE), &gorm.Config{}) + databaseFile := filepath.Join(getDataDir(), "conversations.db") + db, err := gorm.Open(sqlite.Open(databaseFile), &gorm.Config{}) if err != nil { return nil, err }