From b95a48fe45a240ec0a8039594c3ccb1d1d303cef Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Mon, 13 Jul 2015 11:42:42 -0400 Subject: [PATCH] converted Assistant and Watch; avoid duplicate arg parsing for no repo mode --- CmdLine/GitAnnex.hs | 8 ++-- CmdLine/GitAnnex/Options.hs | 16 +++++++ Command/Assistant.hs | 89 +++++++++++++++++-------------------- Command/Watch.hs | 25 ++++------- Command/XMPPGit.hs | 2 +- debian/changelog | 1 + 6 files changed, 71 insertions(+), 70 deletions(-) diff --git a/CmdLine/GitAnnex.hs b/CmdLine/GitAnnex.hs index ede943804b..28a741b790 100644 --- a/CmdLine/GitAnnex.hs +++ b/CmdLine/GitAnnex.hs @@ -98,8 +98,8 @@ import qualified Command.DiffDriver import qualified Command.Undo import qualified Command.Version #ifdef WITH_ASSISTANT ---import qualified Command.Watch ---import qualified Command.Assistant +import qualified Command.Watch +import qualified Command.Assistant #ifdef WITH_WEBAPP --import qualified Command.WebApp #endif @@ -203,8 +203,8 @@ cmds = , Command.Undo.cmd , Command.Version.cmd #ifdef WITH_ASSISTANT --- , Command.Watch.cmd --- , Command.Assistant.cmd + , Command.Watch.cmd + , Command.Assistant.cmd #ifdef WITH_WEBAPP -- , Command.WebApp.cmd #endif diff --git a/CmdLine/GitAnnex/Options.hs b/CmdLine/GitAnnex/Options.hs index f95a4d03eb..a050f57e39 100644 --- a/CmdLine/GitAnnex/Options.hs +++ b/CmdLine/GitAnnex/Options.hs @@ -285,3 +285,19 @@ timeLimitOption = globalSetter Limit.addTimeLimit $ strOption <> help "stop after the specified amount of time" <> hidden ) + +data DaemonOptions = DaemonOptions + { foregroundDaemonOption :: Bool + , stopDaemonOption :: Bool + } + +parseDaemonOptions :: Parser DaemonOptions +parseDaemonOptions = DaemonOptions + <$> switch + ( long "foreground" + <> help "do not daemonize" + ) + <*> switch + ( long "stop" + <> help "stop daemon" + ) diff --git a/Command/Assistant.hs b/Command/Assistant.hs index 08e96da076..836be7b67d 100644 --- a/Command/Assistant.hs +++ b/Command/Assistant.hs @@ -1,6 +1,6 @@ {- git-annex assistant - - - Copyright 2012 Joey Hess + - Copyright 2012-2015 Joey Hess - - Licensed under the GNU GPL version 3 or higher. -} @@ -17,67 +17,60 @@ import qualified Build.SysConfig import Utility.HumanTime import Assistant.Install -import System.Environment - cmd :: Command -cmd = dontCheck repoExists $ withOptions options $ notBareRepo $ - noRepo (withParams checkNoRepoOpts) $ +cmd = dontCheck repoExists $ notBareRepo $ + noRepo (startNoRepo <$$> optParser) $ command "assistant" SectionCommon "automatically sync changes" - paramNothing (withParams seek) + paramNothing (seek <$$> optParser) -options :: [Option] -options = - [ Command.Watch.foregroundOption - , Command.Watch.stopOption - , autoStartOption - , startDelayOption - , autoStopOption - ] +data AssistantOptions = AssistantOptions + { daemonOptions :: DaemonOptions + , autoStartOption :: Bool + , startDelayOption :: Maybe Duration + , autoStopOption :: Bool + } -autoStartOption :: Option -autoStartOption = flagOption [] "autostart" "start in known repositories" +optParser :: CmdParamsDesc -> Parser AssistantOptions +optParser _ = AssistantOptions + <$> parseDaemonOptions + <*> switch + ( long "autostart" + <> help "start in known repositories" + ) + <*> optional (option (str >>= parseDuration) + ( long "startdelay" <> metavar paramNumber + <> help "delay before running startup scan" + )) + <*> switch + ( long "autostop" + <> help "stop in known repositories" + ) -autoStopOption :: Option -autoStopOption = flagOption [] "autostop" "stop in known repositories" +seek :: AssistantOptions -> CommandSeek +seek = commandAction . start -startDelayOption :: Option -startDelayOption = fieldOption [] "startdelay" paramNumber "delay before running startup scan" - -seek :: CmdParams -> CommandSeek -seek ps = do - stopdaemon <- getOptionFlag Command.Watch.stopOption - foreground <- getOptionFlag Command.Watch.foregroundOption - autostart <- getOptionFlag autoStartOption - autostop <- getOptionFlag autoStopOption - startdelay <- getOptionField startDelayOption (pure . maybe Nothing parseDuration) - withNothing (start foreground stopdaemon autostart autostop startdelay) ps - -start :: Bool -> Bool -> Bool -> Bool -> Maybe Duration -> CommandStart -start foreground stopdaemon autostart autostop startdelay - | autostart = do - liftIO $ autoStart startdelay +start :: AssistantOptions -> CommandStart +start o + | autoStartOption o = do + liftIO $ autoStart o stop - | autostop = do + | autoStopOption o = do liftIO autoStop stop | otherwise = do liftIO ensureInstalled ensureInitialized - Command.Watch.start True foreground stopdaemon startdelay + Command.Watch.start True (daemonOptions o) (startDelayOption o) -{- Run outside a git repository; support autostart and autostop mode. -} -checkNoRepoOpts :: CmdParams -> IO () -checkNoRepoOpts _ = ifM (elem "--autostart" <$> getArgs) - ( autoStart Nothing - , ifM (elem "--autostop" <$> getArgs) - ( autoStop - , error "Not in a git repository." - ) - ) +startNoRepo :: AssistantOptions -> IO () +startNoRepo o + | autoStartOption o = autoStart o + | autoStopOption o = autoStop + | otherwise = error "Not in a git repository." -autoStart :: Maybe Duration -> IO () -autoStart startdelay = do +autoStart :: AssistantOptions -> IO () +autoStart o = do dirs <- liftIO readAutoStartFile when (null dirs) $ do f <- autoStartFile @@ -105,7 +98,7 @@ autoStart startdelay = do where baseparams = [ Param "assistant" - , Param $ "--startdelay=" ++ fromDuration (fromMaybe (Duration 5) startdelay) + , Param $ "--startdelay=" ++ fromDuration (fromMaybe (Duration 5) (startDelayOption o)) ] autoStop :: IO () diff --git a/Command/Watch.hs b/Command/Watch.hs index cc7356ddfe..ac2f273978 100644 --- a/Command/Watch.hs +++ b/Command/Watch.hs @@ -13,26 +13,17 @@ import Command import Utility.HumanTime cmd :: Command -cmd = notBareRepo $ withOptions [foregroundOption, stopOption] $ +cmd = notBareRepo $ command "watch" SectionCommon "watch for changes and autocommit" - paramNothing (withParams seek) + paramNothing (seek <$$> const parseDaemonOptions) -seek :: CmdParams -> CommandSeek -seek ps = do - stopdaemon <- getOptionFlag stopOption - foreground <- getOptionFlag foregroundOption - withNothing (start False foreground stopdaemon Nothing) ps +seek :: DaemonOptions -> CommandSeek +seek o = commandAction $ start False o Nothing -foregroundOption :: Option -foregroundOption = flagOption [] "foreground" "do not daemonize" - -stopOption :: Option -stopOption = flagOption [] "stop" "stop daemon" - -start :: Bool -> Bool -> Bool -> Maybe Duration -> CommandStart -start assistant foreground stopdaemon startdelay = do - if stopdaemon +start :: Bool -> DaemonOptions -> Maybe Duration -> CommandStart +start assistant o startdelay = do + if stopDaemonOption o then stopDaemon - else startDaemon assistant foreground startdelay Nothing Nothing Nothing -- does not return + else startDaemon assistant (foregroundDaemonOption o) startdelay Nothing Nothing Nothing -- does not return stop diff --git a/Command/XMPPGit.hs b/Command/XMPPGit.hs index 86d8dbc112..20e7f07430 100644 --- a/Command/XMPPGit.hs +++ b/Command/XMPPGit.hs @@ -22,7 +22,7 @@ cmd = noCommit $ dontCheck repoExists $ seek :: CmdParams -> CommandSeek seek = withWords start -start :: [String] -> CommandStart +start :: CmdParams -> CommandStart start _ = do liftIO gitRemoteHelper liftIO xmppGitRelay diff --git a/debian/changelog b/debian/changelog index 36cde0735b..71b08394ae 100644 --- a/debian/changelog +++ b/debian/changelog @@ -6,6 +6,7 @@ * Bash completion code is built-in to git-annex, and can be enabled by running: source <(git-annex --bash-completion-script git-annex) * version --raw now works when run outside a git repository. + * assistant --startdelay now works when run outside a git repository. * dead now accepts multiple --key options. git-annex (5.20150710) unstable; urgency=medium