registry-0.1.6.3: test/Test/Tutorial/Exercise5.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# OPTIONS_GHC -fno-warn-partial-type-signatures #-}
module Test.Tutorial.Exercise5 where
import qualified Data.ByteString.Char8 as BS8
import Data.Registry
import Data.Text.Encoding (decodeUtf8)
import Protolude
import System.Directory (doesFileExist)
import Test.Tutorial.Application
newCheckedSecretReader :: SecretReaderConfig -> Logger IO -> IO (SecretReader IO)
newCheckedSecretReader (SecretReaderConfig path) logger = do
exists <- doesFileExist (toS path)
if not exists then fileDoesNotExist else pure ()
pure SecretReader {
readSecret = do
if exists then Just . decodeUtf8 <$> BS8.readFile (toS path)
else fileDoesNotExist $> Nothing
}
where
fileDoesNotExist = error logger ("file does not exist at " <> path)
registryIO :: Registry _ _
registryIO =
funTo @IO App
+: funTo @IO newLogger
+: funTo @IO newConsole
+: funTo @IO newUserInput
+: funTo @IO newRng
+: funAs @IO newCheckedSecretReader
+: valTo @IO (SecretReaderConfig "txe/tests/Test/Tutorial/secret.txt")
+: end
newAppIO :: IO App
newAppIO = make @(IO App) registryIO