rosa/Main.hs

35 lines
1001 B
Haskell
Raw Normal View History

2014-11-21 00:14:57 +01:00
{-# LANGUAGE RecordWildCards #-}
import Network.Wreq (get, responseBody)
import System.Console.ArgParser
2014-11-20 21:39:25 +01:00
import Control.Lens
import Data.Aeson
import Json
2014-11-21 00:14:57 +01:00
data Name = Name
{ name :: String
, url :: String
, local :: Bool
, raw :: Bool
} deriving (Show)
parser :: ParserSpec Name
parser = Name
`parsedBy` reqPos "name" `Descr` "Namecoin name id"
`andBy` optPos "http://dns.dnschain.net/" "url" `Descr` "Use custom dnschain API url"
`andBy` boolFlag "local" `Descr` "Use local namecoind db"
`andBy` boolFlag "raw" `Descr` "Print raw JSON data"
main :: IO ()
main = mkApp parser >>= flip runApp exec
exec :: Name -> IO ()
exec args@Name{..} =
if local then putStrLn "to do"
else do
req <- get (url ++ name)
if raw then print $ req ^. responseBody
else case decode (req ^. responseBody) of
Just res -> putStrLn $ repr res
Nothing -> putStrLn "Error parsing data"