summaryrefslogtreecommitdiff
path: root/Command/MetaData.hs
blob: 55d67c6b7719b426d8840e64995aa8d7e4680976 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{- git-annex command
 -
 - Copyright 2014 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Command.MetaData where

import Common.Annex
import qualified Annex
import Command
import Annex.MetaData
import Logs.MetaData
import Types.MetaData

import qualified Data.Set as S
import Data.Time.Clock.POSIX

def :: [Command]
def = [withOptions [setOption, tagOption, untagOption, jsonOption] $
	command "metadata" paramPaths seek
	SectionMetaData "sets metadata of a file"]

storeModMeta :: ModMeta -> Annex ()
storeModMeta modmeta = Annex.changeState $
	\s -> s { Annex.modmeta = modmeta:Annex.modmeta s }

setOption :: Option
setOption = Option ['s'] ["set"] (ReqArg mkmod "FIELD[+-]=VALUE") "set metadata"
  where
	mkmod = either error storeModMeta . parseModMeta

tagOption :: Option
tagOption = Option ['t'] ["tag"] (ReqArg mkmod "TAG") "set a tag"
  where
	mkmod = storeModMeta . AddMeta tagMetaField . toMetaValue

untagOption :: Option
untagOption = Option ['u'] ["untag"] (ReqArg mkmod "TAG") "remove a tag"
  where
	mkmod = storeModMeta . AddMeta tagMetaField . mkMetaValue (CurrentlySet False)

seek :: CommandSeek
seek ps = do
	modmeta <- Annex.getState Annex.modmeta
	now <- liftIO getPOSIXTime
	withFilesInGit (whenAnnexed $ start now modmeta) ps

start :: POSIXTime -> [ModMeta] -> FilePath -> (Key, Backend) -> CommandStart
start now ms file (k, _) = do
	showStart "metadata" file
	next $ perform now ms k

perform :: POSIXTime -> [ModMeta] -> Key -> CommandPerform
perform _ [] k = next $ cleanup k
perform now ms k = do
	oldm <- getCurrentMetaData k
	let m = foldl' unionMetaData emptyMetaData $ map (modMeta oldm) ms
	addMetaData' k m now
	next $ cleanup k
	
cleanup :: Key -> CommandCleanup
cleanup k = do
	l <- map unwrapmeta . fromMetaData <$> getCurrentMetaData k
	maybeShowJSON l
	showLongNote $ unlines $ concatMap showmeta l
	return True
  where
	unwrapmeta (f, v) = (fromMetaField f, map fromMetaValue (S.toList v))
	showmeta (f, vs) = map ((f ++ "=") ++) vs