-
Notifications
You must be signed in to change notification settings - Fork 730
/
Copy pathEnvironment.hs
42 lines (35 loc) · 1.21 KB
/
Environment.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Cardano.Api.Environment
( EnvSocketError(..)
, SocketPath(..)
, readEnvSocketPath
, renderEnvSocketError
) where
import Data.Aeson
import Data.Text (Text)
import qualified Data.Text as Text
import System.Environment (lookupEnv)
import Cardano.Api.Utils (textShow)
newtype SocketPath
= SocketPath { unSocketPath :: FilePath }
deriving (FromJSON, Show, Eq, Ord)
newtype EnvSocketError = CliEnvVarLookup Text deriving Show
renderEnvSocketError :: EnvSocketError -> Text
renderEnvSocketError err =
case err of
CliEnvVarLookup txt ->
"Error while looking up environment variable: CARDANO_NODE_SOCKET_PATH " <> " Error: " <> textShow txt
-- | Read the node socket path from the environment.
-- Fails if the environment variable is not set.
readEnvSocketPath :: IO (Either EnvSocketError SocketPath)
readEnvSocketPath = do
mEnvName <- lookupEnv envName
case mEnvName of
Just sPath ->
return . Right $ SocketPath sPath
Nothing ->
return . Left $ CliEnvVarLookup (Text.pack envName)
where
envName :: String
envName = "CARDANO_NODE_SOCKET_PATH"