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.
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE TypeFamilies, QuasiQuotes, MultiParamTypeClasses, TemplateHaskell, OverloadedStrings, RankNTypes #-}
|
|
|
|
|
|
|
|
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)
|
2012-10-09 19:11:48 +00:00
|
|
|
import qualified 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
|
2012-10-10 20:04:28 +00:00
|
|
|
import Types.StandardGroups
|
2012-10-11 23:22:29 +00:00
|
|
|
import qualified Config
|
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
|
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
|
2012-10-11 23:22:29 +00:00
|
|
|
, repoSyncable :: Bool
|
2012-10-09 19:11:48 +00:00
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
2012-10-14 21:18:01 +00:00
|
|
|
getRepoConfig :: UUID -> Git.Repo -> Maybe Remote -> Annex RepoConfig
|
|
|
|
getRepoConfig uuid r mremote = RepoConfig
|
|
|
|
<$> pure (T.pack $ maybe "here" Remote.name mremote)
|
|
|
|
<*> (maybe Nothing (Just . T.pack) . M.lookup uuid <$> uuidMap)
|
2012-10-10 23:13:49 +00:00
|
|
|
<*> getrepogroup
|
2012-10-11 23:36:28 +00:00
|
|
|
<*> Config.repoSyncable r
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
getrepogroup = do
|
|
|
|
groups <- lookupGroups uuid
|
|
|
|
return $
|
|
|
|
maybe (RepoGroupCustom $ unwords $ S.toList groups) RepoGroupStandard
|
|
|
|
(getStandardGroup groups)
|
2012-10-10 23:13:49 +00:00
|
|
|
|
2012-10-14 21:18:01 +00:00
|
|
|
setRepoConfig :: UUID -> Maybe Remote -> RepoConfig -> RepoConfig -> Handler ()
|
|
|
|
setRepoConfig uuid mremote oldc newc = do
|
2012-10-30 21:22:21 +00:00
|
|
|
when (repoDescription oldc /= repoDescription newc) $ runAnnex undefined $ 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
|
2012-10-14 21:18:01 +00:00
|
|
|
when (repoGroup oldc /= repoGroup newc) $ runAnnex undefined $
|
|
|
|
case repoGroup newc of
|
|
|
|
RepoGroupStandard g -> setStandardGroup uuid g
|
|
|
|
RepoGroupCustom s -> groupSet uuid $ S.fromList $ words s
|
|
|
|
when (repoSyncable oldc /= repoSyncable newc) $
|
|
|
|
changeSyncable mremote (repoSyncable newc)
|
|
|
|
when (isJust mremote && repoName oldc /= repoName newc) $ do
|
|
|
|
runAnnex undefined $ do
|
|
|
|
name <- fromRepo $ uniqueRemoteName (T.unpack $ repoName 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 $
|
|
|
|
inRepo $ Git.Command.run "config"
|
|
|
|
[Param remotefetch, Param ""]
|
2012-10-14 21:18:01 +00:00
|
|
|
inRepo $ Git.Command.run "remote"
|
|
|
|
[ Param "rename"
|
|
|
|
, Param $ T.unpack $ repoName oldc
|
|
|
|
, Param name
|
|
|
|
]
|
|
|
|
void $ Remote.remoteListRefresh
|
2012-10-30 21:14:26 +00:00
|
|
|
liftAssistant updateSyncRemotes
|
2012-10-11 21:14:42 +00:00
|
|
|
|
2012-10-09 19:11:48 +00:00
|
|
|
editRepositoryAForm :: RepoConfig -> AForm WebApp WebApp RepoConfig
|
|
|
|
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)
|
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)]
|
|
|
|
standardgroups = map (\g -> (T.pack $ descStandardGroup g , RepoGroupStandard g))
|
|
|
|
[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
|
|
|
|
2012-10-09 18:43:53 +00:00
|
|
|
getEditRepositoryR :: UUID -> Handler RepHtml
|
2012-10-11 21:35:08 +00:00
|
|
|
getEditRepositoryR = editForm False
|
|
|
|
|
|
|
|
getEditNewRepositoryR :: UUID -> Handler RepHtml
|
|
|
|
getEditNewRepositoryR = editForm True
|
|
|
|
|
2012-10-27 16:25:29 +00:00
|
|
|
getEditNewCloudRepositoryR :: UUID -> Handler RepHtml
|
|
|
|
getEditNewCloudRepositoryR uuid = xmppNeeded >> editForm True uuid
|
|
|
|
|
2012-10-11 21:35:08 +00:00
|
|
|
editForm :: Bool -> UUID -> Handler RepHtml
|
2012-11-25 04:26:46 +00:00
|
|
|
editForm new uuid = page "Configure repository" (Just Config) $ do
|
2012-10-12 16:45:16 +00:00
|
|
|
(repo, mremote) <- lift $ runAnnex undefined $ Remote.repoFromUUID uuid
|
2012-10-14 21:18:01 +00:00
|
|
|
curr <- lift $ runAnnex undefined $ getRepoConfig uuid repo mremote
|
2012-11-24 20:39:36 +00:00
|
|
|
lift $ checkarchivedirectory curr
|
2012-10-09 19:11:48 +00:00
|
|
|
((result, form), enctype) <- lift $
|
|
|
|
runFormGet $ renderBootstrap $ editRepositoryAForm curr
|
|
|
|
case result of
|
2012-10-10 23:13:49 +00:00
|
|
|
FormSuccess input -> lift $ do
|
2012-11-24 20:39:36 +00:00
|
|
|
checkarchivedirectory input
|
2012-10-14 21:18:01 +00:00
|
|
|
setRepoConfig uuid mremote curr input
|
2012-10-10 23:13:49 +00:00
|
|
|
redirect RepositoriesR
|
2012-10-11 21:35:08 +00:00
|
|
|
_ -> showform form enctype curr
|
2012-10-31 06:34:03 +00:00
|
|
|
where
|
|
|
|
showform form enctype curr = do
|
|
|
|
let istransfer = repoGroup curr == RepoGroupStandard TransferGroup
|
|
|
|
$(widgetFile "configurators/editrepository")
|
2012-11-24 20:39:36 +00:00
|
|
|
|
|
|
|
{- Makes a toplevel archive directory, so the user can get on with
|
|
|
|
- using it. This is done both when displaying the form, as well
|
|
|
|
- as after it's posted, because the user may not post the form,
|
|
|
|
- but may see that the repo is set up to use the archive
|
|
|
|
- directory. -}
|
|
|
|
checkarchivedirectory cfg
|
|
|
|
| repoGroup cfg == RepoGroupStandard SmallArchiveGroup = go
|
|
|
|
| repoGroup cfg == RepoGroupStandard FullArchiveGroup = go
|
|
|
|
| otherwise = noop
|
|
|
|
where
|
|
|
|
go = runAnnex undefined $ inRepo $ \g ->
|
|
|
|
createDirectoryIfMissing True $
|
|
|
|
Git.repoPath g </> "archive"
|