add sameas inherited configs to newConfig

This makes initremote --sameas work with encryption inherited.
This commit is contained in:
Joey Hess 2019-10-11 12:45:30 -04:00
parent 2bd6e81bb0
commit 91eed85fd4
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
5 changed files with 46 additions and 35 deletions

View file

@ -33,12 +33,21 @@ findExisting name = do
. findByName name . findByName name
<$> Logs.Remote.readRemoteLog <$> Logs.Remote.readRemoteLog
newConfig :: RemoteName -> Maybe (Sameas UUID) -> RemoteConfig newConfig
newConfig name Nothing = M.singleton nameField name :: RemoteName
newConfig name (Just (Sameas u)) = M.fromList -> Maybe (Sameas UUID)
[ (sameasNameField, name) -> RemoteConfig
, (sameasUUIDField, fromUUID u) -- ^ configuration provided by the user
] -> M.Map UUID RemoteConfig
-- ^ configuration of other special remotes, to inherit from
-- when sameas is used
-> RemoteConfig
newConfig name sameas fromuser m = case sameas of
Nothing -> M.insert nameField name fromuser
Just (Sameas u) -> addSameasInherited m $ M.fromList
[ (sameasNameField, name)
, (sameasUUIDField, fromUUID u)
] `M.union` fromuser
findByName :: RemoteName -> M.Map UUID RemoteConfig -> [(UUID, RemoteConfig)] findByName :: RemoteName -> M.Map UUID RemoteConfig -> [(UUID, RemoteConfig)]
findByName n = filter (matching . snd) . M.toList findByName n = filter (matching . snd) . M.toList

View file

@ -9,6 +9,7 @@ module Annex.SpecialRemote.Config where
import Common import Common
import Types.Remote (RemoteConfigField, RemoteConfig) import Types.Remote (RemoteConfigField, RemoteConfig)
import Types.UUID
import qualified Data.Map as M import qualified Data.Map as M
import qualified Data.Set as S import qualified Data.Set as S
@ -70,3 +71,24 @@ sameasInherits = S.fromList
-- (new-style chunking does not have that limitation) -- (new-style chunking does not have that limitation)
, chunksizeField , chunksizeField
] ]
{- Each RemoteConfig that has a sameas-uuid inherits some fields
- from it. Such fields can only be set by inheritance; the RemoteConfig
- cannot provide values from them. -}
addSameasInherited :: M.Map UUID RemoteConfig -> RemoteConfig -> RemoteConfig
addSameasInherited m c = case toUUID <$> M.lookup sameasUUIDField c of
Nothing -> c
Just sameasuuid -> case M.lookup sameasuuid m of
Nothing -> c
Just parentc ->
M.withoutKeys c sameasInherits
`M.union`
M.restrictKeys parentc sameasInherits
{- Remove any fields inherited from a sameas-uuid. When storing a
- RemoteConfig, those fields don't get stored, since they were already
- inherited. -}
removeSameasInherited :: RemoteConfig -> RemoteConfig
removeSameasInherited c = case M.lookup sameasUUIDField c of
Nothing -> c
Just _ -> M.restrictKeys c sameasInherits

View file

