diff options
author | Joey Hess <joeyh@joeyh.name> | 2016-12-05 12:55:50 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2016-12-05 12:55:50 -0400 |
commit | 41ef0a573bd50bb4c2f984834e7af9f97c596662 (patch) | |
tree | 92b1d5288b8d6596235840c25a3f90883c8578d2 /Command/ReKey.hs | |
parent | b9c88044bcc9e2b409ee2872d4b75c26ce6e821f (diff) |
rekey: Added --batch mode.
Would have liked to make the Parser parse the file and key pairs, but it
seems that optparse-applicative is unable to handle eg:
many ((,) <$> argument <*> argument)
This commit was sponsored by Thomas Hochstein on Patreon.
Diffstat (limited to 'Command/ReKey.hs')
-rw-r--r-- | Command/ReKey.hs | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/Command/ReKey.hs b/Command/ReKey.hs index 33734ebe7..4ddbd68b6 100644 --- a/Command/ReKey.hs +++ b/Command/ReKey.hs @@ -25,15 +25,39 @@ cmd = notDirect $ command "rekey" SectionPlumbing "change keys used for files" (paramRepeating $ paramPair paramPath paramKey) - (withParams seek) + (seek <$$> optParser) -seek :: CmdParams -> CommandSeek -seek = withPairs start +data ReKeyOptions = ReKeyOptions + { reKeyThese :: CmdParams + , batchOption :: BatchMode + } -start :: (FilePath, String) -> CommandStart -start (file, keyname) = ifAnnexed file go stop +optParser :: CmdParamsDesc -> Parser ReKeyOptions +optParser desc = ReKeyOptions + <$> cmdParams desc + <*> parseBatchOption + +-- Split on the last space, since a FilePath can contain whitespace, +-- but a Key very rarely does. +batchParser :: String -> Either String (FilePath, Key) +batchParser s = case separate (== ' ') (reverse s) of + (rk, rf) + | null rk || null rf -> Left "Expected: \"file key\"" + | otherwise -> case file2key (reverse rk) of + Nothing -> Left "bad key" + Just k -> Right (reverse rf, k) + +seek :: ReKeyOptions -> CommandSeek +seek o = case batchOption o of + Batch -> batchInput batchParser (batchCommandAction . start) + NoBatch -> withPairs (start . parsekey) (reKeyThese o) + where + parsekey (file, skey) = + (file, fromMaybe (giveup "bad key") (file2key skey)) + +start :: (FilePath, Key) -> CommandStart +start (file, newkey) = ifAnnexed file go stop where - newkey = fromMaybe (giveup "bad key") $ file2key keyname go oldkey | oldkey == newkey = stop | otherwise = do |