namecoin-update/Main.hs
2016-08-29 06:07:56 +02:00

47 lines
1.3 KiB
Haskell

{-# Language TemplateHaskell, RecordWildCards #-}
import System.Process
import System.Exit
import Data.Aeson
import Data.Aeson.TH
import Data.ByteString.Lazy.Char8 (pack)
import Control.Exception (try)
data Name = Name
{ name :: String
, value :: String
, expires_in :: Int
}
deriveJSON defaultOptions ''Name
namecoin :: [String] -> CreateProcess
namecoin args = shell ("namecoin-cli -conf=$HOME/.config/namecoin " ++ unwords args)
readCommand = readCreateProcess (namecoin ["name_list"]) ""
updateCommand n v = readCreateProcessWithExitCode (namecoin ["name_update", n, v]) ""
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
Right names -> do
errs <- sum <$> mapM updateName (names :: [Name])
if errs > 0
then putStrLn (show errs ++ " updates failed")
else putStrLn "All ok"
Left err -> putStrLn ("Error communicating with namecoin: " ++ err)