File: RmUrl.hs

package info (click to toggle)
git-annex 10.20230126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 69,344 kB
  • sloc: haskell: 74,654; javascript: 9,103; sh: 1,304; makefile: 203; perl: 136; ansic: 44
file content (64 lines) | stat: -rw-r--r-- 1,797 bytes parent folder | download
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
{- git-annex command
 -
 - Copyright 2013-2021 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Command.RmUrl where

import Command
import Logs.Web
import Annex.WorkTree

cmd :: Command
cmd = notBareRepo $
	command "rmurl" SectionCommon 
		"record file is not available at url"
		(paramRepeating (paramPair paramFile paramUrl))
		(seek <$$> optParser)

data RmUrlOptions = RmUrlOptions
	{ rmThese :: CmdParams
	, batchOption :: BatchMode
	}

optParser :: CmdParamsDesc -> Parser RmUrlOptions
optParser desc = RmUrlOptions
	<$> cmdParams desc
	<*> parseBatchOption False

seek :: RmUrlOptions -> CommandSeek
seek o = case batchOption o of
	Batch fmt -> batchOnly Nothing (rmThese o) $
		batchInput fmt batchParser (batchCommandAction . start)
	NoBatch -> withPairs (commandAction . start) (rmThese o)

-- Split on the last space, since a FilePath can contain whitespace,
-- but a url should not.
batchParser :: String -> Annex (Either String (FilePath, URLString))
batchParser s = case separate (== ' ') (reverse s) of
	(ru, rf)
		| null ru || null rf -> return $ Left "Expected: \"file url\""
		| otherwise -> do
			let f = reverse rf
			f' <- liftIO $ fromRawFilePath
				<$> relPathCwdToFile (toRawFilePath f)
			return $ Right (f', reverse ru)

start :: (SeekInput, (FilePath, URLString)) -> CommandStart
start (si, (file, url)) = lookupKeyStaged file' >>= \case
	Nothing -> stop
	Just key -> do
		let ai = mkActionItem (key, AssociatedFile (Just file'))
		starting "rmurl" ai si $
			next $ cleanup url key
  where
	file' = toRawFilePath file

cleanup :: String -> Key -> CommandCleanup
cleanup url key = do
	-- Remove the url, no matter what downloader.
	forM_ [minBound..maxBound] $ \dl -> 
		setUrlMissing key (setDownloader url dl)
	return True