diff options
author | Joey Hess <joey@kitenet.net> | 2011-10-27 16:31:35 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2011-10-27 16:31:35 -0400 |
commit | 5b74b130a39d8c45e7d24520d838d6c1635582c7 (patch) | |
tree | 3d2816f028e7135a168d64c6342e1a2588260417 /Command.hs | |
parent | 66194684acaf8dc5c72e5a163465b42050cf9212 (diff) |
refactored and generalized pre-command sanity checking
Diffstat (limited to 'Command.hs')
-rw-r--r-- | Command.hs | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/Command.hs b/Command.hs index f282791fb..d19dad260 100644 --- a/Command.hs +++ b/Command.hs @@ -18,42 +18,38 @@ import Logs.Location import Config import Backend import Limit +import Init -{- A command runs in four stages. +{- A command runs in these stages. - - - 0. The seek stage takes the parameters passed to the command, + - a. The check stage is run once and should error out if anything + - prevents the command from running. -} +type CommandCheck = Annex () +{- b. The seek stage takes the parameters passed to the command, - looks through the repo to find the ones that are relevant - to that command (ie, new files to add), and generates - a list of start stage actions. -} type CommandSeek = [String] -> Annex [CommandStart] -{- 1. The start stage is run before anything is printed about the +{- c. The start stage is run before anything is printed about the - command, is passed some input, and can early abort it - if the input does not make sense. It should run quickly and - should not modify Annex state. -} type CommandStart = Annex (Maybe CommandPerform) -{- 2. The perform stage is run after a message is printed about the command +{- d. The perform stage is run after a message is printed about the command - being run, and it should be where the bulk of the work happens. -} type CommandPerform = Annex (Maybe CommandCleanup) -{- 3. The cleanup stage is run only if the perform stage succeeds, and it +{- e. The cleanup stage is run only if the perform stage succeeds, and it - returns the overall success/fail of the command. -} type CommandCleanup = Annex Bool data Command = Command { - cmdusesrepo :: Bool, cmdname :: String, cmdparams :: String, + cmdcheck :: CommandCheck, cmdseek :: [CommandSeek], cmddesc :: String } -{- Most commands operate on files in a git repo. -} -repoCommand :: String -> String -> [CommandSeek] -> String -> Command -repoCommand = Command True - -{- Others can run anywhere. -} -standaloneCommand :: String -> String -> [CommandSeek] -> String -> Command -standaloneCommand = Command False - {- For start and perform stages to indicate what step to run next. -} next :: a -> Annex (Maybe a) next a = return $ Just a @@ -62,6 +58,18 @@ next a = return $ Just a stop :: Annex (Maybe a) stop = return Nothing +needsNothing :: CommandCheck +needsNothing = return () + +{- Most commands will check this, as they need to be run in an initialized + - repo. -} +needsRepo :: CommandCheck +needsRepo = ensureInitialized + +{- Checks that the command can be run in the current environment. -} +checkCommand :: Command -> Annex () +checkCommand Command { cmdcheck = check } = check + {- Prepares a list of actions to run to perform a command, based on - the parameters passed to it. -} prepCommand :: Command -> [String] -> Annex [Annex Bool] |