2019-04-15 17:05:44 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2019 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.RenameRemote where
|
|
|
|
|
|
|
|
import Command
|
2019-04-16 16:23:46 +00:00
|
|
|
import qualified Annex.SpecialRemote
|
2019-10-10 19:31:10 +00:00
|
|
|
import Annex.SpecialRemote.Config (nameField, sameasNameField)
|
2019-04-15 17:05:44 +00:00
|
|
|
import qualified Logs.Remote
|
|
|
|
import qualified Types.Remote as R
|
|
|
|
import qualified Remote
|
2020-01-10 18:10:20 +00:00
|
|
|
import Types.ProposedAccepted
|
2019-04-15 17:05:44 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
cmd :: Command
|
|
|
|
cmd = command "renameremote" SectionSetup
|
|
|
|
"changes name of special remote"
|
|
|
|
(paramPair paramName paramName)
|
|
|
|
(withParams seek)
|
|
|
|
|
|
|
|
seek :: CmdParams -> CommandSeek
|
|
|
|
seek = withWords (commandAction . start)
|
|
|
|
|
|
|
|
start :: [String] -> CommandStart
|
2020-09-14 20:49:33 +00:00
|
|
|
start ps@(oldname:newname:[]) = Annex.SpecialRemote.findExisting oldname >>= \case
|
2019-10-11 18:59:41 +00:00
|
|
|
Just (u, cfg, mcu) -> Annex.SpecialRemote.findExisting newname >>= \case
|
2019-04-16 16:23:46 +00:00
|
|
|
Just _ -> giveup $ "The name " ++ newname ++ " is already used by a special remote."
|
2019-10-11 19:32:56 +00:00
|
|
|
Nothing -> go u cfg mcu
|
2019-04-16 16:23:46 +00:00
|
|
|
-- Support lookup by uuid or description as well as remote name,
|
|
|
|
-- as a fallback when there is nothing with the name in the
|
|
|
|
-- special remote log.
|
|
|
|
Nothing -> Remote.nameToUUID' oldname >>= \case
|
2019-04-15 17:05:44 +00:00
|
|
|
Left e -> giveup e
|
2019-04-16 16:23:46 +00:00
|
|
|
Right u -> do
|
2020-09-22 17:52:26 +00:00
|
|
|
m <- Logs.Remote.remoteConfigMap
|
2019-04-16 16:23:46 +00:00
|
|
|
case M.lookup u m of
|
|
|
|
Nothing -> giveup "That is not a special remote."
|
2019-10-11 19:32:56 +00:00
|
|
|
Just cfg -> go u cfg Nothing
|
2019-04-16 16:23:46 +00:00
|
|
|
where
|
2020-09-14 20:49:33 +00:00
|
|
|
ai = ActionItemOther Nothing
|
|
|
|
si = SeekInput ps
|
|
|
|
go u cfg mcu = starting "rename" ai si $ perform u cfg mcu newname
|
2019-04-15 17:05:44 +00:00
|
|
|
start _ = giveup "Specify an old name (or uuid or description) and a new name."
|
|
|
|
|
2019-10-11 19:32:56 +00:00
|
|
|
perform :: UUID -> R.RemoteConfig -> Maybe (Annex.SpecialRemote.ConfigFrom UUID) -> String -> CommandPerform
|
|
|
|
perform u cfg mcu newname = do
|
|
|
|
let (namefield, cu) = case mcu of
|
|
|
|
Nothing -> (nameField, u)
|
|
|
|
Just (Annex.SpecialRemote.ConfigFrom u') -> (sameasNameField, u')
|
2020-01-10 18:10:20 +00:00
|
|
|
Logs.Remote.configSet cu (M.insert namefield (Proposed newname) cfg)
|
2019-10-10 17:08:17 +00:00
|
|
|
|
2019-04-15 17:05:44 +00:00
|
|
|
next $ return True
|