2016-01-25 20:16:18 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-07-26 18:34:21 +00:00
|
|
|
- Copyright 2016-2023 Joey Hess <id@joeyh.name>
|
2016-01-25 20:16:18 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2016-01-25 20:16:18 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.MatchExpression where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Annex.FileMatcher
|
|
|
|
import Utility.DataUnits
|
|
|
|
import Annex.UUID
|
|
|
|
import Logs.Group
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
cmd :: Command
|
|
|
|
cmd = noCommit $
|
|
|
|
command "matchexpression" SectionPlumbing
|
|
|
|
"checks if a preferred content expression matches"
|
|
|
|
paramExpression
|
|
|
|
(seek <$$> optParser)
|
|
|
|
|
|
|
|
data MatchExpressionOptions = MatchExpressionOptions
|
|
|
|
{ matchexpr :: String
|
2016-02-03 20:58:36 +00:00
|
|
|
, largeFilesExpression :: Bool
|
2016-01-25 20:16:18 +00:00
|
|
|
, matchinfo :: MatchInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
optParser :: CmdParamsDesc -> Parser MatchExpressionOptions
|
|
|
|
optParser desc = MatchExpressionOptions
|
|
|
|
<$> argument str (metavar desc)
|
2016-02-03 20:58:36 +00:00
|
|
|
<*> switch
|
|
|
|
( long "largefiles"
|
|
|
|
<> help "parse as annex.largefiles expression"
|
|
|
|
)
|
2020-09-28 16:06:10 +00:00
|
|
|
<*> (MatchingUserInfo . addkeysize <$> dataparser)
|
2016-01-25 20:16:18 +00:00
|
|
|
where
|
2020-09-28 16:06:10 +00:00
|
|
|
dataparser = UserProvidedInfo
|
2016-01-25 20:16:18 +00:00
|
|
|
<$> optinfo "file" (strOption
|
|
|
|
( long "file" <> metavar paramFile
|
|
|
|
<> help "specify filename to match against"
|
|
|
|
))
|
|
|
|
<*> optinfo "key" (option (str >>= parseKey)
|
|
|
|
( long "key" <> metavar paramKey
|
|
|
|
<> help "specify key to match against"
|
|
|
|
))
|
|
|
|
<*> optinfo "size" (option (str >>= maybe (fail "parse error") return . readSize dataUnits)
|
|
|
|
( long "size" <> metavar paramSize
|
|
|
|
<> help "specify size to match against"
|
|
|
|
))
|
2016-02-03 20:58:36 +00:00
|
|
|
<*> optinfo "mimetype" (strOption
|
|
|
|
( long "mimetype" <> metavar paramValue
|
|
|
|
<> help "specify mime type to match against"
|
|
|
|
))
|
2019-04-30 15:58:06 +00:00
|
|
|
<*> optinfo "mimeencoding" (strOption
|
|
|
|
( long "mimeencoding" <> metavar paramValue
|
|
|
|
<> help "specify mime encoding to match against"
|
|
|
|
))
|
2016-02-03 20:58:36 +00:00
|
|
|
|
2016-01-25 20:16:18 +00:00
|
|
|
optinfo datadesc mk = (Right <$> mk)
|
|
|
|
<|> (pure $ Left $ missingdata datadesc)
|
|
|
|
missingdata datadesc = bail $ "cannot match this expression without " ++ datadesc ++ " data"
|
2019-04-30 15:58:06 +00:00
|
|
|
-- When a key is provided, make its size also be provided.
|
2020-09-28 16:06:10 +00:00
|
|
|
addkeysize p = case userProvidedKey p of
|
2019-11-22 20:24:04 +00:00
|
|
|
Right k -> case fromKey keySize k of
|
2020-09-28 16:06:10 +00:00
|
|
|
Just sz -> p { userProvidedFileSize = Right sz }
|
2019-04-30 15:58:06 +00:00
|
|
|
Nothing -> p
|
|
|
|
Left _ -> p
|
2016-01-25 20:16:18 +00:00
|
|
|
|
|
|
|
seek :: MatchExpressionOptions -> CommandSeek
|
|
|
|
seek o = do
|
2016-02-03 20:58:36 +00:00
|
|
|
parser <- if largeFilesExpression o
|
2019-12-20 19:01:34 +00:00
|
|
|
then mkMatchExpressionParser
|
2019-05-14 18:01:09 +00:00
|
|
|
else do
|
|
|
|
u <- getUUID
|
|
|
|
pure $ preferredContentParser $ preferredContentTokens $ PCD
|
2019-05-14 18:59:03 +00:00
|
|
|
{ matchStandard = Right matchAll
|
|
|
|
, matchGroupWanted = Right matchAll
|
2019-05-14 18:01:09 +00:00
|
|
|
, getGroupMap = groupMap
|
|
|
|
, configMap = M.empty
|
|
|
|
, repoUUID = Just u
|
|
|
|
}
|
2023-07-26 18:34:21 +00:00
|
|
|
case parsedToMatcher (MatcherDesc "provided expression") $ parser ((matchexpr o)) of
|
2016-01-25 20:16:18 +00:00
|
|
|
Left e -> liftIO $ bail $ "bad expression: " ++ e
|
|
|
|
Right matcher -> ifM (checkmatcher matcher)
|
|
|
|
( liftIO exitSuccess
|
|
|
|
, liftIO exitFailure
|
|
|
|
)
|
|
|
|
where
|
2023-07-26 18:34:21 +00:00
|
|
|
checkmatcher matcher = checkMatcher' matcher (matchinfo o) S.empty
|
2016-01-25 20:16:18 +00:00
|
|
|
|
|
|
|
bail :: String -> IO a
|
|
|
|
bail s = do
|
|
|
|
hPutStrLn stderr s
|
|
|
|
exitWith $ ExitFailure 42
|