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-01-16 20:05:05 +00:00
|
|
|
import System.IO.Error (try)
|
2010-11-02 23:04:24 +00:00
|
|
|
import System.Console.GetOpt
|
2010-12-08 18:07:49 +00:00
|
|
|
import Control.Monad.State (liftIO)
|
2011-01-16 20:05:05 +00:00
|
|
|
import Control.Monad (when, unless)
|
|
|
|
import System.Directory
|
2010-11-02 23:04:24 +00:00
|
|
|
|
|
|
|
import qualified Annex
|
2010-12-30 19:44:15 +00:00
|
|
|
import qualified GitRepo as Git
|
2011-01-16 20:05:05 +00:00
|
|
|
import qualified GitQueue
|
2010-11-02 23:04:24 +00:00
|
|
|
import Types
|
|
|
|
import Command
|
2010-12-30 19:44:15 +00:00
|
|
|
import BackendList
|
|
|
|
import Upgrade
|
2010-12-30 20:52:24 +00:00
|
|
|
import Options
|
2011-01-16 20:05:05 +00:00
|
|
|
import Messages
|
|
|
|
import UUID
|
|
|
|
import Locations
|
2010-11-02 23:04:24 +00:00
|
|
|
|
2010-12-30 20:52:24 +00:00
|
|
|
{- Runs the passed command line. -}
|
2010-12-31 00:08:22 +00:00
|
|
|
dispatch :: Git.Repo -> [String] -> [Command] -> [Option] -> String -> IO ()
|
|
|
|
dispatch gitrepo args cmds options header = do
|
2010-12-30 19:44:15 +00:00
|
|
|
state <- Annex.new gitrepo allBackends
|
|
|
|
(actions, state') <- Annex.run state $ parseCmd args header cmds options
|
|
|
|
tryRun state' $ [startup, upgrade] ++ actions
|
|
|
|
|
2010-12-08 18:07:49 +00:00
|
|
|
{- Parses command line, stores configure flags, and returns a
|
|
|
|
- list of actions to be run in the Annex monad. -}
|
2010-12-30 19:06:26 +00:00
|
|
|
parseCmd :: [String] -> String -> [Command] -> [Option] -> Annex [Annex Bool]
|
|
|
|
parseCmd argv header cmds options = do
|
2010-12-08 18:07:49 +00:00
|
|
|
(flags, params) <- liftIO $ getopt
|
2010-12-30 20:52:24 +00:00
|
|
|
when (null params) $ error $ "missing command" ++ usagemsg
|
2010-11-06 21:06:19 +00:00
|
|
|
case lookupCmd (head params) of
|
2010-12-30 20:52:24 +00:00
|
|
|
[] -> error $ "unknown command" ++ usagemsg
|
2010-12-30 18:19:16 +00:00
|
|
|
[command] -> do
|
2010-12-08 18:07:49 +00:00
|
|
|
_ <- sequence flags
|
2010-12-30 18:19:16 +00:00
|
|
|
prepCmd command (drop 1 params)
|
|
|
|
_ -> error "internal error: multiple matching commands"
|
2010-11-02 23:04:24 +00:00
|
|
|
where
|
|
|
|
getopt = case getOpt Permute options argv of
|
2010-12-30 20:52:24 +00:00
|
|
|
(flags, params, []) ->
|
|
|
|
return (flags, params)
|
|
|
|
(_, _, errs) ->
|
|
|
|
ioError (userError (concat errs ++ usagemsg))
|
2010-12-30 18:19:16 +00:00
|
|
|
lookupCmd cmd = filter (\c -> cmd == cmdname c) cmds
|
2010-12-30 20:52:24 +00:00
|
|
|
usagemsg = "\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
|
|
|
|
usage header cmds options =
|
2010-12-30 20:52:24 +00:00
|
|
|
usageInfo (header ++ "\n\nOptions:") options ++
|
|
|
|
"\nCommands:\n" ++ cmddescs
|
2010-12-30 19:06:26 +00:00
|
|
|
where
|
|
|
|
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
|
|
|
|
indent l = " " ++ l
|
|
|
|
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).
|
|
|
|
- Runs shutdown and propigates an overall error status at the end.
|
|
|
|
-}
|
2011-01-26 01:49:04 +00:00
|
|
|
tryRun :: Annex.AnnexState -> [Annex Bool] -> IO ()
|
2011-01-16 20:05:05 +00:00
|
|
|
tryRun state actions = tryRun' state 0 actions
|
2011-01-26 01:49:04 +00:00
|
|
|
tryRun' :: Annex.AnnexState -> Integer -> [Annex Bool] -> IO ()
|
2011-01-16 20:05:05 +00:00
|
|
|
tryRun' state errnum (a:as) = do
|
|
|
|
result <- try $ Annex.run state a
|
|
|
|
case result of
|
|
|
|
Left err -> do
|
|
|
|
Annex.eval state $ showErr err
|
|
|
|
tryRun' state (errnum + 1) as
|
|
|
|
Right (True,state') -> tryRun' state' errnum as
|
|
|
|
Right (False,state') -> tryRun' state' (errnum + 1) as
|
|
|
|
tryRun' state errnum [] = do
|
|
|
|
_ <- try $ Annex.run state $ shutdown errnum
|
|
|
|
when (errnum > 0) $ error $ show errnum ++ " failed"
|
|
|
|
|
|
|
|
{- Actions to perform each time ran. -}
|
|
|
|
startup :: Annex Bool
|
|
|
|
startup = do
|
|
|
|
prepUUID
|
|
|
|
return True
|
|
|
|
|
|
|
|
{- Cleanup actions. -}
|
|
|
|
shutdown :: Integer -> Annex ()
|
|
|
|
shutdown errnum = do
|
|
|
|
q <- Annex.queueGet
|
|
|
|
unless (q == GitQueue.empty) $ do
|
|
|
|
showSideAction "Recording state in git..."
|
|
|
|
Annex.queueRun
|
|
|
|
|
|
|
|
-- If nothing failed, clean up any files left in the temp directory,
|
|
|
|
-- but leave the directory itself. If something failed, temp files
|
|
|
|
-- are left behind to allow resuming on re-run.
|
|
|
|
when (errnum == 0) $ do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
let tmp = annexTmpLocation g
|
|
|
|
exists <- liftIO $ doesDirectoryExist tmp
|
|
|
|
when exists $ liftIO $ removeDirectoryRecursive tmp
|
|
|
|
liftIO $ createDirectoryIfMissing True tmp
|