{-# LANGUAGE OverloadedStrings #-}
module Bcc.CLI.Environment
( EnvSocketError(..)
, readEnvSocketPath
, renderEnvSocketError
) where
import Bcc.Prelude
import Prelude (String)
import Control.Monad.Trans.Except.Extra (left)
import qualified Data.Text as Text
import System.Environment (lookupEnv)
import Bcc.CLI.Helpers (textShow)
import Bcc.CLI.Types (SocketPath (..))
newtype EnvSocketError = CliEnvVarLookup Text deriving Int -> EnvSocketError -> ShowS
[EnvSocketError] -> ShowS
EnvSocketError -> String
(Int -> EnvSocketError -> ShowS)
-> (EnvSocketError -> String)
-> ([EnvSocketError] -> ShowS)
-> Show EnvSocketError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EnvSocketError] -> ShowS
$cshowList :: [EnvSocketError] -> ShowS
show :: EnvSocketError -> String
$cshow :: EnvSocketError -> String
showsPrec :: Int -> EnvSocketError -> ShowS
$cshowsPrec :: Int -> EnvSocketError -> ShowS
Show
renderEnvSocketError :: EnvSocketError -> Text
renderEnvSocketError :: EnvSocketError -> Text
renderEnvSocketError EnvSocketError
err =
case EnvSocketError
err of
CliEnvVarLookup Text
txt ->
Text
"Error while looking up environment variable: BCC_NODE_SOCKET_PATH " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" Error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
forall a. Show a => a -> Text
textShow Text
txt
readEnvSocketPath :: ExceptT EnvSocketError IO SocketPath
readEnvSocketPath :: ExceptT EnvSocketError IO SocketPath
readEnvSocketPath =
ExceptT EnvSocketError IO SocketPath
-> (String -> ExceptT EnvSocketError IO SocketPath)
-> Maybe String
-> ExceptT EnvSocketError IO SocketPath
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (EnvSocketError -> ExceptT EnvSocketError IO SocketPath
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left (EnvSocketError -> ExceptT EnvSocketError IO SocketPath)
-> EnvSocketError -> ExceptT EnvSocketError IO SocketPath
forall a b. (a -> b) -> a -> b
$ Text -> EnvSocketError
CliEnvVarLookup (String -> Text
Text.pack String
envName)) (SocketPath -> ExceptT EnvSocketError IO SocketPath
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SocketPath -> ExceptT EnvSocketError IO SocketPath)
-> (String -> SocketPath)
-> String
-> ExceptT EnvSocketError IO SocketPath
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> SocketPath
SocketPath)
(Maybe String -> ExceptT EnvSocketError IO SocketPath)
-> ExceptT EnvSocketError IO (Maybe String)
-> ExceptT EnvSocketError IO SocketPath
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO (Maybe String) -> ExceptT EnvSocketError IO (Maybe String)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (String -> IO (Maybe String)
lookupEnv String
envName)
where
envName :: String
envName :: String
envName = String
"BCC_NODE_SOCKET_PATH"