examinekey: Added --migrate-to-backend
Note that, the way the SeekInput parser is written to support batch mode, it's actually possible to do git-annex examinekey "SHA1--foo foo.tar.gz" --migrate-to-backend=SHA1E While that might be kind of useful to support multiple migrations not using batch mode, I have not documented it. It would be better to take pairs of key and file in that case.
This commit is contained in:
parent
12e32d1dee
commit
7566aa6bc5
5 changed files with 89 additions and 6 deletions
|
@ -1,6 +1,6 @@
|
|||
{- git-annex command
|
||||
-
|
||||
- Copyright 2013 Joey Hess <id@joeyh.name>
|
||||
- Copyright 2013-2020 Joey Hess <id@joeyh.name>
|
||||
-
|
||||
- Licensed under the GNU AGPL version 3 or higher.
|
||||
-}
|
||||
|
@ -11,6 +11,12 @@ import Command
|
|||
import qualified Utility.Format
|
||||
import Command.Find (parseFormatOption, showFormatted, keyVars)
|
||||
import Annex.Link
|
||||
import Backend
|
||||
import Types.Backend
|
||||
import Types.Key
|
||||
|
||||
import Data.Char
|
||||
import qualified Data.ByteString as B
|
||||
|
||||
cmd :: Command
|
||||
cmd = noCommit $ noMessages $ dontCheck repoExists $
|
||||
|
@ -18,15 +24,56 @@ cmd = noCommit $ noMessages $ dontCheck repoExists $
|
|||
command "examinekey" SectionPlumbing
|
||||
"prints information from a key"
|
||||
(paramRepeating paramKey)
|
||||
(batchable run (optional parseFormatOption))
|
||||
(batchable run optParser)
|
||||
|
||||
run :: Maybe Utility.Format.Format -> SeekInput -> String -> Annex Bool
|
||||
run format _ p = do
|
||||
let k = fromMaybe (giveup "bad key") $ deserializeKey p
|
||||
data ExamineOptions = ExamineOptions
|
||||
{ format :: Maybe Utility.Format.Format
|
||||
, migrateToBackend :: Maybe (DeferredParse Backend)
|
||||
, associatedFile :: AssociatedFile
|
||||
}
|
||||
|
||||
optParser :: Parser ExamineOptions
|
||||
optParser = ExamineOptions
|
||||
<$> optional parseFormatOption
|
||||
<*> (fmap (DeferredParse . tobackend) <$> migrateopt)
|
||||
<*> (AssociatedFile <$> fileopt)
|
||||
where
|
||||
fileopt = optional $ strOption
|
||||
( long "filename" <> metavar paramFile
|
||||
<> help "file associated with the key"
|
||||
)
|
||||
migrateopt = optional $ strOption
|
||||
( long "migrate-to-backend" <> metavar paramName
|
||||
<> help "migrate key to other backend when possible"
|
||||
)
|
||||
tobackend = lookupBackendVariety . parseKeyVariety . encodeBS
|
||||
|
||||
run :: ExamineOptions -> SeekInput -> String -> Annex Bool
|
||||
run o _ input = do
|
||||
k <- getkey
|
||||
|
||||
objectpath <- calcRepo $ gitAnnexLocation k
|
||||
let objectpointer = formatPointer k
|
||||
showFormatted format (serializeKey' k) $
|
||||
showFormatted (format o) (serializeKey' k) $
|
||||
[ ("objectpath", fromRawFilePath objectpath)
|
||||
, ("objectpointer", fromRawFilePath objectpointer)
|
||||
] ++ keyVars k
|
||||
return True
|
||||
where
|
||||
-- Parse the input, which is either a key, or in batch mode
|
||||
-- can be "key filename"
|
||||
(ikb, ifb) = B.break (== (fromIntegral (ord ' '))) (toRawFilePath input)
|
||||
ifb' = B.drop 1 ifb
|
||||
ik = fromMaybe (giveup "bad key") (deserializeKey' ikb)
|
||||
af = if B.null ifb'
|
||||
then associatedFile o
|
||||
else AssociatedFile (Just ifb')
|
||||
|
||||
getkey = case migrateToBackend o of
|
||||
Nothing -> pure ik
|
||||
Just v -> getParsed v >>= \b ->
|
||||
maybeLookupBackendVariety (fromKey keyVariety ik) >>= \case
|
||||
Just ib -> case fastMigrate ib of
|
||||
Just fm -> fromMaybe ik <$> fm ik b af
|
||||
Nothing -> pure ik
|
||||
Nothing -> pure ik
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue