migrate-to-gitea/app/Main.hs

103 lines
2.8 KiB
Haskell
Raw Permalink Normal View History

2022-07-11 05:02:02 +00:00
module Main where
2022-07-18 04:32:53 +00:00
import Actions
2022-07-17 03:12:03 +00:00
import Data.Text (pack)
import Options.Applicative
2022-07-18 04:32:53 +00:00
import Request
2022-07-17 03:12:03 +00:00
import Types
2022-07-18 04:32:53 +00:00
data Plan = Plan
2022-07-17 03:12:03 +00:00
{ from :: Source,
2022-07-18 04:32:53 +00:00
to :: Destination,
repos :: [Text]
2022-07-17 03:12:03 +00:00
}
deriving (Eq, Ord, Show)
longShort :: HasName f => String -> Mod f a
longShort t@(c : _) = long t <> short c
longShort t = long t
2022-07-18 04:32:53 +00:00
parsePlan :: ParserInfo Plan
parsePlan =
info
(plan <**> helper)
( fullDesc
<> progDesc "migrate-to-gitea solves the problem wherein you have just spun up a new self-hosted https://gitea.io instance, and now you want to move your project history there (or perhaps to one of the more well known public instances)."
<> header "migrate-to-gitea - cli tool to migrate from git{hub,lab} to a gitea instance"
)
2022-07-17 03:12:03 +00:00
where
2022-07-18 04:32:53 +00:00
plan =
Plan
<$> (sourceSpec <|> source)
<*> (destinationSpec <|> destination)
<*> many repo
2022-07-17 03:12:03 +00:00
sourceSpec =
option
(maybeReader (sourceFromText . pack))
( longShort "from"
2022-07-18 04:32:53 +00:00
<> metavar "FORGE:USER"
<> help "Source to migrate from"
2022-07-17 03:12:03 +00:00
)
source = Source <$> forge <*> fromUser
forge =
option
(str >>= (pure . forgeFromText . pack))
( long "forge"
2022-07-18 04:32:53 +00:00
<> metavar "FORGE"
2022-07-17 03:12:03 +00:00
<> help "Forge to migrate from"
)
fromUser =
mkUser
<$> strOption
( long "from-user"
2022-07-18 04:32:53 +00:00
<> metavar "USER"
2022-07-17 03:12:03 +00:00
<> help "User account to migrate from"
)
destinationSpec =
option
(maybeReader (destinationFromText . pack))
( longShort "to"
2022-07-18 04:32:53 +00:00
<> metavar "GITEA_URL:USER"
<> help "Destination to migrate to"
2022-07-17 03:12:03 +00:00
)
destination = Destination <$> gitea <*> toUser
gitea =
option
(str >>= (pure . mkGitea . pack))
( longShort "gitea-url"
2022-07-18 04:32:53 +00:00
<> metavar "GITEA_URL"
2022-07-17 03:12:03 +00:00
<> help "URL of the Gitea instance to migrate to"
)
toUser =
mkUser
<$> strOption
( long "to-user"
2022-07-18 04:32:53 +00:00
<> metavar "USER"
<> help "Gitea user account to migrate to"
2022-07-17 03:12:03 +00:00
)
2022-07-18 04:32:53 +00:00
repo =
strOption
( longShort "repo"
<> metavar "REPO"
<> help "Repo to migrate. Supply this option multiple times to include multiple repos. Defaults to all repositories"
)
2022-07-17 03:12:03 +00:00
2022-07-11 05:02:02 +00:00
main :: IO ()
2022-07-17 03:12:03 +00:00
main = do
2022-07-18 04:32:53 +00:00
plan <- execParser parsePlan
runRequest (runPlan plan)
runPlan :: Plan -> Request ()
runPlan (Plan {from, to, repos}) = do
repos' <-
if null repos
then listRepos from
else (pure . Right) (sourceRepo from <$> repos)
either
( \case
NotFound -> putTextLn ("Failed to list repos for " <> showSource from)
BadAuth msg -> putText "Authentication issue: " >> putBSLn msg
ParseError msg -> putStrLn msg
)
(mapM_ (chainedTo print . checkRepoExists))
repos'