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.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

View file

@ -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"
)

View file

@ -1,6 +1,6 @@
{- 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.
-}
@ -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 ()

View file

@ -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

View file

@ -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

1
debian/changelog vendored
View file

@ -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