summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2025-03-07 20:04:33 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2025-03-07 20:04:33 +0100
commit652365ffb1c71b5758329c17015cb5c1912da1f4 (patch)
tree3cc0314099f59228d6b097712f896062c3aed95e
parent94d84d2b7a35b965b26f823e1ee7e7c1ce419d87 (diff)
Use XDG data directory for storage by defaultdevel
Changelog: Use XDG data directory for default storage path
-rw-r--r--README.md7
-rw-r--r--main/Main.hs11
2 files changed, 15 insertions, 3 deletions
diff --git a/README.md b/README.md
index 75d9597..b71eb9d 100644
--- a/README.md
+++ b/README.md
@@ -216,8 +216,11 @@ target device with `/<number>`.
Storage
-------
-Data are by default stored within `.erebos` subdirectory of the current working
-directory. This can be overriden by `EREBOS_DIR` environment variable.
+Data are by default stored under `XDG_DATA_HOME`, typically
+`$HOME/.local/share/erebos`, unless there is an erebos storage already
+in `.erebos` subdirectory of the current working directory, in which case the
+latter one in used instead. This can be overriden by `EREBOS_DIR` environment
+variable.
Private keys are currently stored in plaintext under the `keys` subdirectory of
the erebos directory.
diff --git a/main/Main.hs b/main/Main.hs
index 6e96c14..6c9d165 100644
--- a/main/Main.hs
+++ b/main/Main.hs
@@ -31,6 +31,7 @@ import Data.Typeable
import Network.Socket
import System.Console.GetOpt
+import System.Directory
import System.Environment
import System.Exit
import System.IO
@@ -152,6 +153,14 @@ servicesOptions = concatMap helper $ "all" : map soptName availableServices
| otherwise = s : change name f ss
change _ _ [] = []
+getDefaultStorageDir :: IO FilePath
+getDefaultStorageDir = do
+ lookupEnv "EREBOS_DIR" >>= \case
+ Just dir -> return dir
+ Nothing -> doesFileExist "./.erebos/erebos-storage" >>= \case
+ True -> return "./.erebos"
+ False -> getXdgDirectory XdgData "erebos"
+
main :: IO ()
main = do
(opts, args) <- (getOpt RequireOrder (options ++ servicesOptions) <$> getArgs) >>= \case
@@ -163,7 +172,7 @@ main = do
exitFailure
st <- liftIO $ case optStorage opts of
- DefaultStorage -> openStorage . fromMaybe "./.erebos" =<< lookupEnv "EREBOS_DIR"
+ DefaultStorage -> openStorage =<< getDefaultStorageDir
FilesystemStorage path -> openStorage path
MemoryStorage -> memoryStorage