@ -52,7 +52,7 @@ makeRsyncRemote name location = makeRemote name location $ const $ void $
go =<< Annex.SpecialRemote.findExisting name go =<< Annex.SpecialRemote.findExisting name
where where
go Nothing = setupSpecialRemote name Rsync.remote config Nothing go Nothing = setupSpecialRemote name Rsync.remote config Nothing
(Nothing, R.Init, Annex.SpecialRemote.newConfig name Nothing) (Nothing, R.Init, Annex.SpecialRemote.newConfig name Nothing mempty mempty)
go (Just (u, c)) = setupSpecialRemote name Rsync.remote config Nothing go (Just (u, c)) = setupSpecialRemote name Rsync.remote config Nothing
(Just u, R.Enable c, c) (Just u, R.Enable c, c)
config = M.fromList config = M.fromList
@ -83,7 +83,7 @@ initSpecialRemote name remotetype mcreds config = go 0
let fullname = if n == 0 then name else name ++ show n let fullname = if n == 0 then name else name ++ show n
Annex.SpecialRemote.findExisting fullname >>= \case Annex.SpecialRemote.findExisting fullname >>= \case
Nothing -> setupSpecialRemote fullname remotetype config mcreds Nothing -> setupSpecialRemote fullname remotetype config mcreds
(Nothing, R.Init, Annex.SpecialRemote.newConfig fullname Nothing) (Nothing, R.Init, Annex.SpecialRemote.newConfig fullname Nothing mempty mempty)
Just _ -> go (n + 1) Just _ -> go (n + 1)
{- Enables an existing special remote. -} {- Enables an existing special remote. -}

View file

@ -15,6 +15,7 @@ import qualified Remote
import qualified Logs.Remote import qualified Logs.Remote
import qualified Types.Remote as R import qualified Types.Remote as R
import Logs.UUID import Logs.UUID
import Logs.Remote
import Types.GitConfig import Types.GitConfig
cmd :: Command cmd :: Command
@ -57,14 +58,14 @@ start o (name:ws) = ifM (isJust <$> findExisting name)
(pure Nothing) (pure Nothing)
(Just . Sameas <$$> getParsed) (Just . Sameas <$$> getParsed)
(sameas o) (sameas o)
let c = newConfig name sameasuuid c <- newConfig name sameasuuid
t <- either giveup return (findType config) (Logs.Remote.keyValToConfig ws)
<$> readRemoteLog
t <- either giveup return (findType c)
starting "initremote" (ActionItemOther (Just name)) $ starting "initremote" (ActionItemOther (Just name)) $
perform t name $ M.union config c perform t name c
) )
) )
where
config = Logs.Remote.keyValToConfig ws
perform :: RemoteType -> String -> R.RemoteConfig -> CommandPerform perform :: RemoteType -> String -> R.RemoteConfig -> CommandPerform
perform t name c = do perform t name c = do

View file

@ -40,32 +40,11 @@ configSet u cfg = do
{- Map of remotes by uuid containing key/value config maps. -} {- Map of remotes by uuid containing key/value config maps. -}
readRemoteLog :: Annex (M.Map UUID RemoteConfig) readRemoteLog :: Annex (M.Map UUID RemoteConfig)
readRemoteLog = addSameasInherited readRemoteLog = (\m -> M.map (addSameasInherited m) m)
. simpleMap . simpleMap
. parseLogOld remoteConfigParser . parseLogOld remoteConfigParser
<$> Annex.Branch.get remoteLog <$> Annex.Branch.get remoteLog
{- Each RemoteConfig that has a sameas-uuid inherits some fields
- from it. Such fields can only be set by inheritance; the RemoteConfig
- cannot provide values from them. -}
addSameasInherited :: M.Map UUID RemoteConfig -> M.Map UUID RemoteConfig
addSameasInherited m = M.map go m
where
go c = case toUUID <$> M.lookup sameasUUIDField c of
Nothing -> c
Just sameasuuid -> case M.lookup sameasuuid m of
Nothing -> c
Just parentc ->
M.withoutKeys c sameasInherits
`M.union`
M.restrictKeys parentc sameasInherits
{- Remove any fields inherited from a sameas-uuid. -}
removeSameasInherited :: RemoteConfig -> RemoteConfig
removeSameasInherited c = case M.lookup sameasUUIDField c of
Nothing -> c
Just _ -> M.restrictKeys c sameasInherits
remoteConfigParser :: A.Parser RemoteConfig remoteConfigParser :: A.Parser RemoteConfig
remoteConfigParser = keyValToConfig . words . decodeBS <$> A.takeByteString remoteConfigParser = keyValToConfig . words . decodeBS <$> A.takeByteString