2015-03-20 15:44:46 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2016-02-12 20:43:51 +00:00
|
|
|
- Copyright 2015-2016 Joey Hess <id@joeyh.name>
|
2015-03-20 15:44:46 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.CheckPresentKey where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import qualified Remote
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2016-02-12 20:43:51 +00:00
|
|
|
cmd = noCommit $ noMessages $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "checkpresentkey" SectionPlumbing
|
|
|
|
"check if key is present in remote"
|
2016-02-12 20:43:51 +00:00
|
|
|
(paramPair paramKey (paramOptional paramRemote))
|
|
|
|
(seek <$$> optParser)
|
2015-03-20 15:44:46 +00:00
|
|
|
|
2016-02-12 20:43:51 +00:00
|
|
|
data CheckPresentKeyOptions = CheckPresentKeyOptions
|
|
|
|
{ params :: CmdParams
|
|
|
|
, batchOption :: BatchMode
|
|
|
|
}
|
2015-03-20 15:44:46 +00:00
|
|
|
|
2016-02-12 20:43:51 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser CheckPresentKeyOptions
|
|
|
|
optParser desc = CheckPresentKeyOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> parseBatchOption
|
|
|
|
|
|
|
|
seek :: CheckPresentKeyOptions -> CommandSeek
|
|
|
|
seek o = case batchOption o of
|
|
|
|
NoBatch -> case params o of
|
|
|
|
(ks:rn:[]) -> toRemote rn >>= (check ks . Just) >>= exitResult
|
|
|
|
(ks:[]) -> check ks Nothing >>= exitResult
|
|
|
|
_ -> wrongnumparams
|
|
|
|
Batch -> do
|
|
|
|
checker <- case params o of
|
|
|
|
(rn:[]) -> toRemote rn >>= \r -> return (flip check (Just r))
|
|
|
|
[] -> return (flip check Nothing)
|
|
|
|
_ -> wrongnumparams
|
|
|
|
batchInput Right $ checker >=> batchResult
|
|
|
|
where
|
2016-11-16 01:29:54 +00:00
|
|
|
wrongnumparams = giveup "Wrong number of parameters"
|
2016-02-12 20:43:51 +00:00
|
|
|
|
|
|
|
data Result = Present | NotPresent | CheckFailure String
|
|
|
|
|
|
|
|
check :: String -> Maybe Remote -> Annex Result
|
|
|
|
check ks mr = case mr of
|
|
|
|
Nothing -> go Nothing =<< Remote.keyPossibilities k
|
|
|
|
Just r -> go Nothing [r]
|
2015-03-20 15:44:46 +00:00
|
|
|
where
|
2016-02-12 20:43:51 +00:00
|
|
|
k = toKey ks
|
|
|
|
go Nothing [] = return NotPresent
|
|
|
|
go (Just e) [] = return $ CheckFailure e
|
|
|
|
go olderr (r:rs) = do
|
|
|
|
v <- Remote.hasKey r k
|
|
|
|
case v of
|
|
|
|
Right True -> return Present
|
|
|
|
Right False -> go olderr rs
|
|
|
|
Left e -> go (Just e) rs
|
|
|
|
|
|
|
|
exitResult :: Result -> Annex a
|
|
|
|
exitResult Present = liftIO exitSuccess
|
|
|
|
exitResult NotPresent = liftIO exitFailure
|
|
|
|
exitResult (CheckFailure msg) = liftIO $ do
|
|
|
|
hPutStrLn stderr msg
|
|
|
|
exitWith $ ExitFailure 100
|
|
|
|
|
|
|
|
batchResult :: Result -> Annex ()
|
|
|
|
batchResult Present = liftIO $ putStrLn "1"
|
2016-02-12 20:48:03 +00:00
|
|
|
batchResult _ = liftIO $ putStrLn "0"
|
2016-02-12 20:43:51 +00:00
|
|
|
|
|
|
|
toKey :: String -> Key
|
2016-11-16 01:29:54 +00:00
|
|
|
toKey = fromMaybe (giveup "Bad key") . file2key
|
2016-02-12 20:43:51 +00:00
|
|
|
|
|
|
|
toRemote :: String -> Annex Remote
|
2016-11-16 01:29:54 +00:00
|
|
|
toRemote rn = maybe (giveup "Unknown remote") return
|
2016-02-12 20:43:51 +00:00
|
|
|
=<< Remote.byNameWithUUID (Just rn)
|