module Freckle.App.Dotenv
( load
, loadTest
, loadFile
) where
import Freckle.App.Prelude
import qualified Configuration.Dotenv.Compat as Dotenv
import System.FilePath (takeDirectory, (</>))
import UnliftIO.Directory (doesFileExist, getCurrentDirectory)
load :: IO ()
load :: IO ()
load = FilePath -> IO ()
loadFile FilePath
".env"
loadTest :: IO ()
loadTest :: IO ()
loadTest = FilePath -> IO ()
loadFile FilePath
".env.test"
loadFile :: FilePath -> IO ()
loadFile :: FilePath -> IO ()
loadFile = forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
(a -> f b) -> t a -> f ()
traverse_ forall {m :: * -> *}. MonadIO m => FilePath -> m ()
go forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< FilePath -> IO (Maybe FilePath)
locateInParents
where
go :: FilePath -> m ()
go FilePath
path = do
let examplePath :: FilePath
examplePath = FilePath -> FilePath
takeDirectory FilePath
path FilePath -> FilePath -> FilePath
</> FilePath
".env.example"
Bool
exampleExists <- forall (m :: * -> *). MonadIO m => FilePath -> m Bool
doesFileExist FilePath
examplePath
forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). MonadIO m => Config -> m ()
Dotenv.loadFile forall a b. (a -> b) -> a -> b
$ Dotenv.Config
{ configPath :: [FilePath]
Dotenv.configPath = [FilePath
path]
, configExamplePath :: [FilePath]
Dotenv.configExamplePath = [ FilePath
examplePath | Bool
exampleExists ]
, configOverride :: Bool
Dotenv.configOverride = Bool
False
}
locateInParents :: FilePath -> IO (Maybe FilePath)
locateInParents :: FilePath -> IO (Maybe FilePath)
locateInParents FilePath
path = FilePath -> IO (Maybe FilePath)
go forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *). MonadIO m => m FilePath
getCurrentDirectory
where
go :: FilePath -> IO (Maybe FilePath)
go FilePath
cwd = do
let absPath :: FilePath
absPath = FilePath
cwd FilePath -> FilePath -> FilePath
</> FilePath
path
Bool
exists <- forall (m :: * -> *). MonadIO m => FilePath -> m Bool
doesFileExist FilePath
absPath
if Bool
exists
then forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just FilePath
absPath
else if FilePath
cwd forall a. Eq a => a -> a -> Bool
== FilePath
"/" then forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing else FilePath -> IO (Maybe FilePath)
go forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath
takeDirectory FilePath
cwd