Do some order

This commit is contained in:
Rnhmjoj 2014-11-25 00:22:54 +01:00
parent 28ec0bc1e5
commit 113e08365e

29
Main.hs
View File

@ -6,13 +6,12 @@ import Data.Maybe (fromJust)
import Data.HashMap.Strict (delete)
import Control.Lens
import Control.Applicative
import qualified Data.ByteString.Lazy.Char8 as C
import System.Console.ArgParser
import System.Process
import qualified Data.ByteString.Lazy.Char8 as C
import Json
data ProgArgs = ProgArgs
{ name :: String
, url :: String
@ -21,6 +20,7 @@ data ProgArgs = ProgArgs
, raw :: Bool
} deriving (Show)
parser :: ParserSpec ProgArgs
parser = ProgArgs
`parsedBy` reqPos "name" `Descr` "Namecoin name id"
@ -30,12 +30,25 @@ parser = ProgArgs
`andBy` boolFlag "block" `Descr` "Show blockchain data (require local connecton)"
`andBy` boolFlag "raw" `Descr` "Print raw JSON data"
interface :: IO (CmdLnInterface ProgArgs)
interface =
(`setAppDescr` "Query the namecoin blockchain") <$>
(`setAppEpilog` "Stat rosa pristina nomine, nomina nuda tenemus.") <$>
mkApp parser
main :: IO ()
main = interface >>= flip runApp exec
exec :: ProgArgs -> IO ()
exec args@ProgArgs{..} =
if dnschain
then handleDnschain url name raw
else handleLocal name block
handleLocal :: String -> Bool -> IO ()
handleLocal name block = do
out <- readProcess "namecoind" ["name_show", name] ""
@ -48,6 +61,7 @@ handleLocal name block = do
extra = toJSON $ delete "value" res
Nothing -> putStrLn "Error parsing data"
handleDnschain :: String -> String -> Bool -> IO ()
handleDnschain url name raw = do
req <- get (url ++ name)
@ -58,12 +72,3 @@ handleDnschain url name raw = do
case decode body of
Just res -> repr res
Nothing -> "Error parsing data"
exec :: ProgArgs -> IO ()
exec args@ProgArgs{..} =
if dnschain
then handleDnschain url name raw
else handleLocal name block
main :: IO ()
main = interface >>= flip runApp exec