namecoin-update/namecoin-update.hs

39 lines
1.2 KiB
Haskell
Raw Normal View History

2015-04-19 23:36:44 +02:00
{-# Language DeriveGeneric, RecordWildCards #-}
import System.Process
import System.Exit
import GHC.Generics
import Data.Aeson
import Data.ByteString.Lazy.Char8 (pack)
readCommand = readProcess "namecoind" ["name_list"] ""
updateCommand n v = readProcessWithExitCode "namecoind" ["name_update", n, v] ""
data Name =
Name { name :: String
, value :: String
, address :: String
, expires_in :: Int
} deriving (Show, Generic)
instance FromJSON Name
updateName :: Name -> IO Int
updateName Name{..}
| expires_in < 100 = do
(code, out, err) <- updateCommand name value
if code == ExitSuccess
then putStrLn (name ++ " updated: " ++ out) >> return 0
else putStrLn (name ++ " update failed: " ++ err) >> return 1
| otherwise = putStrLn ("No need to update " ++ name) >> return 0
main :: IO ()
main = do
out <- pack <$> readCommand
case eitherDecode out of
Left err -> putStrLn ("Error communicating with namecoin: " ++ err)
Right names -> do
errs <- sum <$> mapM updateName (names :: [Name])
if errs > 0
then putStrLn (show errs ++ " updates failed")
else putStrLn "All ok"