-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.hs
More file actions
49 lines (37 loc) · 1.35 KB
/
Main.hs
File metadata and controls
49 lines (37 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Main where
import Control.Monad (join)
import Options.Applicative
import qualified Data.Text as T
import Gitfred.Request
import qualified Alfred as A
data Command
= Search (Maybe String)
| Cache
| Increment String
main :: IO ()
main = run =<< execParser
(parseCommand `withInfo` "Interact with the GitHub API")
run :: Command -> IO ()
run cmd =
case cmd of
Search mquery -> A.searchItems cacheFile mquery
Cache -> do
repos <- getRepos
A.writeItems cacheFile $ fmap A.toAlfredItem repos
Increment url -> A.incrementVisits cacheFile $ T.pack url
withInfo :: Parser a -> String -> ParserInfo a
withInfo opts desc = info (helper <*> opts) $ progDesc desc
parseCommand :: Parser Command
parseCommand =
subparser $
command "search" (parseSearch `withInfo` "Search for GitHub repos") <>
command "cache" (parseCache `withInfo` "Cache GitHub repos") <>
command "increment" (parseIncrement `withInfo` "Increment a repo's visit count")
parseSearch :: Parser Command
parseSearch = Search <$> optional (argument str (metavar "QUERY"))
parseCache :: Parser Command
parseCache = pure Cache
parseIncrement :: Parser Command
parseIncrement = Increment <$> argument str (metavar "REPO_URL")
cacheFile :: String
cacheFile = "repos.csv"