diff options
| author | Ben Sima <ben@bensima.com> | 2025-11-27 09:09:13 -0500 |
|---|---|---|
| committer | Ben Sima <ben@bensima.com> | 2025-11-27 09:09:13 -0500 |
| commit | dc1b9c834f3c1d38f46c4fedad00a91718f76cb9 (patch) | |
| tree | dba95896bb0afcf50dedaa3d2eda315eeac3b872 /Omni/Task/Core.hs | |
| parent | f929bb23f9b55df81697579065cfeb42c4376799 (diff) | |
jr: Fix empty query param parsing and use XDG for db path
- FromHttpApiData instances return Left for empty strings (Servant
treats as missing param for QueryParam Maybe)
- getTasksDbPath now uses ~/.local/share/jr/jr.db via XDG - Remove
standalone task binary output - Add tests for parseQueryParam on
Priority/Status
Task-Id: t-145, t-146 Amp-Thread-ID:
https://ampcode.com/threads/T-2ad5310f-b7f5-451d-ad9b-35aa17c58774
Co-authored-by: Amp <amp@ampcode.com>
Diffstat (limited to 'Omni/Task/Core.hs')
| -rw-r--r-- | Omni/Task/Core.hs | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/Omni/Task/Core.hs b/Omni/Task/Core.hs index a68f37b..d1d92d5 100644 --- a/Omni/Task/Core.hs +++ b/Omni/Task/Core.hs @@ -19,8 +19,9 @@ import qualified Database.SQLite.Simple.FromField as SQL import qualified Database.SQLite.Simple.Ok as SQLOk import qualified Database.SQLite.Simple.ToField as SQL import GHC.Generics () -import System.Directory (createDirectoryIfMissing, doesFileExist) +import System.Directory (XdgDirectory (..), createDirectoryIfMissing, doesFileExist, getXdgDirectory) import System.Environment (lookupEnv) +import System.FilePath (takeDirectory, (</>)) import System.IO.Unsafe (unsafePerformIO) import Web.HttpApiData (FromHttpApiData (..)) @@ -116,14 +117,18 @@ instance FromJSON RetryContext -- HTTP API Instances (for Servant query params) instance FromHttpApiData Status where - parseQueryParam t = case readMaybe (T.unpack t) of - Just s -> Right s - Nothing -> Left ("Invalid status: " <> t) + parseQueryParam t + | T.null t = Left "" + | otherwise = case readMaybe (T.unpack t) of + Just s -> Right s + Nothing -> Left ("Invalid status: " <> t) instance FromHttpApiData Priority where - parseQueryParam t = case readMaybe (T.unpack t) of - Just p -> Right p - Nothing -> Left ("Invalid priority: " <> t) + parseQueryParam t + | T.null t = Left "" + | otherwise = case readMaybe (T.unpack t) of + Just p -> Right p + Nothing -> Left ("Invalid priority: " <> t) -- SQLite Instances @@ -235,11 +240,12 @@ getTasksDbPath :: IO FilePath getTasksDbPath = do customPath <- lookupEnv "TASK_DB_PATH" testMode <- lookupEnv "TASK_TEST_MODE" - let path = case (testMode, customPath) of - (Just "1", _) -> "_/tmp/tasks-test.db" -- Test mode uses cabdir - (_, Just p) -> p -- Custom path for production - _ -> "_/tmp/tasks.db" -- Default uses cabdir - pure path + case (testMode, customPath) of + (Just "1", _) -> pure "_/tmp/tasks-test.db" + (_, Just p) -> pure p + _ -> do + xdgData <- getXdgDirectory XdgData "jr" + pure (xdgData </> "jr.db") -- DB Helper withDb :: (SQL.Connection -> IO a) -> IO a @@ -252,7 +258,8 @@ withDb action = do -- Initialize the task database initTaskDb :: IO () initTaskDb = do - createDirectoryIfMissing True "_/tmp" + dbPath <- getTasksDbPath + createDirectoryIfMissing True (takeDirectory dbPath) withDb <| \conn -> do SQL.execute_ conn |
