expireGititCache.hs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. {-
  2. expireGititCache - (C) 2009 John MacFarlane, licensed under the GPL
  3. This program is designed to be used in post-update hooks and other scripts.
  4. Usage: expireGititCache base-url [file..]
  5. Example:
  6. expireGititCache http://localhost:5001 page1.page foo/bar.hs "Front Page.page"
  7. will produce POST requests to http://localhost:5001/_expire/page1,
  8. http://localhost:5001/_expire/foo/bar.hs, and
  9. http://localhost:5001/_expire/Front Page.
  10. Return statuses:
  11. 0 -> the cached page was successfully expired (or was not cached in the first place)
  12. 1 -> fewer than two arguments were supplied
  13. 3 -> did not receive a 200 OK response from the request
  14. 5 -> could not parse the uri
  15. -}
  16. module Main
  17. where
  18. import Network.HTTP
  19. import System.Environment
  20. import Network.URI
  21. import System.FilePath
  22. import Control.Monad
  23. import System.IO
  24. import System.Exit
  25. main :: IO ()
  26. main = do
  27. args <- getArgs
  28. (uriString : files) <- if length args < 2
  29. then usageMessage >> return [""]
  30. else return args
  31. uri <- case parseURI uriString of
  32. Just u -> return u
  33. Nothing -> do
  34. hPutStrLn stderr ("Could not parse URI " ++ uriString)
  35. exitWith (ExitFailure 5)
  36. forM_ files (expireFile uri)
  37. usageMessage :: IO ()
  38. usageMessage = do
  39. hPutStrLn stderr $ "Usage: expireGititCache base-url [file..]\n" ++
  40. "Example: expireGititCache http://localhost:5001 page1.page foo/bar.hs"
  41. exitWith (ExitFailure 1)
  42. expireFile :: URI -> FilePath -> IO ()
  43. expireFile uri file = do
  44. let path' = if takeExtension file == ".page"
  45. then dropExtension file
  46. else file
  47. let uri' = uri{uriPath = "/_expire/" ++ urlEncode path'}
  48. resResp <- simpleHTTP Request{rqURI = uri', rqMethod = POST, rqHeaders = [], rqBody = ""}
  49. case resResp of
  50. Left connErr -> error $ show connErr
  51. Right (Response (2,0,0) _ _ _) -> return ()
  52. _ -> do
  53. hPutStrLn stderr ("Request for " ++ show uri' ++ " did not return success status")
  54. exitWith (ExitFailure 3)