From 19f30f33ccfbc401872e142fcf42e3ac32a5b26c Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Wed, 6 Jun 2018 16:17:44 -0700 Subject: [PATCH] Pull port and host from environment --- semantic.cabal | 1 + src/Semantic/Env.hs | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/Semantic/Env.hs diff --git a/semantic.cabal b/semantic.cabal index e524d64bb..590d106f9 100644 --- a/semantic.cabal +++ b/semantic.cabal @@ -153,6 +153,7 @@ library , Semantic.CLI , Semantic.Diff , Semantic.Distribute + , Semantic.Env , Semantic.Graph , Semantic.IO , Semantic.Log diff --git a/src/Semantic/Env.hs b/src/Semantic/Env.hs new file mode 100644 index 000000000..7c93f6788 --- /dev/null +++ b/src/Semantic/Env.hs @@ -0,0 +1,15 @@ +module Semantic.Env where + +import Control.Monad.IO.Class +import Prologue +import System.Environment +import Text.Read (readMaybe) + +envLookupHost :: MonadIO io => String -> String -> io String +envLookupHost defaultHost k = liftIO $ fromMaybe defaultHost <$> lookupEnv k + +envLookupPort :: MonadIO io => Int -> String -> io Int +envLookupPort defaultPort k = liftIO $ parsePort <$> lookupEnv k + where parsePort x | Just s <- x + , Just p <- readMaybe s = p + | otherwise = defaultPort