module Debian.Extra.Files
( withTemporaryFile
) where
import "mtl" Control.Monad.Trans (MonadIO, liftIO)
import System.Directory (getTemporaryDirectory, removeFile)
import System.IO (hPutStr, hClose, openBinaryTempFile)
withTemporaryFile :: MonadIO m
=> (FilePath -> m a)
-> String
-> m a
withTemporaryFile f text =
do path <- liftIO writeTemporaryFile
result <- f path
liftIO $ removeFile path
return result
where
writeTemporaryFile =
do dir <- getTemporaryDirectory
(path, h) <- openBinaryTempFile dir "wtf.tmp"
hPutStr h text
hClose h
return path