refactor
This commit is contained in:
parent
0eae5b806c
commit
df4b461df1
2 changed files with 39 additions and 36 deletions
35
CmdLine.hs
35
CmdLine.hs
|
@ -33,12 +33,6 @@ import qualified Command.SetKey
|
||||||
import qualified Command.Fix
|
import qualified Command.Fix
|
||||||
import qualified Command.Init
|
import qualified Command.Init
|
||||||
|
|
||||||
data SubCommand = SubCommand {
|
|
||||||
subcmdname :: String,
|
|
||||||
subcmdparams :: String,
|
|
||||||
subcmdseek :: SubCmdSeek,
|
|
||||||
subcmddesc :: String
|
|
||||||
}
|
|
||||||
subCmds :: [SubCommand]
|
subCmds :: [SubCommand]
|
||||||
subCmds = [
|
subCmds = [
|
||||||
(SubCommand "add" path (withFilesNotInGit Command.Add.start)
|
(SubCommand "add" path (withFilesNotInGit Command.Add.start)
|
||||||
|
@ -109,35 +103,6 @@ usage = usageInfo header options ++ "\nSubcommands:\n" ++ cmddescs
|
||||||
indent l = " " ++ l
|
indent l = " " ++ l
|
||||||
pad n s = take (n - (length s)) $ repeat ' '
|
pad n s = take (n - (length s)) $ repeat ' '
|
||||||
|
|
||||||
{- Prepares a list of actions to run to perform a subcommand, based on
|
|
||||||
- the parameters passed to it. -}
|
|
||||||
prepSubCmd :: SubCommand -> AnnexState -> [String] -> IO [Annex Bool]
|
|
||||||
prepSubCmd SubCommand { subcmdseek = seek } state params = do
|
|
||||||
list <- Annex.eval state $ seek params
|
|
||||||
return $ map (\a -> doSubCmd a) list
|
|
||||||
|
|
||||||
{- Runs a subcommand through the start, perform and cleanup stages -}
|
|
||||||
doSubCmd :: SubCmdStart -> SubCmdCleanup
|
|
||||||
doSubCmd start = do
|
|
||||||
s <- start
|
|
||||||
case (s) of
|
|
||||||
Nothing -> return True
|
|
||||||
Just perform -> do
|
|
||||||
p <- perform
|
|
||||||
case (p) of
|
|
||||||
Nothing -> do
|
|
||||||
showEndFail
|
|
||||||
return False
|
|
||||||
Just cleanup -> do
|
|
||||||
c <- cleanup
|
|
||||||
if (c)
|
|
||||||
then do
|
|
||||||
showEndOk
|
|
||||||
return True
|
|
||||||
else do
|
|
||||||
showEndFail
|
|
||||||
return False
|
|
||||||
|
|
||||||
{- These functions find appropriate files or other things based on a
|
{- These functions find appropriate files or other things based on a
|
||||||
user's parameters. -}
|
user's parameters. -}
|
||||||
withFilesNotInGit :: SubCmdSeekBackendFiles
|
withFilesNotInGit :: SubCmdSeekBackendFiles
|
||||||
|
|
40
Command.hs
40
Command.hs
|
@ -8,7 +8,9 @@
|
||||||
module Command where
|
module Command where
|
||||||
|
|
||||||
import Types
|
import Types
|
||||||
import Backend
|
import qualified Backend
|
||||||
|
import Core
|
||||||
|
import qualified Annex
|
||||||
|
|
||||||
{- A subcommand runs in four stages.
|
{- A subcommand runs in four stages.
|
||||||
-
|
-
|
||||||
|
@ -35,6 +37,42 @@ type SubCmdStartString = String -> SubCmdStart
|
||||||
type SubCmdSeekBackendFiles = SubCmdStartBackendFile -> SubCmdSeek
|
type SubCmdSeekBackendFiles = SubCmdStartBackendFile -> SubCmdSeek
|
||||||
type SubCmdStartBackendFile = (FilePath, Maybe Backend) -> SubCmdStart
|
type SubCmdStartBackendFile = (FilePath, Maybe Backend) -> SubCmdStart
|
||||||
|
|
||||||
|
data SubCommand = SubCommand {
|
||||||
|
subcmdname :: String,
|
||||||
|
subcmdparams :: String,
|
||||||
|
subcmdseek :: SubCmdSeek,
|
||||||
|
subcmddesc :: String
|
||||||
|
}
|
||||||
|
|
||||||
|
{- Prepares a list of actions to run to perform a subcommand, based on
|
||||||
|
- the parameters passed to it. -}
|
||||||
|
prepSubCmd :: SubCommand -> AnnexState -> [String] -> IO [Annex Bool]
|
||||||
|
prepSubCmd SubCommand { subcmdseek = seek } state params = do
|
||||||
|
list <- Annex.eval state $ seek params
|
||||||
|
return $ map (\a -> doSubCmd a) list
|
||||||
|
|
||||||
|
{- Runs a subcommand through the start, perform and cleanup stages -}
|
||||||
|
doSubCmd :: SubCmdStart -> SubCmdCleanup
|
||||||
|
doSubCmd start = do
|
||||||
|
s <- start
|
||||||
|
case (s) of
|
||||||
|
Nothing -> return True
|
||||||
|
Just perform -> do
|
||||||
|
p <- perform
|
||||||
|
case (p) of
|
||||||
|
Nothing -> do
|
||||||
|
showEndFail
|
||||||
|
return False
|
||||||
|
Just cleanup -> do
|
||||||
|
c <- cleanup
|
||||||
|
if (c)
|
||||||
|
then do
|
||||||
|
showEndOk
|
||||||
|
return True
|
||||||
|
else do
|
||||||
|
showEndFail
|
||||||
|
return False
|
||||||
|
|
||||||
notAnnexed :: FilePath -> Annex (Maybe a) -> Annex (Maybe a)
|
notAnnexed :: FilePath -> Annex (Maybe a) -> Annex (Maybe a)
|
||||||
notAnnexed file a = do
|
notAnnexed file a = do
|
||||||
r <- Backend.lookupFile file
|
r <- Backend.lookupFile file
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue