2013-04-26 22:22:44 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-04-26 22:22:44 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.EnableRemote where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Command
|
|
|
|
import qualified Logs.Remote
|
|
|
|
import qualified Types.Remote as R
|
2015-09-14 18:49:48 +00:00
|
|
|
import qualified Annex.SpecialRemote
|
2015-10-26 18:55:40 +00:00
|
|
|
import qualified Remote
|
|
|
|
import Logs.UUID
|
2013-04-26 22:22:44 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2015-07-08 19:08:02 +00:00
|
|
|
cmd = command "enableremote" SectionSetup
|
|
|
|
"enables use of an existing special remote"
|
2013-04-26 22:22:44 +00:00
|
|
|
(paramPair paramName $ paramOptional $ paramRepeating paramKeyValue)
|
2015-07-08 19:08:02 +00:00
|
|
|
(withParams seek)
|
2013-04-26 22:22:44 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
fix inversion of control in CommandSeek (no behavior changes)
I've been disliking how the command seek actions were written for some
time, with their inversion of control and ugly workarounds.
The last straw to fix it was sync --content, which didn't fit the
Annex [CommandStart] interface well at all. I have not yet made it take
advantage of the changed interface though.
The crucial change, and probably why I didn't do it this way from the
beginning, is to make each CommandStart action be run with exceptions
caught, and if it fails, increment a failure counter in annex state.
So I finally remove the very first code I wrote for git-annex, which
was before I had exception handling in the Annex monad, and so ran outside
that monad, passing state explicitly as it ran each CommandStart action.
This was a real slog from 1 to 5 am.
Test suite passes.
Memory usage is lower than before, sometimes by a couple of megabytes, and
remains constant, even when running in a large repo, and even when
repeatedly failing and incrementing the error counter. So no accidental
laziness space leaks.
Wall clock speed is identical, even in large repos.
This commit was sponsored by an anonymous bitcoiner.
2014-01-20 08:11:42 +00:00
|
|
|
seek = withWords start
|
2013-04-26 22:22:44 +00:00
|
|
|
|
|
|
|
start :: [String] -> CommandStart
|
2015-10-26 18:55:40 +00:00
|
|
|
start [] = unknownNameError "Specify the special remote to enable."
|
2015-09-14 18:49:48 +00:00
|
|
|
start (name:ws) = go =<< Annex.SpecialRemote.findExisting name
|
2013-04-26 22:22:44 +00:00
|
|
|
where
|
|
|
|
config = Logs.Remote.keyValToConfig ws
|
|
|
|
|
2015-10-26 18:55:40 +00:00
|
|
|
go Nothing = do
|
|
|
|
m <- Annex.SpecialRemote.specialRemoteMap
|
|
|
|
confm <- Logs.Remote.readRemoteLog
|
|
|
|
v <- Remote.nameToUUID' name
|
|
|
|
case v of
|
|
|
|
Right u | u `M.member` m ->
|
|
|
|
go (Just (u, fromMaybe M.empty (M.lookup u confm)))
|
|
|
|
_ -> unknownNameError "Unknown special remote."
|
2013-04-26 22:22:44 +00:00
|
|
|
go (Just (u, c)) = do
|
|
|
|
let fullconfig = config `M.union` c
|
2015-09-14 18:49:48 +00:00
|
|
|
t <- either error return (Annex.SpecialRemote.findType fullconfig)
|
2013-04-26 22:22:44 +00:00
|
|
|
showStart "enableremote" name
|
|
|
|
next $ perform t u fullconfig
|
|
|
|
|
|
|
|
unknownNameError :: String -> Annex a
|
|
|
|
unknownNameError prefix = do
|
2015-10-26 18:55:40 +00:00
|
|
|
m <- Annex.SpecialRemote.specialRemoteMap
|
|
|
|
descm <- M.unionWith Remote.addName <$> uuidMap <*> pure m
|
|
|
|
msg <- if M.null m
|
|
|
|
then pure "(No special remotes are currently known; perhaps use initremote instead?)"
|
|
|
|
else Remote.prettyPrintUUIDsDescs
|
|
|
|
"known special remotes"
|
|
|
|
descm (M.keys m)
|
|
|
|
error $ prefix ++ "\n" ++ msg
|
2013-04-26 22:22:44 +00:00
|
|
|
|
|
|
|
perform :: RemoteType -> UUID -> R.RemoteConfig -> CommandPerform
|
|
|
|
perform t u c = do
|
2014-02-11 18:06:50 +00:00
|
|
|
(c', u') <- R.setup t (Just u) Nothing c
|
2013-09-07 22:38:00 +00:00
|
|
|
next $ cleanup u' c'
|
2013-04-26 22:22:44 +00:00
|
|
|
|
|
|
|
cleanup :: UUID -> R.RemoteConfig -> CommandCleanup
|
|
|
|
cleanup u c = do
|
|
|
|
Logs.Remote.configSet u c
|
|
|
|
return True
|