diff --git a/Main.hs b/Main.hs index 689e7dd..76d17aa 100644 --- a/Main.hs +++ b/Main.hs @@ -1,10 +1,34 @@ -import Network.Wreq +{-# LANGUAGE RecordWildCards #-} + +import Network.Wreq (get, responseBody) +import System.Console.ArgParser import Control.Lens import Data.Aeson import Json -main = do - req <- get "http://namecoin.dns:8088/id/rnhmjoj" - case decode (req ^. responseBody) of - Just res -> putStrLn $ repr res 0 - Nothing -> putStrLn "Error parsing data" +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"