converted fsck's options to optparse-applicative
Global options and seeking and key options are still to be done.
This commit is contained in:
parent
b59b8be737
commit
6a88c7c101
4 changed files with 72 additions and 45 deletions
|
@ -75,8 +75,13 @@ dispatch fuzzyok allargs allcmds commonoptions fields header getgitrepo = do
|
||||||
parseCmd :: CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v)
|
parseCmd :: CmdParams -> [Command] -> (Command -> O.Parser v) -> O.ParserResult (Command, v)
|
||||||
parseCmd allargs allcmds getparser = O.execParserPure (O.prefs O.idm) pinfo allargs
|
parseCmd allargs allcmds getparser = O.execParserPure (O.prefs O.idm) pinfo allargs
|
||||||
where
|
where
|
||||||
pinfo = O.info (O.subparser $ mconcat $ map mkcommand allcmds) O.idm
|
pinfo = O.info (O.subparser $ mconcat $ map mkcommand allcmds)
|
||||||
mkcommand c = O.command (cmdname c) (O.info (mkparser c) O.idm)
|
( O.fullDesc
|
||||||
|
<> O.progDesc "hiya"
|
||||||
|
<> O.header "ook - aaa"
|
||||||
|
)
|
||||||
|
mkcommand c = O.command (cmdname c) $ O.info (mkparser c)
|
||||||
|
(O.fullDesc <> O.progDesc (cmddesc c))
|
||||||
mkparser c = (,)
|
mkparser c = (,)
|
||||||
<$> pure c
|
<$> pure c
|
||||||
<*> getparser c
|
<*> getparser c
|
||||||
|
|
24
Command.hs
24
Command.hs
|
@ -1,6 +1,6 @@
|
||||||
{- git-annex command infrastructure
|
{- git-annex command infrastructure
|
||||||
-
|
-
|
||||||
- Copyright 2010-2014 Joey Hess <id@joeyh.name>
|
- Copyright 2010-2015 Joey Hess <id@joeyh.name>
|
||||||
-
|
-
|
||||||
- Licensed under the GNU GPL version 3 or higher.
|
- Licensed under the GNU GPL version 3 or higher.
|
||||||
-}
|
-}
|
||||||
|
@ -8,6 +8,8 @@
|
||||||
module Command (
|
module Command (
|
||||||
command,
|
command,
|
||||||
withParams,
|
withParams,
|
||||||
|
cmdParams,
|
||||||
|
finalOpt,
|
||||||
noRepo,
|
noRepo,
|
||||||
noCommit,
|
noCommit,
|
||||||
noMessages,
|
noMessages,
|
||||||
|
@ -36,16 +38,24 @@ import CmdLine.GitAnnex.Options as ReExported
|
||||||
import qualified Options.Applicative as O
|
import qualified Options.Applicative as O
|
||||||
|
|
||||||
{- Generates a normal Command -}
|
{- Generates a normal Command -}
|
||||||
command :: String -> CommandSection -> String -> String -> (String -> CommandParser) -> Command
|
command :: String -> CommandSection -> String -> CmdParamsDesc -> (CmdParamsDesc -> CommandParser) -> Command
|
||||||
command name section desc paramdesc mkparser =
|
command name section desc paramdesc mkparser =
|
||||||
Command [] commonChecks False False name paramdesc
|
Command [] commonChecks False False name paramdesc
|
||||||
section desc (mkparser paramdesc) Nothing
|
section desc (mkparser paramdesc) Nothing
|
||||||
|
|
||||||
{- Option parser that takes all non-option params as-is. -}
|
{- Simple option parser that takes all non-option params as-is. -}
|
||||||
withParams :: (CmdParams -> v) -> String -> O.Parser v
|
withParams :: (CmdParams -> v) -> CmdParamsDesc -> O.Parser v
|
||||||
withParams mkseek paramdesc = mkseek <$> O.many cmdparams
|
withParams mkseek paramdesc = mkseek <$> cmdParams paramdesc
|
||||||
where
|
|
||||||
cmdparams = O.argument O.str (O.metavar paramdesc)
|
{- Parser that accepts all non-option params. -}
|
||||||
|
cmdParams :: CmdParamsDesc -> O.Parser CmdParams
|
||||||
|
cmdParams paramdesc = O.many (O.argument O.str (O.metavar paramdesc))
|
||||||
|
|
||||||
|
{- Makes an option parser that is normally required be optional;
|
||||||
|
- its switch can be given zero or more times, and the last one
|
||||||
|
- given will be used. -}
|
||||||
|
finalOpt :: O.Parser a -> O.Parser (Maybe a)
|
||||||
|
finalOpt = lastMaybe <$$> O.many
|
||||||
|
|
||||||
{- Indicates that a command doesn't need to commit any changes to
|
{- Indicates that a command doesn't need to commit any changes to
|
||||||
- the git-annex branch. -}
|
- the git-annex branch. -}
|
||||||
|
|
|
@ -39,42 +39,56 @@ import qualified Database.Fsck as FsckDb
|
||||||
|
|
||||||
import Data.Time.Clock.POSIX
|
import Data.Time.Clock.POSIX
|
||||||
import System.Posix.Types (EpochTime)
|
import System.Posix.Types (EpochTime)
|
||||||
|
import Options.Applicative hiding (command)
|
||||||
|
|
||||||
cmd :: Command
|
cmd :: Command
|
||||||
cmd = withOptions fsckOptions $
|
cmd = command "fsck" SectionMaintenance "check for problems"
|
||||||
command "fsck" SectionMaintenance "check for problems"
|
paramPaths (seek <$$> optParser)
|
||||||
paramPaths (withParams seek)
|
|
||||||
|
|
||||||
fsckFromOption :: Option
|
data FsckOptions = FsckOptions
|
||||||
fsckFromOption = fieldOption ['f'] "from" paramRemote "check remote"
|
{ fsckFiles :: CmdParams
|
||||||
|
, fsckFromOption :: Maybe String
|
||||||
|
, startIncrementalOption :: Bool
|
||||||
|
, moreIncrementalOption :: Bool
|
||||||
|
, incrementalScheduleOption :: Maybe Duration
|
||||||
|
}
|
||||||
|
|
||||||
startIncrementalOption :: Option
|
optParser :: CmdParamsDesc -> Parser FsckOptions
|
||||||
startIncrementalOption = flagOption ['S'] "incremental" "start an incremental fsck"
|
optParser desc = FsckOptions
|
||||||
|
<$> cmdParams desc
|
||||||
|
<*> finalOpt (strOption
|
||||||
|
( long "from"
|
||||||
|
<> short 'f'
|
||||||
|
<> metavar paramRemote
|
||||||
|
<> help "check remote"
|
||||||
|
))
|
||||||
|
<*> switch
|
||||||
|
( long "incremental"
|
||||||
|
<> short 'S'
|
||||||
|
<> help "start an incremental fsck"
|
||||||
|
)
|
||||||
|
<*> switch
|
||||||
|
( long "more"
|
||||||
|
<> short 'm'
|
||||||
|
<> help "continue an incremental fsck"
|
||||||
|
)
|
||||||
|
<*> finalOpt (option (str >>= parseDuration)
|
||||||
|
( long "incremental-schedule"
|
||||||
|
<> metavar paramTime
|
||||||
|
<> help "schedule incremental fscking"
|
||||||
|
))
|
||||||
|
|
||||||
moreIncrementalOption :: Option
|
-- TODO: keyOptions, annexedMatchingOptions
|
||||||
moreIncrementalOption = flagOption ['m'] "more" "continue an incremental fsck"
|
|
||||||
|
|
||||||
incrementalScheduleOption :: Option
|
seek :: FsckOptions -> CommandSeek
|
||||||
incrementalScheduleOption = fieldOption [] "incremental-schedule" paramTime
|
seek o = do
|
||||||
"schedule incremental fscking"
|
from <- Remote.byNameWithUUID (fsckFromOption o)
|
||||||
|
|
||||||
fsckOptions :: [Option]
|
|
||||||
fsckOptions =
|
|
||||||
[ fsckFromOption
|
|
||||||
, startIncrementalOption
|
|
||||||
, moreIncrementalOption
|
|
||||||
, incrementalScheduleOption
|
|
||||||
] ++ keyOptions ++ annexedMatchingOptions
|
|
||||||
|
|
||||||
seek :: CmdParams -> CommandSeek
|
|
||||||
seek ps = do
|
|
||||||
from <- getOptionField fsckFromOption Remote.byNameWithUUID
|
|
||||||
u <- maybe getUUID (pure . Remote.uuid) from
|
u <- maybe getUUID (pure . Remote.uuid) from
|
||||||
i <- getIncremental u
|
i <- getIncremental u o
|
||||||
withKeyOptions False
|
withKeyOptions False
|
||||||
(\k -> startKey i k =<< getNumCopies)
|
(\k -> startKey i k =<< getNumCopies)
|
||||||
(withFilesInGit $ whenAnnexed $ start from i)
|
(withFilesInGit $ whenAnnexed $ start from i)
|
||||||
ps
|
(fsckFiles o)
|
||||||
withFsckDb i FsckDb.closeDb
|
withFsckDb i FsckDb.closeDb
|
||||||
void $ tryIO $ recordActivity Fsck u
|
void $ tryIO $ recordActivity Fsck u
|
||||||
|
|
||||||
|
@ -498,13 +512,10 @@ getStartTime u = do
|
||||||
|
|
||||||
data Incremental = StartIncremental FsckDb.FsckHandle | ContIncremental FsckDb.FsckHandle | NonIncremental
|
data Incremental = StartIncremental FsckDb.FsckHandle | ContIncremental FsckDb.FsckHandle | NonIncremental
|
||||||
|
|
||||||
getIncremental :: UUID -> Annex Incremental
|
getIncremental :: UUID -> FsckOptions -> Annex Incremental
|
||||||
getIncremental u = do
|
getIncremental u o = do
|
||||||
i <- maybe (return False) (checkschedule . parseDuration)
|
i <- maybe (return False) checkschedule (incrementalScheduleOption o)
|
||||||
=<< Annex.getField (optionName incrementalScheduleOption)
|
case (i, startIncrementalOption o, moreIncrementalOption o) of
|
||||||
starti <- getOptionFlag startIncrementalOption
|
|
||||||
morei <- getOptionFlag moreIncrementalOption
|
|
||||||
case (i, starti, morei) of
|
|
||||||
(False, False, False) -> return NonIncremental
|
(False, False, False) -> return NonIncremental
|
||||||
(False, True, False) -> startIncremental
|
(False, True, False) -> startIncremental
|
||||||
(False ,False, True) -> contIncremental
|
(False ,False, True) -> contIncremental
|
||||||
|
@ -521,8 +532,7 @@ getIncremental u = do
|
||||||
)
|
)
|
||||||
contIncremental = ContIncremental <$> FsckDb.openDb u
|
contIncremental = ContIncremental <$> FsckDb.openDb u
|
||||||
|
|
||||||
checkschedule Nothing = error "bad --incremental-schedule value"
|
checkschedule delta = do
|
||||||
checkschedule (Just delta) = do
|
|
||||||
Annex.addCleanup FsckCleanup $ do
|
Annex.addCleanup FsckCleanup $ do
|
||||||
v <- getStartTime u
|
v <- getStartTime u
|
||||||
case v of
|
case v of
|
||||||
|
|
|
@ -43,7 +43,7 @@ data Command = Command
|
||||||
, cmdnocommit :: Bool -- don't commit journalled state changes
|
, cmdnocommit :: Bool -- don't commit journalled state changes
|
||||||
, cmdnomessages :: Bool -- don't output normal messages
|
, cmdnomessages :: Bool -- don't output normal messages
|
||||||
, cmdname :: String
|
, cmdname :: String
|
||||||
, cmdparamdesc :: String -- description of params for usage
|
, cmdparamdesc :: CmdParamsDesc -- description of params for usage
|
||||||
, cmdsection :: CommandSection
|
, cmdsection :: CommandSection
|
||||||
, cmddesc :: String -- description of command for usage
|
, cmddesc :: String -- description of command for usage
|
||||||
, cmdparser :: CommandParser -- command line parser
|
, cmdparser :: CommandParser -- command line parser
|
||||||
|
@ -54,6 +54,8 @@ data Command = Command
|
||||||
- are parsed. -}
|
- are parsed. -}
|
||||||
type CmdParams = [String]
|
type CmdParams = [String]
|
||||||
|
|
||||||
|
type CmdParamsDesc = String
|
||||||
|
|
||||||
{- CommandCheck functions can be compared using their unique id. -}
|
{- CommandCheck functions can be compared using their unique id. -}
|
||||||
instance Eq CommandCheck where
|
instance Eq CommandCheck where
|
||||||
a == b = idCheck a == idCheck b
|
a == b = idCheck a == idCheck b
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue