initremote: If two existing remotes have the same name, prefer the one with a higher trust level.

This commit is contained in:
Joey Hess 2013-04-24 21:53:58 -04:00
parent 4f4b2b01fa
commit 49547ad32d
3 changed files with 13 additions and 4 deletions

View file

@ -16,6 +16,9 @@ import qualified Logs.Remote
import qualified Types.Remote as R
import Annex.UUID
import Logs.UUID
import Logs.Trust
import Data.Ord
def :: [Command]
def = [command "initremote"
@ -57,15 +60,18 @@ cleanup u name c = do
{- Look up existing remote's UUID and config by name, or generate a new one -}
findByName :: String -> Annex (UUID, R.RemoteConfig)
findByName name = do
m <- Logs.Remote.readRemoteLog
maybe generate return $ findByName' name m
t <- trustMap
matches <- sortBy (comparing $ \(u, _c) -> M.lookup u t )
. findByName' name
<$> Logs.Remote.readRemoteLog
maybe generate return $ headMaybe matches
where
generate = do
uuid <- liftIO genUUID
return (uuid, M.insert nameKey name M.empty)
findByName' :: String -> M.Map UUID R.RemoteConfig -> Maybe (UUID, R.RemoteConfig)
findByName' n = headMaybe . filter (matching . snd) . M.toList
findByName' :: String -> M.Map UUID R.RemoteConfig -> [(UUID, R.RemoteConfig)]
findByName' n = filter (matching . snd) . M.toList
where
matching c = case M.lookup nameKey c of
Nothing -> False