rosa 0.4.0.0 → 0.5.0.0
raw patch · 4 files changed
+68/−37 lines, 4 filesdep +uri-encode
Dependencies added: uri-encode
Files
- README.md +15/−3
- rosa.cabal +2/−2
- src/Json.hs +4/−4
- src/Main.hs +47/−28
README.md view
@@ -34,6 +34,18 @@ txid: 425cd3cd854a18bb0494f139ae4b89b9f2892d3a9b865d8f2a704eea15723f35 ### License-Dual licensed under the MIT and GPL licenses: -http://www.opensource.org/licenses/mit-license.php -http://www.gnu.org/licenses/gpl.html++Copyright (C) 2015 Michele Guerini Rocco++This program is free software: you can redistribute it and/or modify+it under the terms of the GNU General Public License as published by+the Free Software Foundation, either version 3 of the License, or+(at your option) any later version.++This program is distributed in the hope that it will be useful,+but WITHOUT ANY WARRANTY; without even the implied warranty of+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+GNU General Public License for more details.++You should have received a copy of the GNU General Public License+along with this program. If not, see <http://www.gnu.org/licenses/>.
rosa.cabal view
@@ -1,5 +1,5 @@ name: rosa-version: 0.4.0.0+version: 0.5.0.0 synopsis: Query the namecoin blockchain description: @@ -29,7 +29,7 @@ default-language: Haskell2010 other-extensions: RecordWildCards, OverloadedStrings build-depends: base >=4.8 && <5.0 , aeson, text,- vector, unordered-containers,+ vector, uri-encode, unordered-containers, wreq, lens, bytestring, directory, optparse-applicative, namecoin-update ghc-options: -O2
src/Json.hs view
@@ -9,19 +9,19 @@ import qualified Data.Vector as V import qualified Data.HashMap.Strict as H --- | Get the JSON value of a key+-- | Gets the JSON value of a key (|.) :: Object -> Text -> Value obj |. key = case parse (.: key) obj of Success val -> val Error err -> toJSON err --- | Get the 'String' value of a key+-- | Gets the 'String' value of a key (|:) :: Object -> Text -> String obj |: key = repr (obj |. key) --- | Create a String representation of a 'Value'+-- | Creates a 'String' representation of a 'Value' repr :: Value -> String repr obj = repr' obj 0 where repr' val lev =@@ -33,7 +33,7 @@ Bool x -> show x Null -> "null" mapl f v = V.toList (V.map f v)- newline n = if n == 1 then id else drop 1+ newline n = if n /= 0 then id else drop 1 indent l = '\n' : (concat . replicate l) " " dump o l k = concat [indent l, unpack k, ": ", repr' (o |. k) (l+1)]
src/Main.hs view
@@ -1,23 +1,34 @@-{-# LANGUAGE RecordWildCards, OverloadedStrings #-}+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE OverloadedStrings #-}+ -- | Main module module Main where +-- Rosa modules import Json -import Control.Lens (view)-import Control.Monad (when)-import Namecoin (Error, rpcRequest, uri)+-- Networking+import Namecoin (rpcRequest, uri) import Network.Wreq (get, responseBody)+import qualified Network.URI.Encode as U++-- IO+import Options.Applicative import System.Directory (XdgDirectory(..), getXdgDirectory)-import Data.Aeson (Value(..), decode, toJSON)-import Data.Maybe (fromJust, fromMaybe)+import qualified Data.Text.IO as T++-- Data manipulation+import Data.Text (Text)+import Data.Aeson (Value(..), encode, decode, toJSON) import Data.HashMap.Strict (delete)-import Data.ByteString.Lazy.Char8 (pack)-import Data.Monoid-import Options.Applicative+import Data.ByteString.Lazy.Char8 (pack, unpack) -import qualified Data.Text.IO as T+-- Misc+import Data.Maybe (fromMaybe)+import Control.Lens (view)+import Control.Monad (when) + -- * CLI interface -- | Program arguments record@@ -41,20 +52,20 @@ <> short 'u' <> value "http://namecoin.dns" <> metavar "URL"- <> help "Use custom api url" )+ <> help "Use custom API URL" ) <*> (optional $ strOption $ long "conf" <> short 'c' <> metavar "FILE"- <> help "Use custom api url" )+ <> help "Use custom namecoin config file" ) <*> switch ( long "dnschain" <> short 'd'- <> help "Use dnschain api" )+ <> help "Use dnschain API " ) <*> switch ( long "block" <> short 'b'- <> help "Show blockchain data (require local connection)" )+ <> help "Show blockchain data" ) <*> switch ( long "raw" <> short 'r'@@ -77,8 +88,8 @@ main = execParser description >>= exec where exec Options{..} = if dnschain- then doDnschain url name raw- else doLocal name block conf+ then doDnschain url name raw block+ else doLocal name raw block conf -- | Load namecoin configuration@@ -92,27 +103,35 @@ -- | Connect to local namecoin node-doLocal :: String -> Bool -> Maybe FilePath -> IO ()-doLocal name block conf = do+doLocal :: String -> Bool -> Bool -> Maybe FilePath -> IO ()+doLocal name raw block conf = do uri <- apiURI conf req <- rpcRequest uri "name_show" [name] case req of Left err -> putStrLn ("The lookup failed: " ++ err) Right (Object res) -> do- pprint $ tryParse (res |: "value")- when block (pprint blockInfo)+ if raw+ then putStrLn (unpack $ encode res)+ else do+ pprint $ tryParse (res |: "value")+ when block (pprint blockInfo) where tryParse = fromMaybe (res |. "value") . decode . pack blockInfo = toJSON (delete "value" res) --- | Connect to dnschain api endpoint-doDnschain :: String -> String -> Bool -> IO ()-doDnschain url name raw = do- body <- view responseBody <$> get (url++"/v1/namecoin/key/"++name)+-- | Connect to dnschain API endpoint+doDnschain :: String -> String -> Bool -> Bool -> IO ()+doDnschain url name raw block = do+ body <- view responseBody <$> get (url++"/v1/namecoin/key/"++U.encode name) if raw- then print body- else putStrLn $+ then putStrLn (unpack body)+ else do case decode body of- Just res -> repr res- Nothing -> "Error parsing data"+ Nothing -> putStrLn "Error parsing data"+ Just res -> putStrLn $+ if block+ then repr res+ else repr $ (res .| "data") .| "value"++ where (Object x) .| y = x |. y