rekey: Added --batch mode.
authorJoey Hess <joeyh@joeyh.name>
Mon, 5 Dec 2016 16:55:50 +0000 (12:55 -0400)
committerJoey Hess <joeyh@joeyh.name>
Mon, 5 Dec 2016 16:55:50 +0000 (12:55 -0400)
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.

CHANGELOG
Command/ReKey.hs
Command/RmUrl.hs
doc/git-annex-rekey.mdwn

index 215349f4bd97de05967a06776e4f69a91df3795d..c30447f4e24da54939d83815d7ff5f3fbce1dfed 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -13,6 +13,7 @@ git-annex (6.20161119) UNRELEASED; urgency=medium
   * rmurl: Added --batch mode.
   * fromkey: Accept multiple pairs of files and keys.
     Thanks, Daniel Brooks.
+  * rekey: Added --batch mode.
 
  -- Joey Hess <id@joeyh.name>  Mon, 21 Nov 2016 11:27:50 -0400
 
index 33734ebe79d40abaec5eea2259240d34697b7ebd..4ddbd68b6180637496839a0da56fe8154bd2ae6e 100644 (file)
@@ -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
index 0f33d66b2decb67f7e41a64ce0be01240b86f1d0..1a547a71e2383d73c30e0687b84e8fc2aaf9305b 100644 (file)
@@ -10,7 +10,6 @@ module Command.RmUrl where
 import Command
 import Logs.Web
 import qualified Remote
-import CmdLine.Batch
 
 cmd :: Command
 cmd = notBareRepo $
index 4ec0b49e8436ec49c5494a59945700c6db89589b..ce5e43d419b0cb5670dd461e32c7594d776f1f1d 100644 (file)
@@ -20,6 +20,12 @@ Multiple pairs of file and key can be given in a single command line.
   Allow rekeying of even files whose content is not currently available.
   Use with caution.
 
+* `--batch`
+
+  Enables batch mode, in which lines are read from stdin.
+  Each line should contain the file, and the new key to use for that file,
+  separated by a single space.
+
 # SEE ALSO
 
 [[git-annex]](1)