summaryrefslogtreecommitdiff
path: root/Command/AddUrl.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Command/AddUrl.hs')
-rw-r--r--Command/AddUrl.hs37
1 files changed, 23 insertions, 14 deletions
diff --git a/Command/AddUrl.hs b/Command/AddUrl.hs
index 46584f0d8..600a6169d 100644
--- a/Command/AddUrl.hs
+++ b/Command/AddUrl.hs
@@ -17,26 +17,39 @@ import qualified Annex
import qualified Backend.URL
import Annex.Content
import Logs.Web
+import qualified Option
def :: [Command]
-def = [command "addurl" (paramRepeating paramUrl) seek "add urls to annex"]
+def = [withOptions [fileOption] $
+ command "addurl" (paramRepeating paramUrl) seek "add urls to annex"]
+
+fileOption :: Option
+fileOption = Option.field [] "file" paramFile "specify what file the url is added to"
seek :: [CommandSeek]
-seek = [withStrings start]
+seek = [withField fileOption return $ \f ->
+ withStrings $ start f]
-start :: String -> CommandStart
-start s = notBareRepo $ go $ parseURI s
+start :: Maybe FilePath -> String -> CommandStart
+start optfile s = notBareRepo $ go $ parseURI s
where
go Nothing = error $ "bad url " ++ s
go (Just url) = do
- file <- liftIO $ url2file url
+ let file = fromMaybe (url2file url) optfile
showStart "addurl" file
next $ perform s file
perform :: String -> FilePath -> CommandPerform
-perform url file = do
- fast <- Annex.getState Annex.fast
- if fast then nodownload url file else download url file
+perform url file = ifAnnexed file addurl geturl
+ where
+ geturl = do
+ whenM (liftIO $ doesFileExist file) $
+ error $ "already have this url in " ++ file
+ fast <- Annex.getState Annex.fast
+ if fast then nodownload url file else download url file
+ addurl (key, _backend) = do
+ setUrlPresent key url
+ next $ return True
download :: String -> FilePath -> CommandPerform
download url file = do
@@ -60,12 +73,8 @@ nodownload url file = do
setUrlPresent key url
next $ Command.Add.cleanup file key False
-url2file :: URI -> IO FilePath
-url2file url = do
- whenM (doesFileExist file) $
- error $ "already have this url in " ++ file
- return file
+url2file :: URI -> FilePath
+url2file url = escape $ uriRegName auth ++ uriPath url ++ uriQuery url
where
- file = escape $ uriRegName auth ++ uriPath url ++ uriQuery url
escape = replace "/" "_" . replace "?" "_"
auth = fromMaybe (error $ "bad url " ++ show url) $ uriAuthority url