2012-10-09 18:43:53 +00:00
|
|
|
{- git-annex assistant webapp configurator for editing existing repos
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-04-25 20:42:17 +00:00
|
|
|
{-# LANGUAGE CPP, TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
|
2012-10-09 18:43:53 +00:00
|
|
|
|
|
|
|
module Assistant.WebApp.Configurators.Edit where
|
|
|
|
|
2012-11-25 04:26:46 +00:00
|
|
|
import Assistant.WebApp.Common
|
2012-10-12 05:09:28 +00:00
|
|
|
import Assistant.WebApp.Utility
|
2012-10-14 21:18:01 +00:00
|
|
|
import Assistant.DaemonStatus
|
|
|
|
import Assistant.MakeRemote (uniqueRemoteName)
|
2012-10-27 16:25:29 +00:00
|
|
|
import Assistant.WebApp.Configurators.XMPP (xmppNeeded)
|
2013-04-08 20:45:12 +00:00
|
|
|
import Assistant.ScanRemotes
|
2013-04-25 20:42:17 +00:00
|
|
|
import qualified Assistant.WebApp.Configurators.AWS as AWS
|
|
|
|
import qualified Assistant.WebApp.Configurators.IA as IA
|
|
|
|
#ifdef WITH_S3
|
|
|
|
import qualified Remote.S3 as S3
|
|
|
|
#endif
|
2012-10-09 19:11:48 +00:00
|
|
|
import qualified Remote
|
2013-01-01 17:52:47 +00:00
|
|
|
import qualified Types.Remote as Remote
|
2012-10-14 21:18:01 +00:00
|
|
|
import qualified Remote.List as Remote
|
2012-10-09 19:11:48 +00:00
|
|
|
import Logs.UUID
|
2012-10-10 20:04:28 +00:00
|
|
|
import Logs.Group
|
2012-10-10 23:13:49 +00:00
|
|
|
import Logs.PreferredContent
|
2013-04-25 20:42:17 +00:00
|
|
|
import Logs.Remote
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2012-10-11 23:36:28 +00:00
|
|
|
import qualified Git
|
2012-10-14 21:18:01 +00:00
|
|
|
import qualified Git.Command
|
2012-10-31 18:39:02 +00:00
|
|
|
import qualified Git.Config
|
2013-03-03 18:55:25 +00:00
|
|
|
import qualified Annex
|
2013-01-14 22:42:15 +00:00
|
|
|
import Git.Remote
|
2012-10-09 18:43:53 +00:00
|
|
|
|
|
|
|
import qualified Data.Text as T
|
2012-10-09 19:11:48 +00:00
|
|
|
import qualified Data.Map as M
|
2012-10-10 20:23:41 +00:00
|
|
|
import qualified Data.Set as S
|
|
|
|
|
|
|
|
data RepoGroup = RepoGroupCustom String | RepoGroupStandard StandardGroup
|
|
|
|
deriving (Show, Eq)
|
2012-10-09 19:11:48 +00:00
|
|
|
|
|
|
|
data RepoConfig = RepoConfig
|
2012-10-14 21:18:01 +00:00
|
|
|
{ repoName :: Text
|
|
|
|
, repoDescription :: Maybe Text
|
2012-10-10 20:23:41 +00:00
|
|
|
, repoGroup :: RepoGroup
|
2013-04-26 17:00:14 +00:00
|
|
|
, repoAssociatedDirectory :: Maybe Text
|
2012-10-11 23:22:29 +00:00
|
|
|
, repoSyncable :: Bool
|
2012-10-09 19:11:48 +00:00
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
2013-01-01 17:52:47 +00:00
|
|
|
getRepoConfig :: UUID -> Maybe Remote -> Annex RepoConfig
|
2013-04-26 17:00:14 +00:00
|
|
|
getRepoConfig uuid mremote = do
|
|
|
|
groups <- lookupGroups uuid
|
|
|
|
remoteconfig <- M.lookup uuid <$> readRemoteLog
|
|
|
|
let (repogroup, associateddirectory) = case getStandardGroup groups of
|
|
|
|
Nothing -> (RepoGroupCustom $ unwords $ S.toList groups, Nothing)
|
|
|
|
Just g -> (RepoGroupStandard g, associatedDirectory remoteconfig g)
|
|
|
|
|
|
|
|
description <- maybe Nothing (Just . T.pack) . M.lookup uuid <$> uuidMap
|
|
|
|
|
|
|
|
syncable <- case mremote of
|
2013-03-03 18:55:25 +00:00
|
|
|
Just r -> return $ remoteAnnexSync $ Remote.gitconfig r
|
|
|
|
Nothing -> annexAutoCommit <$> Annex.getGitConfig
|
2012-10-10 23:13:49 +00:00
|
|
|
|
2013-04-26 17:00:14 +00:00
|
|
|
return $ RepoConfig
|
|
|
|
(T.pack $ maybe "here" Remote.name mremote)
|
|
|
|
description
|
|
|
|
repogroup
|
|
|
|
(T.pack <$> associateddirectory)
|
|
|
|
syncable
|
|
|
|
|
2012-10-14 21:18:01 +00:00
|
|
|
setRepoConfig :: UUID -> Maybe Remote -> RepoConfig -> RepoConfig -> Handler ()
|
|
|
|
setRepoConfig uuid mremote oldc newc = do
|
2013-04-08 20:45:12 +00:00
|
|
|
when descriptionChanged $ liftAnnex $ do
|
2012-10-14 21:18:01 +00:00
|
|
|
maybe noop (describeUUID uuid . T.unpack) (repoDescription newc)
|
2012-10-30 21:22:21 +00:00
|
|
|
void uuidMapLoad
|
2013-04-08 20:45:12 +00:00
|
|
|
when nameChanged $ do
|
2013-03-04 20:36:38 +00:00
|
|
|
liftAnnex $ do
|
2013-04-08 20:45:12 +00:00
|
|
|
name <- fromRepo $ uniqueRemoteName (legalName newc) 0
|
2012-10-31 18:39:02 +00:00
|
|
|
{- git remote rename expects there to be a
|
|
|
|
- remote.<name>.fetch, and exits nonzero if
|
|
|
|
- there's not. Special remotes don't normally
|
|
|
|
- have that, and don't use it. Temporarily add
|
|
|
|
- it if it's missing. -}
|
|
|
|
let remotefetch = "remote." ++ T.unpack (repoName oldc) ++ ".fetch"
|
|
|
|
needfetch <- isNothing <$> fromRepo (Git.Config.getMaybe remotefetch)
|
|
|
|
when needfetch $
|
2013-03-03 17:39:07 +00:00
|
|
|
inRepo $ Git.Command.run
|
|
|
|
[Param "config", Param remotefetch, Param ""]
|
|
|
|
inRepo $ Git.Command.run
|
|
|
|
[ Param "remote"
|
|
|
|
, Param "rename"
|
2012-10-14 21:18:01 +00:00
|
|
|
, Param $ T.unpack $ repoName oldc
|
|
|
|
, Param name
|
|
|
|
]
|
|
|
|
void $ Remote.remoteListRefresh
|
2012-10-30 21:14:26 +00:00
|
|
|
liftAssistant updateSyncRemotes
|
2013-04-26 17:00:14 +00:00
|
|
|
when associatedDirectoryChanged $ case repoAssociatedDirectory newc of
|
|
|
|
Nothing -> noop
|
|
|
|
Just t
|
|
|
|
| T.null t -> noop
|
|
|
|
| otherwise -> liftAnnex $ do
|
|
|
|
let dir = takeBaseName $ T.unpack t
|
|
|
|
m <- readRemoteLog
|
|
|
|
case M.lookup uuid m of
|
|
|
|
Nothing -> noop
|
|
|
|
Just remoteconfig -> configSet uuid $
|
|
|
|
M.insert "preferreddir" dir remoteconfig
|
2013-04-08 20:45:12 +00:00
|
|
|
when groupChanged $ do
|
|
|
|
liftAnnex $ case repoGroup newc of
|
|
|
|
RepoGroupStandard g -> setStandardGroup uuid g
|
|
|
|
RepoGroupCustom s -> groupSet uuid $ S.fromList $ words s
|
|
|
|
{- Enabling syncing will cause a scan,
|
|
|
|
- so avoid queueing a duplicate scan. -}
|
|
|
|
when (repoSyncable newc && not syncableChanged) $ liftAssistant $
|
|
|
|
case mremote of
|
|
|
|
Just remote -> do
|
|
|
|
addScanRemotes True [remote]
|
|
|
|
Nothing -> do
|
|
|
|
addScanRemotes True
|
|
|
|
=<< syncDataRemotes <$> getDaemonStatus
|
|
|
|
when syncableChanged $
|
|
|
|
changeSyncable mremote (repoSyncable newc)
|
|
|
|
where
|
|
|
|
syncableChanged = repoSyncable oldc /= repoSyncable newc
|
2013-04-26 17:00:14 +00:00
|
|
|
associatedDirectoryChanged = repoAssociatedDirectory oldc /= repoAssociatedDirectory newc
|
2013-04-08 20:45:12 +00:00
|
|
|
groupChanged = repoGroup oldc /= repoGroup newc
|
|
|
|
nameChanged = isJust mremote && legalName oldc /= legalName newc
|
|
|
|
descriptionChanged = repoDescription oldc /= repoDescription newc
|
|
|
|
|
|
|
|
legalName = makeLegalName . T.unpack . repoName
|
2012-10-11 21:14:42 +00:00
|
|
|
|
2013-06-02 19:57:22 +00:00
|
|
|
editRepositoryAForm :: RepoConfig -> AForm Handler RepoConfig
|
2013-04-26 17:00:14 +00:00
|
|
|
editRepositoryAForm def = RepoConfig
|
2012-10-14 21:18:01 +00:00
|
|
|
<$> areq textField "Name" (Just $ repoName def)
|
|
|
|
<*> aopt textField "Description" (Just $ repoDescription def)
|
2012-12-03 02:33:30 +00:00
|
|
|
<*> areq (selectFieldList groups `withNote` help) "Repository group" (Just $ repoGroup def)
|
2013-04-26 17:00:14 +00:00
|
|
|
<*> associateddirectory
|
2012-10-11 23:22:29 +00:00
|
|
|
<*> areq checkBoxField "Syncing enabled" (Just $ repoSyncable def)
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
2012-12-03 02:33:30 +00:00
|
|
|
groups = customgroups ++ standardgroups
|
2012-10-31 06:34:03 +00:00
|
|
|
standardgroups :: [(Text, RepoGroup)]
|
2013-04-26 17:00:14 +00:00
|
|
|
standardgroups = map (\g -> (T.pack $ descStandardGroup g , RepoGroupStandard g))
|
2012-10-31 06:34:03 +00:00
|
|
|
[minBound :: StandardGroup .. maxBound :: StandardGroup]
|
|
|
|
customgroups :: [(Text, RepoGroup)]
|
|
|
|
customgroups = case repoGroup def of
|
|
|
|
RepoGroupCustom s -> [(T.pack s, RepoGroupCustom s)]
|
|
|
|
_ -> []
|
2012-12-03 02:33:30 +00:00
|
|
|
help = [whamlet|<a href="@{RepoGroupR}">What's this?</a>|]
|
2012-10-09 19:11:48 +00:00
|
|
|
|
2013-04-26 17:00:14 +00:00
|
|
|
associateddirectory = case repoAssociatedDirectory def of
|
2013-04-26 19:06:18 +00:00
|
|
|
Nothing -> aopt hiddenField "" Nothing
|
2013-04-26 17:00:14 +00:00
|
|
|
Just d -> aopt textField "Associated directory" (Just $ Just d)
|
|
|
|
|
2012-10-09 18:43:53 +00:00
|
|
|
getEditRepositoryR :: UUID -> Handler RepHtml
|
2013-03-16 22:48:23 +00:00
|
|
|
getEditRepositoryR = postEditRepositoryR
|
|
|
|
|
|
|
|
postEditRepositoryR :: UUID -> Handler RepHtml
|
|
|
|
postEditRepositoryR = editForm False
|
2012-10-11 21:35:08 +00:00
|
|
|
|
|
|
|
getEditNewRepositoryR :: UUID -> Handler RepHtml
|
2013-03-16 22:48:23 +00:00
|
|
|
getEditNewRepositoryR = postEditNewRepositoryR
|
|
|
|
|
|
|
|
postEditNewRepositoryR :: UUID -> Handler RepHtml
|
|
|
|
postEditNewRepositoryR = editForm True
|
2012-10-11 21:35:08 +00:00
|
|
|
|
2012-10-27 16:25:29 +00:00
|
|
|
getEditNewCloudRepositoryR :: UUID -> Handler RepHtml
|
2013-03-16 22:48:23 +00:00
|
|
|
getEditNewCloudRepositoryR = postEditNewCloudRepositoryR
|
|
|
|
|
|
|
|
postEditNewCloudRepositoryR :: UUID -> Handler RepHtml
|
|
|
|
postEditNewCloudRepositoryR uuid = xmppNeeded >> editForm True uuid
|
2012-10-27 16:25:29 +00:00
|
|
|
|
2012-10-11 21:35:08 +00:00
|
|
|
editForm :: Bool -> UUID -> Handler RepHtml
|
2012-12-30 03:10:18 +00:00
|
|
|
editForm new uuid = page "Configure repository" (Just Configuration) $ do
|
2013-03-16 04:12:28 +00:00
|
|
|
mremote <- liftAnnex $ Remote.remoteFromUUID uuid
|
|
|
|
curr <- liftAnnex $ getRepoConfig uuid mremote
|
2013-04-26 17:00:14 +00:00
|
|
|
liftAnnex $ checkAssociatedDirectory curr mremote
|
2013-06-02 19:57:22 +00:00
|
|
|
((result, form), enctype) <- handlerToWidget $
|
2013-04-26 17:00:14 +00:00
|
|
|
runFormPost $ renderBootstrap $ editRepositoryAForm curr
|
2012-10-09 19:11:48 +00:00
|
|
|
case result of
|
2013-06-02 19:57:22 +00:00
|
|
|
FormSuccess input -> handlerToWidget $ do
|
2012-10-14 21:18:01 +00:00
|
|
|
setRepoConfig uuid mremote curr input
|
2013-04-26 17:00:14 +00:00
|
|
|
liftAnnex $ checkAssociatedDirectory input mremote
|
2013-03-15 04:34:42 +00:00
|
|
|
redirect DashboardR
|
2013-04-25 20:42:17 +00:00
|
|
|
_ -> do
|
|
|
|
let istransfer = repoGroup curr == RepoGroupStandard TransferGroup
|
2013-04-26 17:00:14 +00:00
|
|
|
repoInfo <- getRepoInfo mremote . M.lookup uuid
|
|
|
|
<$> liftAnnex readRemoteLog
|
2013-04-25 20:42:17 +00:00
|
|
|
$(widgetFile "configurators/editrepository")
|
2013-04-26 17:00:14 +00:00
|
|
|
|
|
|
|
{- Makes any directory associated with the repository. -}
|
|
|
|
checkAssociatedDirectory :: RepoConfig -> Maybe Remote -> Annex ()
|
|
|
|
checkAssociatedDirectory _ Nothing = noop
|
|
|
|
checkAssociatedDirectory cfg (Just r) = do
|
|
|
|
repoconfig <- M.lookup (Remote.uuid r) <$> readRemoteLog
|
|
|
|
case repoGroup cfg of
|
|
|
|
RepoGroupStandard gr -> case associatedDirectory repoconfig gr of
|
|
|
|
Just d -> inRepo $ \g ->
|
2013-04-26 03:44:55 +00:00
|
|
|
createDirectoryIfMissing True $
|
|
|
|
Git.repoPath g </> d
|
|
|
|
Nothing -> noop
|
|
|
|
_ -> noop
|
2013-04-25 20:42:17 +00:00
|
|
|
|
|
|
|
getRepoInfo :: Maybe Remote.Remote -> Maybe Remote.RemoteConfig -> Widget
|
|
|
|
getRepoInfo (Just r) (Just c) = case M.lookup "type" c of
|
|
|
|
Just "S3"
|
|
|
|
#ifdef WITH_S3
|
|
|
|
| S3.isIA c -> IA.getRepoInfo c
|
|
|
|
#endif
|
|
|
|
| otherwise -> AWS.getRepoInfo c
|
|
|
|
Just t
|
|
|
|
| t /= "git" -> [whamlet|#{t} remote|]
|
|
|
|
_ -> getGitRepoInfo $ Remote.repo r
|
|
|
|
getRepoInfo (Just r) _ = getRepoInfo (Just r) (Just $ Remote.config r)
|
|
|
|
getRepoInfo _ _ = [whamlet|git repository|]
|
|
|
|
|
|
|
|
getGitRepoInfo :: Git.Repo -> Widget
|
|
|
|
getGitRepoInfo r = do
|
|
|
|
let loc = Git.repoLocation r
|
|
|
|
[whamlet|git repository located at <tt>#{loc}</tt>|]
|