2012-09-18 21:50:07 +00:00
|
|
|
{- git-annex assistant webapp switching to other repos
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
2012-09-24 18:48:47 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2012-09-18 21:50:07 +00:00
|
|
|
-}
|
|
|
|
|
2013-06-05 01:02:09 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes, TemplateHaskell, OverloadedStrings #-}
|
2012-09-18 21:50:07 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.OtherRepos where
|
|
|
|
|
|
|
|
import Assistant.Common
|
|
|
|
import Assistant.WebApp.Types
|
2013-01-03 20:34:57 +00:00
|
|
|
import Assistant.WebApp.Page
|
2012-09-18 21:50:07 +00:00
|
|
|
import qualified Git.Construct
|
|
|
|
import qualified Git.Config
|
2013-04-23 15:38:52 +00:00
|
|
|
import Config.Files
|
2012-09-18 21:50:07 +00:00
|
|
|
import qualified Utility.Url as Url
|
2013-01-03 20:34:57 +00:00
|
|
|
import Utility.Yesod
|
2013-11-23 04:54:08 +00:00
|
|
|
import Assistant.Upgrade
|
2012-09-18 21:50:07 +00:00
|
|
|
|
|
|
|
import Control.Concurrent
|
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getRepositorySwitcherR :: Handler Html
|
2013-01-03 20:34:57 +00:00
|
|
|
getRepositorySwitcherR = page "Switch repository" Nothing $ do
|
|
|
|
repolist <- liftIO listOtherRepos
|
|
|
|
$(widgetFile "control/repositoryswitcher")
|
|
|
|
|
|
|
|
listOtherRepos :: IO [(String, String)]
|
|
|
|
listOtherRepos = do
|
2013-03-03 21:07:27 +00:00
|
|
|
dirs <- readAutoStartFile
|
2013-01-03 20:34:57 +00:00
|
|
|
pwd <- getCurrentDirectory
|
2013-03-03 21:07:27 +00:00
|
|
|
gooddirs <- filterM doesDirectoryExist $
|
|
|
|
filter (\d -> not $ d `dirContains` pwd) dirs
|
2013-01-09 22:59:45 +00:00
|
|
|
names <- mapM relHome gooddirs
|
|
|
|
return $ sort $ zip names gooddirs
|
2013-01-03 20:34:57 +00:00
|
|
|
|
2013-06-27 05:15:28 +00:00
|
|
|
getSwitchToRepositoryR :: FilePath -> Handler Html
|
2012-09-18 21:50:07 +00:00
|
|
|
getSwitchToRepositoryR repo = do
|
2013-06-11 04:06:06 +00:00
|
|
|
liftIO $ addAutoStartFile repo -- make this the new default repo
|
2013-11-23 03:12:06 +00:00
|
|
|
switchToAssistant repo
|
|
|
|
|
|
|
|
{- Starts up the assistant in the repository, and waits for it to create
|
|
|
|
- a gitAnnexUrlFile. Waits for the assistant to be up and listening for
|
|
|
|
- connections by testing the url. Once it's running, redirect to it. -}
|
|
|
|
switchToAssistant :: FilePath -> Handler Html
|
|
|
|
switchToAssistant repo = do
|
|
|
|
liftIO $ startAssistant repo
|
2013-02-19 00:37:26 +00:00
|
|
|
redirect =<< liftIO geturl
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
geturl = do
|
|
|
|
r <- Git.Config.read =<< Git.Construct.fromPath repo
|
|
|
|
waiturl $ gitAnnexUrlFile r
|
|
|
|
waiturl urlfile = do
|
|
|
|
v <- tryIO $ readFile urlfile
|
|
|
|
case v of
|
|
|
|
Left _ -> delayed $ waiturl urlfile
|
|
|
|
Right url -> ifM (listening url)
|
|
|
|
( return url
|
|
|
|
, delayed $ waiturl urlfile
|
|
|
|
)
|
2013-09-28 18:35:21 +00:00
|
|
|
listening url = catchBoolIO $ fst <$> Url.exists url [] Nothing
|
2012-10-31 06:34:03 +00:00
|
|
|
delayed a = do
|
|
|
|
threadDelay 100000 -- 1/10th of a second
|
|
|
|
a
|