2011-01-16 20:05:05 +00:00
|
|
|
{- git-annex command line parsing and dispatch
|
2010-11-02 23:04:24 +00:00
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2010-12-30 19:06:26 +00:00
|
|
|
module CmdLine (
|
2010-12-30 20:52:24 +00:00
|
|
|
dispatch,
|
2010-12-31 00:08:22 +00:00
|
|
|
usage,
|
2011-01-16 20:05:05 +00:00
|
|
|
shutdown
|
2010-12-30 19:06:26 +00:00
|
|
|
) where
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2011-11-16 04:49:09 +00:00
|
|
|
import qualified System.IO.Error as IO
|
|
|
|
import qualified Control.Exception as E
|
|
|
|
import Control.Exception (throw)
|
2010-11-02 23:04:24 +00:00
|
|
|
import System.Console.GetOpt
|
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2010-11-02 23:04:24 +00:00
|
|
|
import qualified Annex
|
2011-10-04 04:40:47 +00:00
|
|
|
import qualified Annex.Queue
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-10-04 04:40:47 +00:00
|
|
|
import Annex.Content
|
2010-11-02 23:04:24 +00:00
|
|
|
import Command
|
|
|
|
|
2011-10-31 00:04:15 +00:00
|
|
|
type Params = [String]
|
|
|
|
type Flags = [Annex ()]
|
|
|
|
|
2010-12-30 20:52:24 +00:00
|
|
|
{- Runs the passed command line. -}
|
2011-11-16 04:49:09 +00:00
|
|
|
dispatch :: Params -> [Command] -> [Option] -> String -> IO Git.Repo -> IO ()
|
|
|
|
dispatch args cmds options header getgitrepo = do
|
2011-03-12 19:30:17 +00:00
|
|
|
setupConsole
|
2011-11-16 04:49:09 +00:00
|
|
|
r <- E.try getgitrepo :: IO (Either E.SomeException Git.Repo)
|
|
|
|
case r of
|
2011-12-09 05:57:13 +00:00
|
|
|
Left e -> fromMaybe (throw e) (cmdnorepo cmd)
|
2011-11-16 04:49:09 +00:00
|
|
|
Right g -> do
|
|
|
|
state <- Annex.new g
|
|
|
|
(actions, state') <- Annex.run state $ do
|
|
|
|
sequence_ flags
|
|
|
|
prepCommand cmd params
|
|
|
|
tryRun state' cmd $ [startup] ++ actions ++ [shutdown]
|
2011-10-31 00:04:15 +00:00
|
|
|
where
|
|
|
|
(flags, cmd, params) = parseCmd args cmds options header
|
2010-12-30 19:44:15 +00:00
|
|
|
|
2011-10-31 00:04:15 +00:00
|
|
|
{- Parses command line, and returns actions to run to configure flags,
|
|
|
|
- the Command being run, and the remaining parameters for the command. -}
|
|
|
|
parseCmd :: Params -> [Command] -> [Option] -> String -> (Flags, Command, Params)
|
|
|
|
parseCmd argv cmds options header = check $ getOpt Permute options argv
|
2010-11-02 23:04:24 +00:00
|
|
|
where
|
2011-10-31 00:04:15 +00:00
|
|
|
check (_, [], []) = err "missing command"
|
|
|
|
check (flags, name:rest, [])
|
|
|
|
| null matches = err $ "unknown command " ++ name
|
|
|
|
| otherwise = (flags, head matches, rest)
|
|
|
|
where
|
|
|
|
matches = filter (\c -> name == cmdname c) cmds
|
|
|
|
check (_, _, errs) = err $ concat errs
|
|
|
|
err msg = error $ msg ++ "\n\n" ++ usage header cmds options
|
2010-12-30 19:06:26 +00:00
|
|
|
|
|
|
|
{- Usage message with lists of commands and options. -}
|
|
|
|
usage :: String -> [Command] -> [Option] -> String
|
2011-10-31 00:04:15 +00:00
|
|
|
usage header cmds options = usageInfo top options ++ commands
|
2010-12-30 19:06:26 +00:00
|
|
|
where
|
2011-10-31 00:04:15 +00:00
|
|
|
top = header ++ "\n\nOptions:"
|
|
|
|
commands = "\nCommands:\n" ++ cmddescs
|
2010-12-30 19:06:26 +00:00
|
|
|
cmddescs = unlines $ map (indent . showcmd) cmds
|
|
|
|
showcmd c =
|
|
|
|
cmdname c ++
|
2010-12-30 19:15:22 +00:00
|
|
|
pad (longest cmdname + 1) (cmdname c) ++
|
2010-12-30 19:06:26 +00:00
|
|
|
cmdparams c ++
|
2010-12-30 19:15:22 +00:00
|
|
|
pad (longest cmdparams + 2) (cmdparams c) ++
|
2010-12-30 19:06:26 +00:00
|
|
|
cmddesc c
|
|
|
|
pad n s = replicate (n - length s) ' '
|
2010-12-30 19:15:22 +00:00
|
|
|
longest f = foldl max 0 $ map (length . f) cmds
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
{- Runs a list of Annex actions. Catches IO errors and continues
|
|
|
|
- (but explicitly thrown errors terminate the whole command).
|
|
|
|
-}
|
2011-10-31 00:04:15 +00:00
|
|
|
tryRun :: Annex.AnnexState -> Command -> [CommandCleanup] -> IO ()
|
2011-07-15 07:12:05 +00:00
|
|
|
tryRun = tryRun' 0
|
2011-10-31 00:04:15 +00:00
|
|
|
tryRun' :: Integer -> Annex.AnnexState -> Command -> [CommandCleanup] -> IO ()
|
|
|
|
tryRun' errnum _ cmd []
|
|
|
|
| errnum > 0 = error $ cmdname cmd ++ ": " ++ show errnum ++ " failed"
|
|
|
|
| otherwise = return ()
|
|
|
|
tryRun' errnum state cmd (a:as) = run >>= handle
|
|
|
|
where
|
2011-11-16 04:49:09 +00:00
|
|
|
run = IO.try $ Annex.run state $ do
|
2011-10-31 00:04:15 +00:00
|
|
|
Annex.Queue.flushWhenFull
|
|
|
|
a
|
|
|
|
handle (Left err) = showerr err >> cont False state
|
|
|
|
handle (Right (success, state')) = cont success state'
|
|
|
|
cont success s = tryRun' (if success then errnum else errnum + 1) s cmd as
|
|
|
|
showerr err = Annex.eval state $ do
|
|
|
|
showErr err
|
|
|
|
showEndFail
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
{- Actions to perform each time ran. -}
|
|
|
|
startup :: Annex Bool
|
2011-08-17 18:14:43 +00:00
|
|
|
startup = return True
|
2011-01-16 20:05:05 +00:00
|
|
|
|
|
|
|
{- Cleanup actions. -}
|
2011-01-30 03:32:32 +00:00
|
|
|
shutdown :: Annex Bool
|
|
|
|
shutdown = do
|
2011-06-23 01:19:52 +00:00
|
|
|
saveState
|
2011-10-02 15:42:34 +00:00
|
|
|
liftIO Git.reap -- zombies from long-running git processes
|
2011-01-30 03:32:32 +00:00
|
|
|
return True
|