git-annex/git-annex.hs

44 lines
1.4 KiB
Haskell
Raw Normal View History

2010-10-10 00:18:16 -04:00
{- git-annex main program
- -}
2010-10-13 21:28:47 -04:00
import Control.Monad.State
import System.IO
2010-10-10 18:05:37 -04:00
import System.Environment
import Control.Exception
2010-10-10 18:05:37 -04:00
import CmdLine
2010-10-13 21:28:47 -04:00
import Types
2010-10-10 15:04:18 -04:00
import Annex
2010-10-10 00:18:16 -04:00
main = do
2010-10-10 18:05:37 -04:00
args <- getArgs
2010-10-13 21:28:47 -04:00
(mode, params) <- argvToMode args
2010-10-10 18:25:31 -04:00
state <- startAnnex
2010-10-13 21:28:47 -04:00
tryRun state mode 0 0 params
2010-10-10 12:41:20 -04:00
2010-10-13 21:28:47 -04:00
{- Processes each param in the list by dispatching the handler function
- for the user-selection operation mode. Catches exceptions, not stopping
- if some error out, and propigates an overall error status at the end.
-
- This runs in the IO monad, not in the Annex monad. It seems that
- exceptions can only be caught in the IO monad, not in a stacked monad;
- or more likely I missed an easy way to do it. So, I have to laboriously
- thread AnnexState through this function.
-}
tryRun :: AnnexState -> Mode -> Int -> Int -> [String] -> IO ()
tryRun state mode errnum oknum [] = do
2010-10-11 18:39:09 -04:00
if (errnum > 0)
2010-10-11 18:39:36 -04:00
then error $ (show errnum) ++ " failed ; " ++ show (oknum) ++ " ok"
else return ()
2010-10-13 21:28:47 -04:00
tryRun state mode errnum oknum (f:fs) = do
result <- try (runAnnexState state (dispatch mode f))::IO (Either SomeException ((), AnnexState))
case (result) of
Left err -> do
showErr err
2010-10-13 21:28:47 -04:00
tryRun state mode (errnum + 1) oknum fs
Right (_,state') -> tryRun state' mode errnum (oknum + 1) fs
{- Exception pretty-printing. -}
showErr e = do
2010-10-13 16:21:50 -04:00
hPutStrLn stderr $ "git-annex: " ++ (show e)
return ()