-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcurl.hs
49 lines (44 loc) · 1.5 KB
/
curl.hs
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.Lens ((^.))
import Data.Foldable (forM_)
import Data.Text.Lazy.Encoding (decodeUtf8)
import qualified Data.Text.Lazy.IO as TIO (putStrLn)
import qualified Network.Wreq as W
import qualified Network.Wreq.Session as WS
import Options.Applicative (Parser, ParserInfo, argument,
execParser, fullDesc, header, help,
helper, info, many, metavar, progDesc,
str, (<*>), (<>))
-- curl
main :: IO ()
main = do
-- run the options parser over the cli arguments
opts <- execParser optsParserInfo
-- start up a wreq session
responses <- WS.withSession $ \sess ->
-- make a get request for each url
traverse (WS.get sess) (urls opts)
-- extract the bodies from each request
let bodies = map (\r -> decodeUtf8 $ r ^. W.responseBody) responses
-- print the bodies
forM_ bodies TIO.putStrLn
-- structure to hold cli arguments
data Options = Options
{ urls :: [String]
}
-- Parser for cli arguments
optsParser :: Parser Options
optsParser = Options
<$> many (
argument str
( metavar "URLS"
<> help "Urls to request"
)
)
-- Adding program help text to the parser
optsParserInfo :: ParserInfo Options
optsParserInfo = info (helper <*> optsParser)
( fullDesc
<> progDesc "A bad clone of curl"
<> header "curl - a bad clone of the real curl"
)