improve error message when no remote name is specified

list available remotes in case user wanted to use or change one of them
This commit is contained in:
Joey Hess 2011-05-28 21:58:48 -04:00
parent a19d81a42e
commit fdead6b9bb

View file

@ -10,6 +10,8 @@ module Command.InitRemote where
import qualified Data.Map as M import qualified Data.Map as M
import Control.Monad (when) import Control.Monad (when)
import Control.Monad.State (liftIO) import Control.Monad.State (liftIO)
import Data.Maybe
import Data.String.Utils
import Command import Command
import qualified Annex import qualified Annex
@ -32,7 +34,7 @@ seek = [withWords start]
start :: CommandStartWords start :: CommandStartWords
start ws = notBareRepo $ do start ws = notBareRepo $ do
when (null ws) $ error "Specify a name for the remote" when (null ws) $ needname
(u, c) <- findByName name (u, c) <- findByName name
let fullconfig = M.union config c let fullconfig = M.union config c
@ -44,6 +46,13 @@ start ws = notBareRepo $ do
where where
name = head ws name = head ws
config = Remote.keyValToConfig $ tail ws config = Remote.keyValToConfig $ tail ws
needname = do
let err s = error $ "Specify a name for the remote. " ++ s
names <- remoteNames
if null names
then err ""
else err $ "Either a new name, or one of these existing special remotes: " ++ join " " names
perform :: RemoteClass.RemoteType Annex -> UUID -> RemoteClass.RemoteConfig -> CommandPerform perform :: RemoteClass.RemoteType Annex -> UUID -> RemoteClass.RemoteConfig -> CommandPerform
perform t u c = do perform t u c = do
@ -83,6 +92,11 @@ findByName' n m = if null matches then Nothing else Just $ head matches
| n' == n -> True | n' == n -> True
| otherwise -> False | otherwise -> False
remoteNames :: Annex [String]
remoteNames = do
m <- Remote.readRemoteLog
return $ catMaybes $ map ((M.lookup nameKey) . snd) $ M.toList m
{- find the specified remote type -} {- find the specified remote type -}
findType :: RemoteClass.RemoteConfig -> Annex (RemoteClass.RemoteType Annex) findType :: RemoteClass.RemoteConfig -> Annex (RemoteClass.RemoteType Annex)
findType config = maybe unspecified specified $ M.lookup typeKey config findType config = maybe unspecified specified $ M.lookup typeKey config