File: ExamineKey.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 (79 lines) | stat: -rw-r--r-- 2,303 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{- git-annex command
 -
 - Copyright 2013-2020 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Command.ExamineKey where

import Command
import qualified Utility.Format
import Command.Find (parseFormatOption, showFormatted, formatVars)
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 $ 
	withAnnexOptions [jsonOptions] $
		command "examinekey" SectionPlumbing 
			"prints information from a key"
			(paramRepeating paramKey)
			(batchable run optParser)

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 o) (serializeKey' k) $
		[ ("objectpath", fromRawFilePath objectpath)
		, ("objectpointer", fromRawFilePath objectpointer)
		] ++ formatVars k af
	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