summaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: 75a4396a0a4adb35776efafb78a85e2217e0fedd (plain)
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
module Main
    ( main
    ) where

import Data.Aeson.TH (defaultOptions, deriveJSON)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text)
import qualified Data.Text.IO as Text.IO
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Network.HTTP.Req
    ( runReq
    , defaultHttpConfig
    , req
    , GET(..)
    , https
    , jsonResponse
    , NoReqBody(..)
    , (/:)
    , responseBody
    )
import Data.Maybe (fromMaybe)

newtype PackagistPackage = PackagistPackage
    { version :: Text
    } deriving (Eq, Show)

$(deriveJSON defaultOptions ''PackagistPackage)

newtype PackagistResponse = PackagistResponse
    { packages :: HashMap Text (Vector PackagistPackage)
    } deriving (Eq, Show)

$(deriveJSON defaultOptions ''PackagistResponse)

main :: IO ()
main = do
    packagistResponse <- runReq defaultHttpConfig $
        let uri = https "repo.packagist.org" /: "p2" /: "composer" /: "composer.json"
         in req GET uri NoReqBody jsonResponse mempty
    let packagistPackages = packages $ responseBody packagistResponse

    Text.IO.putStrLn $ fromMaybe ""
        $ HashMap.lookup "composer/composer" packagistPackages
        >>= fmap (version . fst) . Vector.uncons