summaryrefslogtreecommitdiff
path: root/Command/Remotes.hs
blob: f9ae9b3cd4ea439367e6aa96a1cbedb893816103 (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
{- git-annex command
 -
 - Copyright 2013 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Command.Remotes where

import qualified Data.Set as S

import Common.Annex
import Command
import Remote
import Logs.Trust
import Annex.UUID

def :: [Command]
def = [noCommit $ command "remotes" paramPaths seek
	SectionQuery "show which remotes contain files"]

seek :: [CommandSeek]
seek = 
	[ withValue getList $ \l -> withNothing $ startHeader l
	, withValue getList $ \l -> withFilesInGit $ whenAnnexed $ start l
	]

getList :: Annex [(UUID, RemoteName, TrustLevel)]
getList = do
	rs <- remoteList
	ts <- mapM (lookupTrust . uuid) rs
	hereu <- getUUID
	heretrust <- lookupTrust hereu
	return $ (hereu, "here", heretrust) : zip3 (map uuid rs) (map name rs) ts

startHeader :: [(UUID, RemoteName, TrustLevel)] -> CommandStart
startHeader l = do
	liftIO $ putStrLn $ header $ map (\(_, n, t) -> (n, t)) l
	stop

start :: [(UUID, RemoteName, TrustLevel)] -> FilePath -> (Key, Backend) -> CommandStart
start l file (key, _) = do
	ls <- S.fromList <$> keyLocations key
	liftIO $ putStrLn $ format (map (\(u, _, t) -> (t, S.member u ls)) l) file
	stop

type RemoteName = String
type Present = Bool

header :: [(RemoteName, TrustLevel)] -> String
header remotes = (unlines $ zipWith formatheader [0..] remotes) ++ (pipes (length remotes))
  where
    formatheader n (remotename, trustlevel) = (pipes n) ++ remotename ++ (trust trustlevel)
    pipes = flip replicate '|'
    trust UnTrusted = " (untrusted)"
    trust _ = ""

format :: [(TrustLevel, Present)] -> FilePath -> String
format remotes file = thereMap ++ " " ++ file
  where 
    thereMap = concatMap there remotes
    there (UnTrusted, True) = "x"
    there (_, True) = "X"
    there (_, False) = "_"