2011-03-29 03:22:31 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2024-05-24 18:29:36 +00:00
|
|
|
- Copyright 2011-2024 Joey Hess <id@joeyh.name>
|
2011-03-29 03:22:31 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-03-29 03:22:31 +00:00
|
|
|
-}
|
|
|
|
|
2019-12-05 15:40:10 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2011-03-29 03:22:31 +00:00
|
|
|
module Command.InitRemote where
|
|
|
|
|
|
|
|
import Command
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.SpecialRemote
|
2011-03-29 03:22:31 +00:00
|
|
|
import qualified Remote
|
2011-06-02 01:56:04 +00:00
|
|
|
import qualified Types.Remote as R
|
2020-01-20 20:05:51 +00:00
|
|
|
import Types.RemoteConfig
|
2019-10-11 18:18:44 +00:00
|
|
|
import Annex.UUID
|
2012-06-07 15:16:48 +00:00
|
|
|
import Logs.UUID
|
2019-10-11 16:45:30 +00:00
|
|
|
import Logs.Remote
|
2017-08-17 16:26:14 +00:00
|
|
|
import Types.GitConfig
|
2020-01-10 18:10:20 +00:00
|
|
|
import Types.ProposedAccepted
|
2019-10-11 18:18:44 +00:00
|
|
|
import Config
|
2020-02-19 17:58:26 +00:00
|
|
|
import Git.Config
|
2024-05-24 18:29:36 +00:00
|
|
|
import Git.Types
|
2023-05-10 18:01:46 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.Text as T
|
2011-03-29 03:22:31 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2023-05-10 18:01:46 +00:00
|
|
|
cmd = withAnnexOptions [jsonOptions] $
|
|
|
|
command "initremote" SectionSetup
|
|
|
|
"creates a special (non-git) remote"
|
|
|
|
(paramPair paramName $ paramOptional $ paramRepeating paramParamValue)
|
|
|
|
(seek <$$> optParser)
|
2011-03-29 03:22:31 +00:00
|
|
|
|
2019-10-01 16:36:25 +00:00
|
|
|
data InitRemoteOptions = InitRemoteOptions
|
|
|
|
{ cmdparams :: CmdParams
|
|
|
|
, sameas :: Maybe (DeferredParse UUID)
|
2024-05-24 18:29:36 +00:00
|
|
|
, withUrl :: Bool
|
2020-01-20 21:04:45 +00:00
|
|
|
, whatElse :: Bool
|
2021-04-23 18:47:46 +00:00
|
|
|
, privateRemote :: Bool
|
2019-10-01 16:36:25 +00:00
|
|
|
}
|
2011-03-29 03:22:31 +00:00
|
|
|
|
2019-10-01 16:36:25 +00:00
|
|
|
optParser :: CmdParamsDesc -> Parser InitRemoteOptions
|
|
|
|
optParser desc = InitRemoteOptions
|
|
|
|
<$> cmdParams desc
|
|
|
|
<*> optional parseSameasOption
|
2024-05-24 18:29:36 +00:00
|
|
|
<*> switch
|
|
|
|
( long "with-url"
|
|
|
|
<> short 'u'
|
|
|
|
<> help "configure remote with an annex:: url"
|
|
|
|
)
|
2020-01-20 20:05:51 +00:00
|
|
|
<*> switch
|
2020-01-20 21:04:45 +00:00
|
|
|
( long "whatelse"
|
|
|
|
<> short 'w'
|
2020-01-20 20:05:51 +00:00
|
|
|
<> help "describe other configuration parameters for a special remote"
|
|
|
|
)
|
2021-04-23 18:47:46 +00:00
|
|
|
<*> switch
|
|
|
|
( long "private"
|
|
|
|
<> help "keep special remote information out of git-annex branch"
|
|
|
|
)
|
2019-10-01 16:36:25 +00:00
|
|
|
|
|
|
|
parseSameasOption :: Parser (DeferredParse UUID)
|
|
|
|
parseSameasOption = parseUUIDOption <$> strOption
|
|
|
|
( long "sameas"
|
|
|
|
<> metavar (paramRemote `paramOr` paramDesc `paramOr` paramUUID)
|
|
|
|
<> help "new remote that accesses the same data"
|
|
|
|
<> completeRemotes
|
|
|
|
)
|
|
|
|
|
|
|
|
seek :: InitRemoteOptions -> CommandSeek
|
|
|
|
seek o = withWords (commandAction . (start o)) (cmdparams o)
|
|
|
|
|
|
|
|
start :: InitRemoteOptions -> [String] -> CommandStart
|
|
|
|
start _ [] = giveup "Specify a name for the remote."
|
2023-05-10 18:01:46 +00:00
|
|
|
start o (name:ws) = do
|
|
|
|
if whatElse o
|
|
|
|
then ifM jsonOutputEnabled
|
|
|
|
( starting "initremote" ai si $ prep $ \c t ->
|
|
|
|
describeOtherParamsFor c t
|
|
|
|
, startingCustomOutput (ActionItemOther Nothing) $ prep $ \c t ->
|
|
|
|
describeOtherParamsFor c t
|
|
|
|
)
|
|
|
|
else starting "initremote" ai si $ prep $ \c t ->
|
|
|
|
perform t name c o
|
2020-09-14 20:49:33 +00:00
|
|
|
where
|
2023-05-10 18:01:46 +00:00
|
|
|
prep a = do
|
|
|
|
whenM (not . null <$> findExisting name) $
|
|
|
|
giveup $ "There is already a special remote named \"" ++ name ++
|
|
|
|
"\". (Use enableremote to enable an existing special remote.)"
|
|
|
|
whenM (isJust <$> Remote.byNameOnly name) $
|
|
|
|
giveup $ "There is already a remote named \"" ++ name ++ "\""
|
|
|
|
sameasuuid <- maybe
|
|
|
|
(pure Nothing)
|
|
|
|
(Just . Sameas <$$> getParsed)
|
|
|
|
(sameas o)
|
|
|
|
c <- newConfig name sameasuuid
|
|
|
|
(Logs.Remote.keyValToConfig Proposed ws)
|
|
|
|
<$> remoteConfigMap
|
|
|
|
t <- either giveup return (findType c)
|
|
|
|
a c t
|
|
|
|
|
|
|
|
si = SeekInput (name:ws)
|
|
|
|
ai = ActionItemOther (Just (UnquotedString name))
|
2011-03-29 03:22:31 +00:00
|
|
|
|
2019-10-11 18:18:44 +00:00
|
|
|
perform :: RemoteType -> String -> R.RemoteConfig -> InitRemoteOptions -> CommandPerform
|
|
|
|
perform t name c o = do
|
2021-04-23 18:47:46 +00:00
|
|
|
when (privateRemote o) $
|
|
|
|
setConfig (remoteAnnexConfig c "private") (boolConfig True)
|
2017-08-17 16:26:14 +00:00
|
|
|
dummycfg <- liftIO dummyRemoteGitConfig
|
2020-01-15 15:22:36 +00:00
|
|
|
let c' = M.delete uuidField c
|
|
|
|
(c'', u) <- R.setup t R.Init (sameasu <|> uuidfromuser) Nothing c' dummycfg
|
2020-02-19 17:58:26 +00:00
|
|
|
next $ cleanup t u name c'' o
|
2017-02-07 19:10:41 +00:00
|
|
|
where
|
2020-01-15 15:22:36 +00:00
|
|
|
uuidfromuser = case fromProposedAccepted <$> M.lookup uuidField c of
|
2017-02-07 19:10:41 +00:00
|
|
|
Just s
|
|
|
|
| isUUID s -> Just (toUUID s)
|
|
|
|
| otherwise -> giveup "invalid uuid"
|
|
|
|
Nothing -> Nothing
|
2020-01-10 18:10:20 +00:00
|
|
|
sameasu = toUUID . fromProposedAccepted <$> M.lookup sameasUUIDField c
|
2011-03-29 18:55:59 +00:00
|
|
|
|
2020-01-15 15:22:36 +00:00
|
|
|
uuidField :: R.RemoteConfigField
|
|
|
|
uuidField = Accepted "uuid"
|
|
|
|
|
2020-02-19 17:58:26 +00:00
|
|
|
cleanup :: RemoteType -> UUID -> String -> R.RemoteConfig -> InitRemoteOptions -> CommandCleanup
|
|
|
|
cleanup t u name c o = do
|
2019-10-11 18:18:44 +00:00
|
|
|
case sameas o of
|
|
|
|
Nothing -> do
|
|
|
|
describeUUID u (toUUIDDesc name)
|
|
|
|
Logs.Remote.configSet u c
|
|
|
|
Just _ -> do
|
|
|
|
cu <- liftIO genUUID
|
2020-02-19 17:45:11 +00:00
|
|
|
setConfig (remoteAnnexConfig c "config-uuid") (fromUUID cu)
|
2019-10-11 18:18:44 +00:00
|
|
|
Logs.Remote.configSet cu c
|
2024-05-24 18:29:36 +00:00
|
|
|
when (withUrl o) $
|
|
|
|
setAnnexUrl c
|
|
|
|
unless (Remote.gitSyncableRemoteType t || withUrl o) $
|
2020-02-19 17:58:26 +00:00
|
|
|
setConfig (remoteConfig c "skipFetchAll") (boolConfig True)
|
2012-12-13 04:45:27 +00:00
|
|
|
return True
|
2020-01-20 20:05:51 +00:00
|
|
|
|
2024-05-24 18:29:36 +00:00
|
|
|
setAnnexUrl :: R.RemoteConfig -> Annex ()
|
|
|
|
setAnnexUrl c =
|
|
|
|
getConfigMaybe (remoteConfig c "url") >>= \case
|
|
|
|
Just (ConfigValue _) -> noop
|
|
|
|
_ -> do
|
|
|
|
setConfig (remoteConfig c "url") "annex::"
|
|
|
|
setConfig (remoteConfig c "fetch") $
|
|
|
|
"+refs/heads/*:refs/remotes/" ++
|
|
|
|
getRemoteName c ++ "/*"
|
|
|
|
|
2020-01-20 20:05:51 +00:00
|
|
|
describeOtherParamsFor :: RemoteConfig -> RemoteType -> CommandPerform
|
|
|
|
describeOtherParamsFor c t = do
|
|
|
|
cp <- R.configParser t c
|
2020-01-20 20:23:35 +00:00
|
|
|
let l = map mk (filter notinconfig $ remoteConfigFieldParsers cp)
|
|
|
|
++ map mk' (maybe [] snd (remoteConfigRestPassthrough cp))
|
2023-05-10 18:01:46 +00:00
|
|
|
ifM jsonOutputEnabled
|
|
|
|
( maybeAddJSONField "whatelse" $ M.fromList $ mkjson l
|
|
|
|
, liftIO $ forM_ l $ \(p, fd, vd) -> case fd of
|
|
|
|
HiddenField -> return ()
|
|
|
|
FieldDesc d -> do
|
|
|
|
putStrLn p
|
|
|
|
putStrLn ("\t" ++ d)
|
|
|
|
case vd of
|
|
|
|
Nothing -> return ()
|
|
|
|
Just (ValueDesc d') ->
|
|
|
|
putStrLn $ "\t(" ++ d' ++ ")"
|
|
|
|
|
|
|
|
)
|
2020-01-20 20:05:51 +00:00
|
|
|
next $ return True
|
|
|
|
where
|
2023-05-10 18:01:46 +00:00
|
|
|
mkjson = mapMaybe $ \(p, fd, vd) ->
|
|
|
|
case fd of
|
|
|
|
HiddenField -> Nothing
|
|
|
|
FieldDesc d -> Just
|
|
|
|
( T.pack p
|
|
|
|
, M.fromList
|
|
|
|
[ ("description" :: T.Text, d)
|
|
|
|
, ("valuedescription", case vd of
|
|
|
|
Nothing -> ""
|
|
|
|
Just (ValueDesc d') -> d')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-01-20 20:05:51 +00:00
|
|
|
notinconfig fp = not (M.member (parserForField fp) c)
|
2023-05-10 18:01:46 +00:00
|
|
|
|
2020-01-20 20:23:35 +00:00
|
|
|
mk fp = ( fromProposedAccepted (parserForField fp)
|
|
|
|
, fieldDesc fp
|
|
|
|
, valueDesc fp
|
|
|
|
)
|
|
|
|
mk' (k, v) = (k, v, Nothing)
|