git-annex/git-annex.hs

49 lines
1.4 KiB
Haskell
Raw Normal View History

2010-10-27 20:53:54 +00:00
{- git-annex main program
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
2010-10-10 04:18:16 +00:00
import IO (try)
2010-10-10 22:05:37 +00:00
import System.Environment
2010-10-28 16:40:05 +00:00
import Monad
2010-10-16 20:20:49 +00:00
2010-10-14 07:18:11 +00:00
import qualified Annex
2010-10-14 18:38:29 +00:00
import Types
2010-10-14 07:40:26 +00:00
import Core
2010-10-14 18:38:29 +00:00
import Commands
2010-10-14 20:13:43 +00:00
import qualified GitRepo as Git
import BackendList
2010-10-10 04:18:16 +00:00
2010-10-31 18:32:18 +00:00
main :: IO ()
2010-10-10 04:18:16 +00:00
main = do
2010-10-10 22:05:37 +00:00
args <- getArgs
2010-10-14 20:13:43 +00:00
gitrepo <- Git.repoFromCwd
state <- Annex.new gitrepo allBackends
(configure, actions) <- parseCmd args state
tryRun state $ [startup] ++ configure ++ actions ++ [shutdown]
2010-10-14 18:38:29 +00:00
{- Runs a list of Annex actions. Catches IO errors and continues
- (but explicitly thrown errors terminate the whole command).
- Propigates an overall error status at the end.
2010-10-14 18:38:29 +00:00
-
- 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.
-}
2010-10-25 23:17:11 +00:00
tryRun :: AnnexState -> [Annex Bool] -> IO ()
2010-10-14 21:57:04 +00:00
tryRun state actions = tryRun' state 0 actions
2010-10-31 18:32:18 +00:00
tryRun' :: AnnexState -> Integer -> [Annex Bool] -> IO ()
2010-10-14 21:57:04 +00:00
tryRun' state errnum (a:as) = do
result <- try $ Annex.run state a
2010-10-14 18:38:29 +00:00
case (result) of
Left err -> do
2010-11-01 03:24:16 +00:00
Annex.eval state $ showErr err
2010-10-14 21:57:04 +00:00
tryRun' state (errnum + 1) as
2010-10-25 23:17:11 +00:00
Right (True,state') -> tryRun' state' errnum as
Right (False,state') -> tryRun' state' (errnum + 1) as
2010-10-31 18:32:18 +00:00
tryRun' _ errnum [] =
2010-10-28 16:40:05 +00:00
when (errnum > 0) $ error $ (show errnum) ++ " failed"