2020-01-30 18:40:08 +00:00
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Control.Monad
|
2020-01-31 14:54:41 +00:00
|
|
|
import Control.Monad.Except
|
2020-01-30 18:40:08 +00:00
|
|
|
import Data.ByteString ( ByteString )
|
2020-01-31 14:54:41 +00:00
|
|
|
import qualified Data.ByteString.UTF8 as UTF8
|
|
|
|
import Data.Dates ( getCurrentDateTime
|
|
|
|
, parseDate
|
|
|
|
, DateTime(..)
|
|
|
|
)
|
2020-01-30 18:40:08 +00:00
|
|
|
import Data.Functor ( (<&>) )
|
2020-01-31 14:54:41 +00:00
|
|
|
import Data.List
|
2020-01-30 18:40:08 +00:00
|
|
|
import Data.Semigroup ( (<>) )
|
2020-01-31 14:54:41 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Time.Clock
|
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
import Data.Time.Format
|
2020-01-30 18:40:08 +00:00
|
|
|
import GHup
|
|
|
|
import GitHub.Auth
|
2020-01-31 14:54:41 +00:00
|
|
|
import GitHub.Data.Definitions
|
|
|
|
import GitHub.Data.Name
|
|
|
|
import GitHub.Data.Repos
|
2020-01-30 18:40:08 +00:00
|
|
|
import HPath
|
|
|
|
import Options.Applicative
|
2020-01-31 14:54:41 +00:00
|
|
|
import Safe
|
2020-01-30 22:12:46 +00:00
|
|
|
import System.Console.Pretty
|
2020-01-30 18:40:08 +00:00
|
|
|
import System.Exit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data Options = Options
|
|
|
|
{ optCommand :: Command
|
|
|
|
}
|
|
|
|
|
|
|
|
data Command
|
|
|
|
= Fork ForkOptions
|
|
|
|
| Config ConfigOptions
|
|
|
|
| Del DelOptions
|
2020-01-31 14:54:41 +00:00
|
|
|
| ListForks ListForkOptions
|
2020-01-30 18:40:08 +00:00
|
|
|
|
|
|
|
data ForkOptions = ForkOptions
|
|
|
|
{
|
|
|
|
repo :: ByteString
|
|
|
|
, newBranch :: Maybe ByteString
|
|
|
|
, repoBasePath :: Maybe ByteString
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:54:41 +00:00
|
|
|
data ListForkOptions = ListForkOptions
|
|
|
|
{
|
|
|
|
lSince :: Maybe ByteString
|
|
|
|
}
|
|
|
|
|
2020-01-30 18:40:08 +00:00
|
|
|
data ConfigOptions = ConfigOptions {
|
2020-01-30 22:12:46 +00:00
|
|
|
oAuth :: ByteString
|
|
|
|
, bPath :: Maybe ByteString
|
2020-01-30 18:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
data DelOptions = DelOptions {
|
|
|
|
del :: ByteString
|
|
|
|
}
|
|
|
|
|
|
|
|
opts :: Parser Command
|
|
|
|
opts = subparser
|
2020-01-31 14:54:41 +00:00
|
|
|
( command "fork" (Fork <$> (info (forkOpts <**> helper) idm))
|
|
|
|
<> command "config" (Config <$> (info (configOpts <**> helper) idm))
|
|
|
|
<> command "delete" (Del <$> (info (delOpts <**> helper) idm))
|
|
|
|
<> command "list-forks" (ListForks <$> (info (lForkOpts <**> helper) idm))
|
2020-01-30 18:40:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
configOpts :: Parser ConfigOptions
|
2020-01-30 22:12:46 +00:00
|
|
|
configOpts =
|
|
|
|
ConfigOptions
|
|
|
|
<$> strOption
|
|
|
|
(short 'o' <> long "oauth" <> metavar "OAUTH" <> help
|
|
|
|
"The OAUTH token"
|
|
|
|
)
|
|
|
|
<*> optional
|
|
|
|
(strOption
|
|
|
|
((short 'p') <> long "base-path" <> metavar "BASE_PATH" <> help
|
|
|
|
"The base path to clone into"
|
|
|
|
)
|
|
|
|
)
|
2020-01-30 18:40:08 +00:00
|
|
|
|
|
|
|
forkOpts :: Parser ForkOptions
|
|
|
|
forkOpts =
|
|
|
|
ForkOptions
|
|
|
|
<$> strOption
|
|
|
|
(short 'r' <> long "repo" <> metavar "REPO" <> help
|
|
|
|
"The repository to fork"
|
|
|
|
)
|
|
|
|
<*> optional
|
|
|
|
(strOption
|
|
|
|
(short 'b' <> long "branch" <> metavar "BRANCH" <> help
|
|
|
|
"The branch to create after cloning the fork"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
<*> optional
|
|
|
|
(strOption
|
|
|
|
(short 'p' <> long "repo-path" <> metavar "REPO_PATH" <> help
|
|
|
|
"The base path where to clone the repository to"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
delOpts :: Parser DelOptions
|
|
|
|
delOpts = DelOptions <$> strOption
|
|
|
|
(short 'r' <> long "repo" <> metavar "REPO" <> help "The REPO fork to delete")
|
|
|
|
|
2020-01-31 14:54:41 +00:00
|
|
|
lForkOpts :: Parser ListForkOptions
|
|
|
|
lForkOpts = ListForkOptions <$> optional
|
|
|
|
(strOption
|
|
|
|
(short 's' <> long "since" <> metavar "SINCE" <> help
|
|
|
|
"The repository to fork"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-01-30 18:40:08 +00:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
e <- execParser (info (opts <**> helper) idm) >>= \case
|
|
|
|
Fork (ForkOptions {..}) -> do
|
|
|
|
case repoBasePath of
|
|
|
|
Just rbp -> case parseAbs rbp of
|
|
|
|
Just p -> prepareRepoForPR' repo (Just p) newBranch
|
|
|
|
Nothing -> fail "Repo path must be absolute"
|
|
|
|
Nothing -> prepareRepoForPR' repo Nothing newBranch
|
2020-01-30 22:12:46 +00:00
|
|
|
Config (ConfigOptions {..}) -> do
|
|
|
|
p <- maybe (pure Nothing) (fmap Just . parseAbs) bPath
|
|
|
|
writeSettings (Settings (OAuth oAuth) p) <&> Right
|
2020-01-31 14:54:41 +00:00
|
|
|
Del (DelOptions {..} ) -> deleteFork' del
|
|
|
|
ListForks (ListForkOptions {..}) -> runExceptT $ do
|
|
|
|
mtime <- lift $ case lSince of
|
|
|
|
Just t -> do
|
|
|
|
dt <- getCurrentDateTime
|
|
|
|
let mt =
|
|
|
|
either (const Nothing) Just . parseDate dt . UTF8.toString $ t
|
|
|
|
pure $ mt >>= \t ->
|
|
|
|
(parseTimeM
|
|
|
|
True
|
|
|
|
defaultTimeLocale
|
|
|
|
"%Y-%-m-%-d"
|
|
|
|
(show (year t) <> "-" <> show (month t) <> "-" <> show (day t)) :: Maybe
|
|
|
|
UTCTime
|
|
|
|
)
|
|
|
|
Nothing -> pure Nothing
|
|
|
|
|
|
|
|
forks <- ExceptT $ getForks' mtime
|
|
|
|
let formatted = intercalate "\n" $ fmap
|
|
|
|
(\Repo {..} ->
|
|
|
|
T.unpack (untagName $ simpleOwnerLogin repoOwner)
|
|
|
|
<> "/"
|
|
|
|
<> T.unpack (untagName repoName)
|
|
|
|
)
|
|
|
|
forks
|
|
|
|
lift $ putStrLn $ formatted
|
|
|
|
pure ()
|
2020-01-30 18:40:08 +00:00
|
|
|
case e of
|
2020-01-30 22:12:46 +00:00
|
|
|
Right () -> _info "success!"
|
|
|
|
Left t -> die (color Red $ t)
|