packages feed

hablo-1.0.0.0: src/Files.hs

module Files (
      File(..)
    , absolute
    , absoluteLink
    , filePath
    , find
  ) where

import System.Exit (die)
import System.Directory (doesDirectoryExist, doesFileExist, listDirectory, makeAbsolute)
import System.FilePath ((</>))

data File = File FilePath | Dir FilePath

absolute :: File -> IO (FilePath)
absolute file = filePath file >>= makeAbsolute

absoluteLink :: FilePath -> FilePath
absoluteLink ('.':path) = path
absoluteLink path = "/" </> path

filePath :: File -> IO FilePath
filePath file = do
  let (thePath, test, errorMessage) =
        case file of
          File path -> (path, doesFileExist, (++ ": no such file"))
          Dir path -> (path, doesDirectoryExist, (++ ": no such directory"))
  bool <- test thePath
  if bool
    then return thePath
    else die $ errorMessage thePath

find :: FilePath -> IO [FilePath]
find path =
  fmap (path </>) <$> listDirectory path