Created
July 3, 2018 23:40
-
-
Save nsaunders/30f4e53df3f58a0f465dd2fb27ec9152 to your computer and use it in GitHub Desktop.
lookupEnv / MaybeT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.Trans.Class (lift) | |
import Control.Monad.Trans.Maybe (MaybeT(..), runMaybeT) | |
import Data.Maybe (Maybe(..), fromMaybe) | |
import System.Environment (lookupEnv) | |
data ConnectInfo = ConnectInfo { host :: String, user :: String, pass :: String } | |
deriving Show | |
defaultConnectInfo :: ConnectInfo | |
defaultConnectInfo = ConnectInfo "localhost" "user" "pass" | |
connectInfo :: IO (Maybe ConnectInfo) | |
connectInfo = runMaybeT $ do | |
host <- MaybeT $ lookupEnv "PGHOST" | |
user <- MaybeT $ lookupEnv "PGUSER" | |
pass <- MaybeT $ lookupEnv "PGPASS" | |
return $ ConnectInfo host user pass | |
main :: IO () | |
main = do | |
x <- connectInfo | |
putStrLn $ show $ fromMaybe defaultConnectInfo x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment