Add listing gists

This commit is contained in:
Julian Ospald 2020-02-02 15:34:20 +01:00
parent f1186c3b11
commit 97041d7012
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
2 changed files with 87 additions and 22 deletions

View File

@ -42,6 +42,7 @@ data Command
| Del DelOptions | Del DelOptions
| ListForks ListForkOptions | ListForks ListForkOptions
| CreateGist CreateGistOptions | CreateGist CreateGistOptions
| ListGist ListGistOptions
data ForkOptions = ForkOptions data ForkOptions = ForkOptions
{ {
@ -74,6 +75,12 @@ data Input
= FileInput [ByteString] = FileInput [ByteString]
| StdInput | StdInput
data ListGistOptions = ListGistOptions
{
lgSince :: Maybe ByteString
, lgDesc :: Bool
}
fileInput :: Parser Input fileInput :: Parser Input
fileInput = fileInput =
FileInput FileInput
@ -117,9 +124,12 @@ opts = subparser
<$> (info (lForkOpts <**> helper) (progDesc "List my forks")) <$> (info (lForkOpts <**> helper) (progDesc "List my forks"))
) )
<> command <> command
"gist" "gistc"
(CreateGist <$> (info (cGistOpts <**> helper) (progDesc "Create gist")) (CreateGist <$> (info (cGistOpts <**> helper) (progDesc "Create gist"))
) )
<> command
"gistl"
(ListGist <$> (info (lGistOpts <**> helper) (progDesc "List gists")))
) )
configOpts :: Parser ConfigOptions configOpts :: Parser ConfigOptions
@ -183,6 +193,20 @@ cGistOpts =
"Whether gist should be private (default: public)" "Whether gist should be private (default: public)"
) )
lGistOpts :: Parser ListGistOptions
lGistOpts =
ListGistOptions
<$> optional
(strOption
(short 's' <> long "since" <> metavar "SINCE" <> help
"The repository to fork"
)
)
<*> switch
(short 'd' <> long "descriptions" <> help
"Whether to show descriptions (default: False)"
)
main :: IO () main :: IO ()
main = do main = do
@ -222,26 +246,7 @@ main = do
-- list-forks -- list-forks
ListForks (ListForkOptions {..}) -> run $ do ListForks (ListForkOptions {..}) -> run $ do
mtime <- liftIO $ case lSince of mtime <- parseSince lSince
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 <- withExceptT show $ getForks mtime forks <- withExceptT show $ getForks mtime
let let
formatted = formatted =
@ -258,7 +263,7 @@ main = do
liftIO $ putStrLn $ formatted liftIO $ putStrLn $ formatted
pure () pure ()
-- gist -- gistc
CreateGist (CreateGistOptions {..}) -> run $ do CreateGist (CreateGistOptions {..}) -> run $ do
let desc = maybe T.empty E.decodeUtf8 description let desc = maybe T.empty E.decodeUtf8 description
public = not private public = not private
@ -276,8 +281,54 @@ main = do
postGistFiles files' desc public postGistFiles files' desc public
liftIO $ putStrLn $ T.unpack $ getUrl $ gistHtmlUrl gist liftIO $ putStrLn $ T.unpack $ getUrl $ gistHtmlUrl gist
-- gistl
ListGist (ListGistOptions {..}) -> run $ do
mtime <- parseSince lgSince
gists <- listGists mtime
let
formatted =
gridString
( [column expand left def def]
<> (if lgDesc then [column expand left def def] else [])
<> [column expand left def def]
)
$ fmap
(\Gist {..} ->
[(T.unpack . getUrl $ gistHtmlUrl)]
<> (if lgDesc
then
[ T.unpack $ fromMaybe (T.pack "(No desc)")
gistDescription
]
else []
)
<> [ formatShow (iso8601Format :: Format Day)
(utctDay gistUpdatedAt)
]
)
gists
liftIO $ putStrLn $ formatted
-- print error, if any -- print error, if any
case e of case e of
Right () -> pure () Right () -> pure ()
Left t -> die (color Red $ t) Left t -> die (color Red $ t)
where
parseSince lSince = do
liftIO $ 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

View File

@ -33,6 +33,7 @@ module GHup
, postGistStdin , postGistStdin
, postGistFiles , postGistFiles
, postGist , postGist
, listGists
-- * Parsers -- * Parsers
, parseURL , parseURL
, ghURLParser , ghURLParser
@ -67,6 +68,8 @@ import qualified Data.Text.Encoding as E
import qualified Data.Text.IO as T import qualified Data.Text.IO as T
import Data.Traversable import Data.Traversable
import Data.Time.Clock import Data.Time.Clock
import Data.Time.Format.ISO8601
import qualified Data.Vector as V
import Data.Word8 import Data.Word8
import GHC.Exts ( toList ) import GHC.Exts ( toList )
import GitHub.Auth import GitHub.Auth
@ -398,6 +401,17 @@ postGist :: (MonadIO m, MonadReader Settings m)
postGist greq = githubAuth (command Post [T.pack "gists"] (encode greq)) postGist greq = githubAuth (command Post [T.pack "gists"] (encode greq))
listGists :: (MonadIO m, MonadReader Settings m)
=> Maybe UTCTime
-> ExceptT Error m [Gist]
listGists mtime = do
let queryString = case mtime of
Just time -> [(u8 "since", Just $ UTF8.fromString $ iso8601Show time)]
Nothing -> []
V.toList <$> githubAuth (pagedQuery [T.pack "gists"] queryString FetchAll)
--------------- ---------------
--[ Parsers ]-- --[ Parsers ]--