packages feed

primula-board-0.0.1: Config.hs

-- | Config.hs
-- A module which parse config file and contain Config structure for
-- further usage.

module Config (
    Config(..),
    readConfig
) where

import Data.ConfigFile
import System.Directory

import Control.Monad.Error

-- | Config structure.
data Config = Config
    { cport :: Int
    , cprivateKey :: String
    } deriving Show

-- | Read config file in home directory and return conf structure.
readConfig :: String -> IO Config
readConfig filePath = do
  home <- getHomeDirectory
  config <- runErrorT $ do
      conf <- join $ liftIO $ readfile emptyCP (home++"/"++filePath)

      let param = get conf "board"
      liftM2 Config (liftM read (param "port"))
                    (param "privatekey")
  case config of
    Left e -> error $ "Can't parse config file.\n"++show e
    Right conf -> return conf