git-annex/Command/Init.hs
Joey Hess 484a74f073
auto-init autoenable=yes
Try to enable special remotes configured with autoenable=yes when git-annex
auto-initialization happens in a new clone of an existing repo. Previously,
git-annex init had to be explicitly run to enable them. That was a bit of a
wart of a special case for users to need to keep in mind.

Special remotes cannot display anything when autoenabled this way, to avoid
interfering with the output of git-annex query commands.

Any error messages will be hidden, and if it fails, nothing is displayed.
The user will realize the remote isn't enable when they try to use it,
and can run git-annex init manually then to try the autoenable again and
see what failed.

That seems like a reasonable approach, and it's less complicated than
communicating something across a pipe in order to display it as a side
message. Other reason not to do that is that, if the first command the
user runs is one like git-annex find that has machine readable output,
any message about autoenable failing would need to not be displayed anyway.
So better to not display a failure message ever, for consistency.

(Had to split out Remote.List.Util to avoid an import cycle.)
2020-05-27 12:40:35 -04:00

78 lines
2.2 KiB
Haskell

{- git-annex command
-
- Copyright 2010-2020 Joey Hess <id@joeyh.name>
-
- Licensed under the GNU AGPL version 3 or higher.
-}
module Command.Init where
import Command
import Annex.Init
import Annex.Version
import Types.RepoVersion
import qualified Annex.SpecialRemote
import Control.Monad.Fail as Fail (MonadFail(..))
import qualified Data.Map as M
cmd :: Command
cmd = dontCheck repoExists $
command "init" SectionSetup "initialize git-annex"
paramDesc (seek <$$> optParser)
data InitOptions = InitOptions
{ initDesc :: String
, initVersion :: Maybe RepoVersion
, autoEnableOnly :: Bool
}
optParser :: CmdParamsDesc -> Parser InitOptions
optParser desc = InitOptions
<$> (unwords <$> cmdParams desc)
<*> optional (option (str >>= parseRepoVersion)
( long "version" <> metavar paramValue
<> help "Override default annex.version"
))
<*> switch
( long "autoenable"
<> help "only enable special remotes configured with autoenable=true"
)
parseRepoVersion :: MonadFail m => String -> m RepoVersion
parseRepoVersion s = case RepoVersion <$> readish s of
Nothing -> Fail.fail $ "version parse error"
Just v
| v `elem` supportedVersions -> return v
| otherwise -> case M.lookup v autoUpgradeableVersions of
Just v' -> return v'
Nothing -> Fail.fail $ s ++ " is not a currently supported repository version"
seek :: InitOptions -> CommandSeek
seek = commandAction . start
start :: InitOptions -> CommandStart
start os
| autoEnableOnly os = starting "init" (ActionItemOther (Just "autoenable")) $
performAutoEnableOnly
| otherwise = starting "init" (ActionItemOther (Just $ initDesc os)) $
perform os
perform :: InitOptions -> CommandPerform
perform os = do
case initVersion os of
Nothing -> noop
Just wantversion -> getVersion >>= \case
Just v | v /= wantversion ->
giveup $ "This repository is already a initialized with version " ++ show (fromRepoVersion v) ++ ", not changing to requested version."
_ -> noop
initialize
(if null (initDesc os) then Nothing else Just (initDesc os))
(initVersion os)
Annex.SpecialRemote.autoEnable
next $ return True
performAutoEnableOnly :: CommandPerform
performAutoEnableOnly = do
Annex.SpecialRemote.autoEnable
next $ return True