move global options handling closer to Command definitions

This commit is contained in:
Joey Hess 2021-02-02 15:55:45 -04:00
parent 4cc65d97fc
commit e78d2c9642
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
4 changed files with 20 additions and 15 deletions

View file

@ -12,7 +12,6 @@ import Types.Benchmark
import Types.Command import Types.Command
import CmdLine.Action import CmdLine.Action
import CmdLine import CmdLine
import CmdLine.GitAnnex.Options
import qualified Annex import qualified Annex
import qualified Annex.Branch import qualified Annex.Branch
@ -39,7 +38,7 @@ mkGenerator cmds userinput = do
-- matching or out-of-repo commands. -- matching or out-of-repo commands.
parsesubcommand ps = do parsesubcommand ps = do
(cmd, seek, globalconfig) <- liftIO $ O.handleParseResult $ (cmd, seek, globalconfig) <- liftIO $ O.handleParseResult $
parseCmd "git-annex" "benchmarking" gitAnnexGlobalOptions ps cmds cmdparser parseCmd "git-annex" "benchmarking" ps cmds cmdparser
-- Make an entirely separate Annex state for each subcommand, -- Make an entirely separate Annex state for each subcommand,
-- and prepare it to run the cmd. -- and prepare it to run the cmd.
st <- liftIO . Annex.new =<< Annex.getState Annex.repo st <- liftIO . Annex.new =<< Annex.getState Annex.repo

View file

@ -27,8 +27,8 @@ import Command
import Types.Messages import Types.Messages
{- Runs the passed command line. -} {- Runs the passed command line. -}
dispatch :: Bool -> CmdParams -> [Command] -> [GlobalOption] -> [(String, String)] -> IO Git.Repo -> String -> String -> IO () dispatch :: Bool -> CmdParams -> [Command] -> [(String, String)] -> IO Git.Repo -> String -> String -> IO ()
dispatch fuzzyok allargs allcmds globaloptions fields getgitrepo progname progdesc = do dispatch fuzzyok allargs allcmds fields getgitrepo progname progdesc = do
setupConsole setupConsole
go =<< tryNonAsync getgitrepo go =<< tryNonAsync getgitrepo
where where
@ -61,13 +61,13 @@ dispatch fuzzyok allargs allcmds globaloptions fields getgitrepo progname progde
a a
parsewith secondrun getparser ingitrepo handleresult = parsewith secondrun getparser ingitrepo handleresult =
case parseCmd progname progdesc globaloptions allargs allcmds getparser of case parseCmd progname progdesc allargs allcmds getparser of
O.Failure _ -> do O.Failure _ -> do
-- parse failed, so fall back to -- parse failed, so fall back to
-- fuzzy matching, or to showing usage -- fuzzy matching, or to showing usage
when (fuzzy && not secondrun) $ when (fuzzy && not secondrun) $
ingitrepo autocorrect ingitrepo autocorrect
handleresult (parseCmd progname progdesc globaloptions correctedargs allcmds getparser) handleresult (parseCmd progname progdesc correctedargs allcmds getparser)
res -> handleresult res res -> handleresult res
where where
autocorrect = Git.AutoCorrect.prepare (fromJust inputcmdname) cmdname cmds autocorrect = Git.AutoCorrect.prepare (fromJust inputcmdname) cmdname cmds
@ -84,8 +84,8 @@ dispatch fuzzyok allargs allcmds globaloptions fields getgitrepo progname progde
(fuzzy, cmds) = findCmd fuzzyok allcmds inputcmdname (fuzzy, cmds) = findCmd fuzzyok allcmds inputcmdname
{- Parses command line, selecting one of the commands from the list. -} {- Parses command line, selecting one of the commands from the list. -}
parseCmd :: String -> String -> [GlobalOption] -> CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v, GlobalSetter) parseCmd :: String -> String -> CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v, GlobalSetter)
parseCmd progname progdesc globaloptions allargs allcmds getparser = parseCmd progname progdesc allargs allcmds getparser =
O.execParserPure (O.prefs O.idm) pinfo allargs O.execParserPure (O.prefs O.idm) pinfo allargs
where where
pinfo = O.info (O.helper <*> subcmds) (O.progDescDoc (Just intro)) pinfo = O.info (O.helper <*> subcmds) (O.progDescDoc (Just intro))
@ -96,7 +96,7 @@ parseCmd progname progdesc globaloptions allargs allcmds getparser =
mkparser c = (,,) mkparser c = (,,)
<$> pure c <$> pure c
<*> getparser c <*> getparser c
<*> combineGlobalOptions (globaloptions ++ cmdglobaloptions c) <*> combineGlobalOptions (cmdglobaloptions c)
synopsis n d = n ++ " - " ++ d synopsis n d = n ++ " - " ++ d
intro = mconcat $ concatMap (\l -> [H.text l, H.line]) intro = mconcat $ concatMap (\l -> [H.text l, H.line])
(synopsis progname progdesc : commandList allcmds) (synopsis progname progdesc : commandList allcmds)

