Skip to content

Commit

Permalink
[#78] Add function to get user's home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
vaclavsvejcar committed Aug 9, 2021
1 parent d64aadf commit 6935dce
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
27 changes: 10 additions & 17 deletions src/Headroom/IO/FileSystem.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module Headroom.IO.FileSystem
, FindFilesFn
, FindFilesByExtsFn
, FindFilesByTypesFn
, GetCacheDirectoryFn
, GetCurrentDirectoryFn
, GetUserDirectoryFn
, ListFilesFn
, LoadFileFn
-- * Polymorphic Record
Expand All @@ -32,7 +32,6 @@ module Headroom.IO.FileSystem
, findFiles
, findFilesByExts
, findFilesByTypes
, getCacheDirectory
, listFiles
, loadFile
-- * Working with Files Metadata
Expand All @@ -49,13 +48,12 @@ import Headroom.Data.Regex ( Regex
import Headroom.FileType ( listExtensions )
import Headroom.FileType.Types ( FileType )
import RIO
import RIO.Directory ( XdgDirectory(..)
, createDirectory
import RIO.Directory ( createDirectory
, doesDirectoryExist
, doesFileExist
, getCurrentDirectory
, getDirectoryContents
, getXdgDirectory
, getHomeDirectory
)
import RIO.FilePath ( isExtensionOf
, takeExtension
Expand Down Expand Up @@ -119,15 +117,16 @@ type FindFilesByTypesFn m
-- ^ list of found files


-- | Type of a function that obtains the cache directory as an absolute path.
type GetCacheDirectoryFn m = m FilePath


-- | Type of a function that obtains the current working directory as an
-- absolute path.
type GetCurrentDirectoryFn m = m FilePath


-- | Type of a function that obtains the user's home directory as an absolute
-- path.
type GetUserDirectoryFn m = m FilePath


-- | Type of a function that recursively find all files on given path. If file
-- reference is passed instead of directory, such file path is returned.
type ListFilesFn m
Expand Down Expand Up @@ -164,10 +163,9 @@ data FileSystem m = FileSystem
-- ^ Function that recursively finds files on given path by file extensions.
, fsFindFilesByTypes :: FindFilesByTypesFn m
-- ^ Function that recursively find files on given path by their file types.
, fsGetCacheDirectory :: GetCacheDirectoryFn m
-- ^ Function that obtains cache directory as an absolute path.
, fsGetCurrentDirectory :: GetCurrentDirectoryFn m
-- ^ Function that obtains the current working directory as an absolute path.
, fsGetUserDirectory :: GetUserDirectoryFn m
, fsListFiles :: ListFilesFn m
-- ^ Function that recursively find all files on given path. If file reference
-- is passed instead of directory, such file path is returned.
Expand All @@ -183,8 +181,8 @@ mkFileSystem = FileSystem { fsCreateDirectory = createDirectory
, fsFindFiles = findFiles
, fsFindFilesByExts = findFilesByExts
, fsFindFilesByTypes = findFilesByTypes
, fsGetCacheDirectory = getCacheDirectory
, fsGetCurrentDirectory = getCurrentDirectory
, fsGetUserDirectory = getHomeDirectory
, fsListFiles = listFiles
, fsLoadFile = loadFile
}
Expand All @@ -209,11 +207,6 @@ findFilesByTypes headersConfig types path =
findFilesByExts path (types >>= listExtensions headersConfig)


-- | Returns absolute path of the system preferred cache directory.
getCacheDirectory :: MonadIO m => GetCacheDirectoryFn m
getCacheDirectory = getXdgDirectory XdgCache ""


-- | Recursively find all files on given path. If file reference is passed
-- instead of directory, such file path is returned.
listFiles :: MonadIO m => ListFilesFn m
Expand Down
2 changes: 1 addition & 1 deletion test/Headroom/Command/InitSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ env = TestEnv { .. }
, fsFindFiles = undefined
, fsFindFilesByExts = undefined
, fsFindFilesByTypes = undefined
, fsGetCacheDirectory = undefined
, fsGetCurrentDirectory = undefined
, fsGetUserDirectory = undefined
, fsListFiles = undefined
, fsLoadFile = undefined
}
Expand Down
2 changes: 1 addition & 1 deletion test/Headroom/Command/RunSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ env = TestEnv { .. }
, fsFindFiles = undefined
, fsFindFilesByExts = undefined
, fsFindFilesByTypes = undefined
, fsGetCacheDirectory = undefined
, fsGetCurrentDirectory = undefined
, fsGetUserDirectory = undefined
, fsListFiles = undefined
, fsLoadFile = undefined
}
Expand Down

0 comments on commit 6935dce

Please sign in to comment.