2015-09-14 18:49:48 +00:00
|
|
|
{- git-annex special remote configuration
|
|
|
|
-
|
|
|
|
- Copyright 2011-2015 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Annex.SpecialRemote where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2015-09-14 19:34:15 +00:00
|
|
|
import Remote (remoteTypes, remoteMap)
|
2017-02-07 18:35:58 +00:00
|
|
|
import Types.Remote (RemoteConfig, RemoteConfigKey, SetupStage(..), typename, setup)
|
2017-08-17 16:26:14 +00:00
|
|
|
import Types.GitConfig
|
2015-09-14 18:49:48 +00:00
|
|
|
import Logs.Remote
|
|
|
|
import Logs.Trust
|
|
|
|
import qualified Git.Config
|
2016-11-30 18:35:24 +00:00
|
|
|
import Git.Types (RemoteName)
|
2015-09-14 18:49:48 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.Ord
|
|
|
|
|
|
|
|
{- See if there's an existing special remote with this name.
|
|
|
|
-
|
|
|
|
- Prefer remotes that are not dead when a name appears multiple times. -}
|
|
|
|
findExisting :: RemoteName -> Annex (Maybe (UUID, RemoteConfig))
|
|
|
|
findExisting name = do
|
|
|
|
t <- trustMap
|
2018-04-13 18:50:14 +00:00
|
|
|
headMaybe
|
2018-04-13 19:16:07 +00:00
|
|
|
. sortBy (comparing $ \(u, _c) -> Down $ M.lookup u t)
|
2015-09-14 18:49:48 +00:00
|
|
|
. findByName name
|
|
|
|
<$> Logs.Remote.readRemoteLog
|
|
|
|
|
|
|
|
newConfig :: RemoteName -> RemoteConfig
|
|
|
|
newConfig = M.singleton nameKey
|
|
|
|
|
|
|
|
findByName :: RemoteName -> M.Map UUID RemoteConfig -> [(UUID, RemoteConfig)]
|
|
|
|
findByName n = filter (matching . snd) . M.toList
|
|
|
|
where
|
|
|
|
matching c = case M.lookup nameKey c of
|
|
|
|
Nothing -> False
|
|
|
|
Just n'
|
|
|
|
| n' == n -> True
|
|
|
|
| otherwise -> False
|
|
|
|
|
2015-10-26 18:55:40 +00:00
|
|
|
specialRemoteMap :: Annex (M.Map UUID RemoteName)
|
|
|
|
specialRemoteMap = do
|
2015-09-14 18:49:48 +00:00
|
|
|
m <- Logs.Remote.readRemoteLog
|
2015-10-26 18:55:40 +00:00
|
|
|
return $ M.fromList $ mapMaybe go (M.toList m)
|
|
|
|
where
|
|
|
|
go (u, c) = case M.lookup nameKey c of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just n -> Just (u, n)
|
2015-09-14 18:49:48 +00:00
|
|
|
|
|
|
|
{- find the specified remote type -}
|
|
|
|
findType :: RemoteConfig -> Either String RemoteType
|
|
|
|
findType config = maybe unspecified specified $ M.lookup typeKey config
|
|
|
|
where
|
|
|
|
unspecified = Left "Specify the type of remote with type="
|
|
|
|
specified s = case filter (findtype s) remoteTypes of
|
|
|
|
[] -> Left $ "Unknown remote type " ++ s
|
|
|
|
(t:_) -> Right t
|
|
|
|
findtype s i = typename i == s
|
|
|
|
|
|
|
|
{- The name of a configured remote is stored in its config using this key. -}
|
|
|
|
nameKey :: RemoteConfigKey
|
|
|
|
nameKey = "name"
|
|
|
|
|
|
|
|
{- The type of a remote is stored in its config using this key. -}
|
|
|
|
typeKey :: RemoteConfigKey
|
|
|
|
typeKey = "type"
|
|
|
|
|
|
|
|
autoEnableKey :: RemoteConfigKey
|
|
|
|
autoEnableKey = "autoenable"
|
|
|
|
|
|
|
|
autoEnable :: Annex ()
|
|
|
|
autoEnable = do
|
2015-09-14 19:34:15 +00:00
|
|
|
remotemap <- M.filter configured <$> readRemoteLog
|
|
|
|
enabled <- remoteMap id
|
|
|
|
forM_ (M.toList remotemap) $ \(u, c) -> unless (u `M.member` enabled) $ do
|
2015-09-14 18:49:48 +00:00
|
|
|
case (M.lookup nameKey c, findType c) of
|
2015-09-14 19:34:15 +00:00
|
|
|
(Just name, Right t) -> whenM (canenable u) $ do
|
2015-09-14 18:49:48 +00:00
|
|
|
showSideAction $ "Auto enabling special remote " ++ name
|
2017-08-17 16:26:14 +00:00
|
|
|
dummycfg <- liftIO dummyRemoteGitConfig
|
2017-12-05 19:00:50 +00:00
|
|
|
tryNonAsync (setup t (Enable c) (Just u) Nothing c dummycfg) >>= \case
|
2015-09-14 18:49:48 +00:00
|
|
|
Left e -> warning (show e)
|
|
|
|
Right _ -> return ()
|
|
|
|
_ -> return ()
|
|
|
|
where
|
2015-09-14 19:34:15 +00:00
|
|
|
configured rc = fromMaybe False $
|
2015-09-14 18:49:48 +00:00
|
|
|
Git.Config.isTrue =<< M.lookup autoEnableKey rc
|
2015-09-14 19:34:15 +00:00
|
|
|
canenable u = (/= DeadTrusted) <$> lookupTrust u
|