Change default connection method
This commit is contained in:
parent
8d1cb7b3ef
commit
28ec0bc1e5
29
Main.hs
29
Main.hs
@ -4,7 +4,6 @@ import Network.Wreq (get, responseBody)
|
||||
import Data.Aeson (decode, toJSON)
|
||||
import Data.Maybe (fromJust)
|
||||
import Data.HashMap.Strict (delete)
|
||||
import Control.Monad (when)
|
||||
import Control.Lens
|
||||
import Control.Applicative
|
||||
|
||||
@ -17,7 +16,7 @@ import Json
|
||||
data ProgArgs = ProgArgs
|
||||
{ name :: String
|
||||
, url :: String
|
||||
, local :: Bool
|
||||
, dnschain :: Bool
|
||||
, block :: Bool
|
||||
, raw :: Bool
|
||||
} deriving (Show)
|
||||
@ -26,8 +25,8 @@ parser :: ParserSpec ProgArgs
|
||||
parser = ProgArgs
|
||||
`parsedBy` reqPos "name" `Descr` "Namecoin name id"
|
||||
`andBy` optFlag "http://dns.dnschain.net/" "url"
|
||||
`Descr` "Use custom dnschain API url"
|
||||
`andBy` boolFlag "local" `Descr` "Use local namecoind db"
|
||||
`Descr` "Use custom api url"
|
||||
`andBy` boolFlag "dnschain" `Descr` "Use dnschain api"
|
||||
`andBy` boolFlag "block" `Descr` "Show blockchain data (require local connecton)"
|
||||
`andBy` boolFlag "raw" `Descr` "Print raw JSON data"
|
||||
|
||||
@ -37,20 +36,20 @@ interface =
|
||||
(`setAppEpilog` "Stat rosa pristina nomine, nomina nuda tenemus.") <$>
|
||||
mkApp parser
|
||||
|
||||
exec :: ProgArgs -> IO ()
|
||||
exec args@ProgArgs{..} =
|
||||
if local
|
||||
then do
|
||||
handleLocal :: String -> Bool -> IO ()
|
||||
handleLocal name block = do
|
||||
out <- readProcess "namecoind" ["name_show", name] ""
|
||||
case decode (C.pack out) of
|
||||
Just res -> do
|
||||
putStrLn (repr value)
|
||||
when block $ putStrLn (repr extra)
|
||||
jprint $ reparse (res |: "value")
|
||||
if block then jprint extra else return ()
|
||||
where
|
||||
value = fromJust . decode . C.pack $ res |: "value"
|
||||
reparse = fromJust . decode . C.pack
|
||||
extra = toJSON $ delete "value" res
|
||||
Nothing -> putStrLn "Error parsing data"
|
||||
else do
|
||||
|
||||
handleDnschain :: String -> String -> Bool -> IO ()
|
||||
handleDnschain url name raw = do
|
||||
req <- get (url ++ name)
|
||||
let body = req ^. responseBody
|
||||
if raw
|
||||
@ -60,5 +59,11 @@ exec args@ProgArgs{..} =
|
||||
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
|
Loading…
Reference in New Issue
Block a user