converted Assistant and Watch; avoid duplicate arg parsing for no repo mode

This commit is contained in:
Joey Hess 2015-07-13 11:42:42 -04:00
parent 084f8d9ac7
commit b95a48fe45
6 changed files with 71 additions and 70 deletions

View file

@ -98,8 +98,8 @@ import qualified Command.DiffDriver
import qualified Command.Undo import qualified Command.Undo
import qualified Command.Version import qualified Command.Version
#ifdef WITH_ASSISTANT #ifdef WITH_ASSISTANT
--import qualified Command.Watch import qualified Command.Watch
--import qualified Command.Assistant import qualified Command.Assistant
#ifdef WITH_WEBAPP #ifdef WITH_WEBAPP
--import qualified Command.WebApp --import qualified Command.WebApp
#endif #endif
@ -203,8 +203,8 @@ cmds =
, Command.Undo.cmd , Command.Undo.cmd
, Command.Version.cmd , Command.Version.cmd
#ifdef WITH_ASSISTANT #ifdef WITH_ASSISTANT
-- , Command.Watch.cmd , Command.Watch.cmd
-- , Command.Assistant.cmd , Command.Assistant.cmd
#ifdef WITH_WEBAPP #ifdef WITH_WEBAPP
-- , Command.WebApp.cmd -- , Command.WebApp.cmd
#endif #endif

View file

@ -285,3 +285,19 @@ timeLimitOption = globalSetter Limit.addTimeLimit $ strOption
<> help "stop after the specified amount of time" <> help "stop after the specified amount of time"
<> hidden <> 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"
)

View file

@ -1,6 +1,6 @@
{- git-annex assistant {- git-annex assistant
- -
- Copyright 2012 Joey Hess <id@joeyh.name> - Copyright 2012-2015 Joey Hess <id@joeyh.name>
- -
- Licensed under the GNU GPL version 3 or higher. - Licensed under the GNU GPL version 3 or higher.
-} -}
@ -17,67 +17,60 @@ import qualified Build.SysConfig
import Utility.HumanTime import Utility.HumanTime
import Assistant.Install import Assistant.Install
import System.Environment
cmd :: Command cmd :: Command
cmd = dontCheck repoExists $ withOptions options $ notBareRepo $ cmd = dontCheck repoExists $ notBareRepo $
noRepo (withParams checkNoRepoOpts) $ noRepo (startNoRepo <$$> optParser) $
command "assistant" SectionCommon command "assistant" SectionCommon
"automatically sync changes" "automatically sync changes"
paramNothing (withParams seek) paramNothing (seek <$$> optParser)
options :: [Option] data AssistantOptions = AssistantOptions
options = { daemonOptions :: DaemonOptions
[ Command.Watch.foregroundOption , autoStartOption :: Bool
, Command.Watch.stopOption , startDelayOption :: Maybe Duration
, autoStartOption , autoStopOption :: Bool
, startDelayOption }
, autoStopOption
]
autoStartOption :: Option optParser :: CmdParamsDesc -> Parser AssistantOptions
autoStartOption = flagOption [] "autostart" "start in known repositories" 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 seek :: AssistantOptions -> CommandSeek
autoStopOption = flagOption [] "autostop" "stop in known repositories" seek = commandAction . start
startDelayOption :: Option start :: AssistantOptions -> CommandStart
startDelayOption = fieldOption [] "startdelay" paramNumber "delay before running startup scan" start o
| autoStartOption o = do
seek :: CmdParams -> CommandSeek liftIO $ autoStart o
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
stop stop
| autostop = do | autoStopOption o = do
liftIO autoStop liftIO autoStop
stop stop
| otherwise = do | otherwise = do
liftIO ensureInstalled liftIO ensureInstalled
ensureInitialized 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. -} startNoRepo :: AssistantOptions -> IO ()
checkNoRepoOpts :: CmdParams -> IO () startNoRepo o
checkNoRepoOpts _ = ifM (elem "--autostart" <$> getArgs) | autoStartOption o = autoStart o
( autoStart Nothing | autoStopOption o = autoStop
, ifM (elem "--autostop" <$> getArgs) | otherwise = error "Not in a git repository."
( autoStop
, error "Not in a git repository."
)
)
autoStart :: Maybe Duration -> IO () autoStart :: AssistantOptions -> IO ()
autoStart startdelay = do autoStart o = do
dirs <- liftIO readAutoStartFile dirs <- liftIO readAutoStartFile
when (null dirs) $ do when (null dirs) $ do
f <- autoStartFile f <- autoStartFile
@ -105,7 +98,7 @@ autoStart startdelay = do
where where
baseparams = baseparams =
[ Param "assistant" [ Param "assistant"
, Param $ "--startdelay=" ++ fromDuration (fromMaybe (Duration 5) startdelay) , Param $ "--startdelay=" ++ fromDuration (fromMaybe (Duration 5) (startDelayOption o))
] ]
autoStop :: IO () autoStop :: IO ()

View file

@ -13,26 +13,17 @@ import Command
import Utility.HumanTime import Utility.HumanTime
cmd :: Command cmd :: Command
cmd = notBareRepo $ withOptions [foregroundOption, stopOption] $ cmd = notBareRepo $
command "watch" SectionCommon command "watch" SectionCommon
"watch for changes and autocommit" "watch for changes and autocommit"
paramNothing (withParams seek) paramNothing (seek <$$> const parseDaemonOptions)
seek :: CmdParams -> CommandSeek seek :: DaemonOptions -> CommandSeek
seek ps = do seek o = commandAction $ start False o Nothing
stopdaemon <- getOptionFlag stopOption
foreground <- getOptionFlag foregroundOption
withNothing (start False foreground stopdaemon Nothing) ps
foregroundOption :: Option start :: Bool -> DaemonOptions -> Maybe Duration -> CommandStart
foregroundOption = flagOption [] "foreground" "do not daemonize" start assistant o startdelay = do
if stopDaemonOption o
stopOption :: Option
stopOption = flagOption [] "stop" "stop daemon"
start :: Bool -> Bool -> Bool -> Maybe Duration -> CommandStart
start assistant foreground stopdaemon startdelay = do
if stopdaemon
then stopDaemon 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 stop

View file

@ -22,7 +22,7 @@ cmd = noCommit $ dontCheck repoExists $
seek :: CmdParams -> CommandSeek seek :: CmdParams -> CommandSeek
seek = withWords start seek = withWords start
start :: [String] -> CommandStart start :: CmdParams -> CommandStart
start _ = do start _ = do
liftIO gitRemoteHelper liftIO gitRemoteHelper
liftIO xmppGitRelay liftIO xmppGitRelay

1
debian/changelog vendored
View file

@ -6,6 +6,7 @@
* Bash completion code is built-in to git-annex, and can be enabled by * Bash completion code is built-in to git-annex, and can be enabled by
running: source <(git-annex --bash-completion-script git-annex) running: source <(git-annex --bash-completion-script git-annex)
* version --raw now works when run outside a git repository. * 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. * dead now accepts multiple --key options.
git-annex (5.20150710) unstable; urgency=medium git-annex (5.20150710) unstable; urgency=medium