1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
{- git-annex command
-
- Copyright 2015-2018 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU GPL version 3 or higher.
-}
{-# LANGUAGE BangPatterns #-}
module Command.RegisterUrl where
import Command
import Logs.Web
import Command.FromKey (mkKey)
import qualified Remote
cmd :: Command
cmd = notDirect $ notBareRepo $
command "registerurl"
SectionPlumbing "registers an url for a key"
(paramPair paramKey paramUrl)
(seek <$$> optParser)
data RegisterUrlOptions = RegisterUrlOptions
{ keyUrlPairs :: CmdParams
, batchOption :: BatchMode
}
optParser :: CmdParamsDesc -> Parser RegisterUrlOptions
optParser desc = RegisterUrlOptions
<$> cmdParams desc
<*> parseBatchOption
seek :: RegisterUrlOptions -> CommandSeek
seek o = case (batchOption o, keyUrlPairs o) of
(Batch fmt, _) -> commandAction $ startMass fmt
-- older way of enabling batch input, does not support BatchNull
(NoBatch, []) -> commandAction $ startMass BatchLine
(NoBatch, ps) -> withWords (commandAction . start) ps
start :: [String] -> CommandStart
start (keyname:url:[]) = do
let key = mkKey keyname
showStart' "registerurl" (Just url)
next $ perform key url
start _ = giveup "specify a key and an url"
startMass :: BatchFormat -> CommandStart
startMass fmt = do
showStart' "registerurl" (Just "stdin")
next (massAdd fmt)
massAdd :: BatchFormat -> CommandPerform
massAdd fmt = go True =<< map (separate (== ' ')) <$> batchLines fmt
where
go status [] = next $ return status
go status ((keyname,u):rest) | not (null keyname) && not (null u) = do
let key = mkKey keyname
ok <- perform' key u
let !status' = status && ok
go status' rest
go _ _ = giveup "Expected pairs of key and url on stdin, but got something else."
perform :: Key -> URLString -> CommandPerform
perform key url = do
ok <- perform' key url
next $ return ok
perform' :: Key -> URLString -> Annex Bool
perform' key url = do
r <- Remote.claimingUrl url
setUrlPresent key (setDownloader' url r)
return True
|