View file

@ -126,7 +126,7 @@ import qualified Command.TestRemote
import qualified Command.Benchmark import qualified Command.Benchmark
cmds :: Parser TestOptions -> TestRunner -> MkBenchmarkGenerator -> [Command] cmds :: Parser TestOptions -> TestRunner -> MkBenchmarkGenerator -> [Command]
cmds testoptparser testrunner mkbenchmarkgenerator = cmds testoptparser testrunner mkbenchmarkgenerator = map addGitAnnexGlobalOptions $
[ Command.Help.cmd [ Command.Help.cmd
, Command.Add.cmd , Command.Add.cmd
, Command.Get.cmd , Command.Get.cmd
@ -237,12 +237,15 @@ cmds testoptparser testrunner mkbenchmarkgenerator =
mkbenchmarkgenerator $ cmds testoptparser testrunner (\_ _ -> return noop) mkbenchmarkgenerator $ cmds testoptparser testrunner (\_ _ -> return noop)
] ]
addGitAnnexGlobalOptions :: Command -> Command
addGitAnnexGlobalOptions c = c { cmdglobaloptions = gitAnnexGlobalOptions ++ cmdglobaloptions c }
run :: Parser TestOptions -> TestRunner -> MkBenchmarkGenerator -> [String] -> IO () run :: Parser TestOptions -> TestRunner -> MkBenchmarkGenerator -> [String] -> IO ()
run testoptparser testrunner mkbenchmarkgenerator args = go envmodes run testoptparser testrunner mkbenchmarkgenerator args = go envmodes
where where
go [] = dispatch True args go [] = dispatch True args
(cmds testoptparser testrunner mkbenchmarkgenerator) (cmds testoptparser testrunner mkbenchmarkgenerator)
gitAnnexGlobalOptions [] Git.CurrentRepo.get [] Git.CurrentRepo.get
"git-annex" "git-annex"
"manage files with git, without checking their contents in" "manage files with git, without checking their contents in"
go ((v, a):rest) = maybe (go rest) a =<< getEnv v go ((v, a):rest) = maybe (go rest) a =<< getEnv v

View file

@ -40,7 +40,7 @@ cmdsMap = M.fromList $ map mk
, (ServeReadWrite, allcmds) , (ServeReadWrite, allcmds)
] ]
where where
readonlycmds = readonlycmds = map addGlobalOptions
[ Command.ConfigList.cmd [ Command.ConfigList.cmd
, gitAnnexShellCheck Command.InAnnex.cmd , gitAnnexShellCheck Command.InAnnex.cmd
, gitAnnexShellCheck Command.LockContent.cmd , gitAnnexShellCheck Command.LockContent.cmd
@ -51,11 +51,11 @@ cmdsMap = M.fromList $ map mk
-- determine the security policy to use -- determine the security policy to use
, gitAnnexShellCheck Command.P2PStdIO.cmd , gitAnnexShellCheck Command.P2PStdIO.cmd
] ]
appendcmds = readonlycmds ++ appendcmds = readonlycmds ++ map addGlobalOptions
[ gitAnnexShellCheck Command.RecvKey.cmd [ gitAnnexShellCheck Command.RecvKey.cmd
, gitAnnexShellCheck Command.Commit.cmd , gitAnnexShellCheck Command.Commit.cmd
] ]
allcmds = allcmds = map addGlobalOptions
[ gitAnnexShellCheck Command.DropKey.cmd [ gitAnnexShellCheck Command.DropKey.cmd
, Command.GCryptSetup.cmd , Command.GCryptSetup.cmd
] ]
@ -69,6 +69,9 @@ cmdsFor = fromMaybe [] . flip M.lookup cmdsMap
cmdsList :: [Command] cmdsList :: [Command]
cmdsList = concat $ M.elems cmdsMap cmdsList = concat $ M.elems cmdsMap
addGlobalOptions :: Command -> Command
addGlobalOptions c = c { cmdglobaloptions = globalOptions ++ cmdglobaloptions c }
globalOptions :: [GlobalOption] globalOptions :: [GlobalOption]
globalOptions = globalOptions =
globalSetter checkUUID (strOption globalSetter checkUUID (strOption
@ -119,7 +122,7 @@ builtin cmd dir params = do
let (params', fieldparams, opts) = partitionParams params let (params', fieldparams, opts) = partitionParams params
rsyncopts = ("RsyncOptions", unwords opts) rsyncopts = ("RsyncOptions", unwords opts)
fields = rsyncopts : filter checkField (parseFields fieldparams) fields = rsyncopts : filter checkField (parseFields fieldparams)
dispatch False (cmd : params') cmdsList globalOptions fields mkrepo dispatch False (cmd : params') cmdsList fields mkrepo
"git-annex-shell" "git-annex-shell"
"Restricted login shell for git-annex only SSH access" "Restricted login shell for git-annex only SSH access"
where where