namecoin-update/Main.hs
2015-08-28 15:17:36 +00:00

37 lines
1.2 KiB
Haskell

{-# Language TemplateHaskell, RecordWildCards #-}
import System.Process
import System.Exit
import Data.Aeson
import Data.Aeson.TH
import Data.ByteString.Lazy.Char8 (pack)
readCommand = readProcess "namecoin-cli" ["name_list"] ""
updateCommand n v = readProcessWithExitCode "namecoin-cli" ["name_update", n, v] ""
data Name = Name { name :: String
, value :: String
, expires_in :: Int
}
deriveJSON defaultOptions ''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"