Untitled


SUBMITTED BY: itsDaveLad

DATE: March 11, 2016, 6:29 p.m.

FORMAT: Text only

SIZE: 1.1 kB

HITS: 765

  1. -- for IdempotentIO
  2. import Control.Monad (when)
  3. -- for Link
  4. import Control.Monad.Loops (andM)
  5. import System.Posix.Files (fileExist, getSymbolicLinkStatus, isSymbolicLink, readSymbolicLink, createSymbolicLink)
  6. import System.Directory (getHomeDirectory, getCurrentDirectory)
  7. class IdempotentIO thing where
  8. check :: thing -> IO Bool
  9. set, ensure :: thing -> IO ()
  10. ensure thing = do
  11. isSet <- check thing
  12. when (not isSet) $ set thing
  13. data Link = Link
  14. { fromPath :: String
  15. , toPath :: String
  16. , qualifyPath :: String -> String
  17. }
  18. instance IdempotentIO Link where
  19. check link = andM
  20. [ fileExist $ qualified fromPath
  21. , fmap isSymbolicLink . getSymbolicLinkStatus $ qualified fromPath
  22. , fmap (== qualified toPath) . readSymbolicLink $ qualified fromPath
  23. ]
  24. where qualified path = qualifyPath link $ path link
  25. set link = createSymbolicLink (qualified toPath) (qualified fromPath)
  26. where qualified path = qualifyPath link $ path link
  27. main = ensure $ Link "foo" "bar" id

comments powered by Disqus