aboutsummaryrefslogtreecommitdiff
path: root/Command/RegisterUrl.hs
blob: 3ff1becc967092208e1bceed7d7bea9e05d7deff (plain)
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
{- git-annex command
 -
 - Copyright 2015 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

{-# LANGUAGE BangPatterns #-}

module Command.RegisterUrl where

import Common.Annex
import Command
import Types.Key
import Logs.Web
import Annex.UUID

cmd :: [Command]
cmd = [notDirect $ notBareRepo $
	command "registerurl" (paramPair paramKey paramUrl) seek
		SectionPlumbing "registers an url for a key"]

seek :: CommandSeek
seek = withWords start

start :: [String] -> CommandStart
start (keyname:url:[]) = do
	let key = fromMaybe (error "bad key") $ file2key keyname
	showStart "registerurl" url
	next $ perform key url
start [] = do
	showStart "registerurl" "stdin"
	next massAdd
start _ = error "specify a key and an url"

massAdd :: CommandPerform
massAdd = go True =<< map words . lines <$> liftIO getContents
  where
	go status [] = next $ return status
	go status ([keyname,u]:rest) = do
		let key = fromMaybe (error $ "bad key " ++ keyname) $ file2key keyname
		ok <- perform' key u
		let !status' = status && ok
		go status' rest
	go _ _ = error "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
	setUrlPresent webUUID key url
	return True