2014-02-13 01:12:22 +00:00
|
|
|
{- 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
|
2014-02-13 05:49:38 +00:00
|
|
|
import qualified Annex
|
2014-02-13 01:12:22 +00:00
|
|
|
import Command
|
2014-02-23 04:08:29 +00:00
|
|
|
import Annex.MetaData
|
2014-02-13 01:12:22 +00:00
|
|
|
import Logs.MetaData
|
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2014-02-13 05:57:43 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
2014-02-13 01:12:22 +00:00
|
|
|
|
|
|
|
def :: [Command]
|
2014-03-17 19:26:18 +00:00
|
|
|
def = [withOptions metaDataOptions $
|
2014-02-19 19:04:12 +00:00
|
|
|
command "metadata" paramPaths seek
|
2014-02-19 18:55:34 +00:00
|
|
|
SectionMetaData "sets metadata of a file"]
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2014-03-17 19:26:18 +00:00
|
|
|
metaDataOptions :: [Option]
|
|
|
|
metaDataOptions =
|
|
|
|
[ setOption
|
|
|
|
, tagOption
|
|
|
|
, untagOption
|
|
|
|
, getOption
|
|
|
|
, jsonOption
|
|
|
|
] ++ keyOptions
|
|
|
|
|
2014-02-19 19:04:12 +00:00
|
|
|
storeModMeta :: ModMeta -> Annex ()
|
|
|
|
storeModMeta modmeta = Annex.changeState $
|
|
|
|
\s -> s { Annex.modmeta = modmeta:Annex.modmeta s }
|
|
|
|
|
2014-02-13 05:49:38 +00:00
|
|
|
setOption :: Option
|
2014-02-13 06:24:30 +00:00
|
|
|
setOption = Option ['s'] ["set"] (ReqArg mkmod "FIELD[+-]=VALUE") "set metadata"
|
2014-02-13 05:49:38 +00:00
|
|
|
where
|
2014-02-19 19:04:12 +00:00
|
|
|
mkmod = either error storeModMeta . parseModMeta
|
|
|
|
|
2014-03-15 21:29:40 +00:00
|
|
|
getOption :: Option
|
|
|
|
getOption = fieldOption ['g'] "get" paramField "get single metadata field"
|
|
|
|
|
2014-02-19 19:04:12 +00:00
|
|
|
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)
|
2014-02-13 05:49:38 +00:00
|
|
|
|
2014-02-13 01:12:22 +00:00
|
|
|
seek :: CommandSeek
|
2014-02-13 05:49:38 +00:00
|
|
|
seek ps = do
|
|
|
|
modmeta <- Annex.getState Annex.modmeta
|
2014-03-15 21:29:40 +00:00
|
|
|
getfield <- getOptionField getOption $ \ms ->
|
|
|
|
return $ either error id . mkMetaField <$> ms
|
2014-02-13 05:57:43 +00:00
|
|
|
now <- liftIO getPOSIXTime
|
2014-03-17 19:26:18 +00:00
|
|
|
withKeyOptions
|
|
|
|
(startKeys now getfield modmeta)
|
|
|
|
(withFilesInGit (whenAnnexed $ start now getfield modmeta))
|
|
|
|
ps
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2014-04-17 22:03:39 +00:00
|
|
|
start :: POSIXTime -> Maybe MetaField -> [ModMeta] -> FilePath -> Key -> CommandStart
|
|
|
|
start now f ms file = start' (Just file) now f ms
|
2014-03-17 19:26:18 +00:00
|
|
|
|
|
|
|
startKeys :: POSIXTime -> Maybe MetaField -> [ModMeta] -> Key -> CommandStart
|
|
|
|
startKeys = start' Nothing
|
|
|
|
|
|
|
|
start' :: AssociatedFile -> POSIXTime -> Maybe MetaField -> [ModMeta] -> Key -> CommandStart
|
|
|
|
start' afile now Nothing ms k = do
|
|
|
|
showStart' "metadata" k afile
|
2014-02-13 05:57:43 +00:00
|
|
|
next $ perform now ms k
|
2014-03-17 19:26:18 +00:00
|
|
|
start' _ _ (Just f) _ k = do
|
2014-03-15 21:29:40 +00:00
|
|
|
l <- S.toList . currentMetaDataValues f <$> getCurrentMetaData k
|
|
|
|
liftIO $ forM_ l $
|
|
|
|
putStrLn . fromMetaValue
|
|
|
|
stop
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2014-02-13 05:57:43 +00:00
|
|
|
perform :: POSIXTime -> [ModMeta] -> Key -> CommandPerform
|
|
|
|
perform _ [] k = next $ cleanup k
|
|
|
|
perform now ms k = do
|
2014-02-13 01:48:25 +00:00
|
|
|
oldm <- getCurrentMetaData k
|
2014-03-18 22:55:43 +00:00
|
|
|
let m = combineMetaData $ map (modMeta oldm) ms
|
2014-02-13 05:57:43 +00:00
|
|
|
addMetaData' k m now
|
2014-02-13 01:48:25 +00:00
|
|
|
next $ cleanup k
|
2014-02-13 01:12:22 +00:00
|
|
|
|
2014-02-13 01:48:25 +00:00
|
|
|
cleanup :: Key -> CommandCleanup
|
|
|
|
cleanup k = do
|
2014-02-23 17:58:16 +00:00
|
|
|
l <- map unwrapmeta . fromMetaData <$> getCurrentMetaData k
|
|
|
|
maybeShowJSON l
|
|
|
|
showLongNote $ unlines $ concatMap showmeta l
|
2014-02-13 01:12:22 +00:00
|
|
|
return True
|
|
|
|
where
|
2014-02-23 17:58:16 +00:00
|
|
|
unwrapmeta (f, v) = (fromMetaField f, map fromMetaValue (S.toList v))
|
|
|
|
showmeta (f, vs) = map ((f ++ "=") ++) vs
|