-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added System.Environment.Lookup and System.Environment.LookupSpec to core - Removed common parts from - Cardano.Environment.HttpBridge - Cardano.Environment.HttpBridgeSpec - Cardano.Environment.Jormungandr - Cardano.Environment.JormungandrSpec (added)
- Loading branch information
Showing
9 changed files
with
246 additions
and
240 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
-- | Helpers for reading ENV vars using 'unsafePerformIO' with readable error | ||
-- messages. | ||
-- | ||
-- Copyright: © 2018-2019 IOHK | ||
-- License: MIT | ||
-- | ||
module Cardano.Environment | ||
( | ||
ErrMissingOrInvalidEnvVar(..) | ||
, unsafeLookupEnv | ||
) where | ||
|
||
import Prelude | ||
|
||
import Control.Exception | ||
( Exception (..), throwIO ) | ||
import Data.Text | ||
( Text ) | ||
import Data.Text.Class | ||
( FromText (..), TextDecodingError (..) ) | ||
import Fmt | ||
( Buildable (..), nameF, padLeftF, pretty ) | ||
import System.Environment | ||
( getProgName, lookupEnv ) | ||
import System.IO.Unsafe | ||
( unsafePerformIO ) | ||
|
||
import qualified Data.Text as T | ||
|
||
|
||
-- | Fatal exception thrown when a required ENV var is missing upon start-up. | ||
data ErrMissingOrInvalidEnvVar = ErrMissingOrInvalidEnvVar | ||
{ name :: String | ||
, command :: String | ||
, additionalContext :: Maybe (String, TextDecodingError) | ||
} | ||
|
||
instance Show ErrMissingOrInvalidEnvVar where | ||
show = displayException | ||
|
||
-- | Produces a nice terminal output so that the error is very readable. | ||
-- | ||
-- @ | ||
-- $ NETWORK=patate cardano-wallet-launcher | ||
-- Starting... | ||
-- cardano-wallet-launcher: Missing or invalid ENV var: | ||
-- | ||
-- ENV[NETWORK] = patate | ||
-- | | ||
-- | | ||
-- *--> patate is neither "mainnet", "testnet" nor "staging" | ||
-- | ||
-- @ | ||
-- | ||
-- @ | ||
-- $ cardano-wallet-launcher | ||
-- Starting... | ||
-- cardano-wallet-launcher: Missing or invalid ENV var: | ||
-- | ||
-- ENV[NETWORK] = ? | ||
-- | ||
-- What about trying to provide a valid ENV var `NETWORK=value cardano-wallet-launcher` ? | ||
-- @ | ||
instance Exception ErrMissingOrInvalidEnvVar where | ||
displayException (ErrMissingOrInvalidEnvVar n cmd ctx) = pretty $ mempty | ||
<> nameF "Missing or invalid ENV var" | ||
( "\n ENV[" <> build n <> "] = " <> ctxF ) | ||
where | ||
ctxF = case ctx of | ||
Nothing -> "?" | ||
<> "\n\nWhat about trying to provide a valid ENV var " | ||
<> "`" <> build n <> "=value " <> build cmd <> "` ?" | ||
Just (v, err) -> | ||
let | ||
pad = length n + (length v `div` 2) + 11 | ||
in | ||
build v | ||
<> "\n " <> padLeftF @Text pad ' ' "| " | ||
<> "\n " <> padLeftF @Text pad ' ' "| " | ||
<> "\n " <> padLeftF @Text pad ' ' "*--> " | ||
<> build err | ||
|
||
-- | Lookup the environment for a given variable | ||
unsafeLookupEnv | ||
:: FromText a | ||
=> String | ||
-> a | ||
unsafeLookupEnv k = unsafePerformIO $ do | ||
cmd <- getProgName | ||
v <- lookupEnv k >>= \case | ||
Just v -> return v | ||
Nothing -> throwIO $ ErrMissingOrInvalidEnvVar | ||
{ name = k | ||
, command = cmd | ||
, additionalContext = Nothing | ||
} | ||
case fromText (T.pack v) of | ||
Right a -> return a | ||
Left err -> throwIO $ ErrMissingOrInvalidEnvVar | ||
{ name = k | ||
, command = cmd | ||
, additionalContext = Just (v, err) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
|
||
module Cardano.EnvironmentSpec | ||
( spec | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Environment | ||
( ErrMissingOrInvalidEnvVar (..), unsafeLookupEnv ) | ||
import Data.Maybe | ||
( isNothing ) | ||
import Data.Text.Class | ||
( FromText (..), TextDecodingError (..), ToText (..) ) | ||
import GHC.Generics | ||
( Generic ) | ||
import System.Environment | ||
( setEnv, unsetEnv ) | ||
import Test.Hspec | ||
( Spec, describe, it, shouldThrow ) | ||
import Test.QuickCheck | ||
( Arbitrary (..) ) | ||
import Test.QuickCheck.Arbitrary.Generic | ||
( genericArbitrary, genericShrink ) | ||
|
||
import qualified Data.Text as T | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "ErrMissingOrInvalidEnvVar (Show / displayException)" $ do | ||
let errNoAdditionalContext = ErrMissingOrInvalidEnvVar | ||
{ name = "PATATE" | ||
, command = "my-command" | ||
, additionalContext = Nothing | ||
} | ||
let errWithAdditionalContext = ErrMissingOrInvalidEnvVar | ||
{ name = "PATATE" | ||
, command = "my-command" | ||
, additionalContext = Just | ||
("💩" | ||
, TextDecodingError | ||
{ getTextDecodingError = "not a valid value" } | ||
) | ||
} | ||
it (show errNoAdditionalContext) True | ||
it (show errWithAdditionalContext) True | ||
|
||
describe "unsafeLookupEnv" $ do | ||
it "throws with no context when variable isn't present" $ do | ||
unsetEnv "PATATE" -- Just in case | ||
let io = | ||
unsafeLookupEnv @Network "PATATE" `seq` (return ()) | ||
let selector (ErrMissingOrInvalidEnvVar n _ c) = | ||
n == "PATATE" && isNothing c | ||
io `shouldThrow` selector | ||
|
||
it "throws with extra context when variable is present but invalid" $ do | ||
setEnv "PATATE" "not-a-network" | ||
let ctx = | ||
( "not-a-network" | ||
, TextDecodingError "not-a-network is neither \"mainnet\",\ | ||
\ \"testnet\" nor \"staging\"." | ||
) | ||
let selector (ErrMissingOrInvalidEnvVar n _ c) = | ||
n == "PATATE" && c == Just ctx | ||
let io = | ||
unsafeLookupEnv @Network "PATATE" `seq` (return ()) | ||
io `shouldThrow` selector | ||
|
||
{------------------------------------------------------------------------------- | ||
Types | ||
-------------------------------------------------------------------------------} | ||
|
||
data Network = Mainnet | Testnet | Staging | ||
deriving Generic | ||
|
||
instance Arbitrary Network where | ||
arbitrary = genericArbitrary | ||
shrink = genericShrink | ||
|
||
instance FromText Network where | ||
fromText = \case | ||
"mainnet" -> Right Mainnet | ||
"testnet" -> Right Testnet | ||
"staging" -> Right Staging | ||
s -> Left $ TextDecodingError $ T.unpack s | ||
<> " is neither \"mainnet\", \"testnet\" nor \"staging\"." | ||
|
||
instance ToText Network where | ||
toText = \case | ||
Mainnet -> "mainnet" | ||
Testnet -> "testnet" | ||
Staging -> "staging" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,6 @@ library | |
, cryptonite | ||
, digest | ||
, exceptions | ||
, fmt | ||
, http-api-data | ||
, http-client | ||
, http-media | ||
|
This file contains 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
Oops, something went wrong.