load-env 0.2.0.2 → 0.2.1.0
raw patch · 4 files changed
+57/−18 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- CHANGELOG.md +24/−1
- load-env.cabal +15/−14
- src/LoadEnv.hs +9/−3
- test/LoadEnvSpec.hs +9/−0
CHANGELOG.md view
@@ -1,6 +1,29 @@-## [*Unreleased*](https://github.com/pbrisbin/load-env/compare/v0.2.0.2...master)+## [*Unreleased*](https://github.com/pbrisbin/load-env/compare/v0.2.1.0...master) None++## [v0.2.1.0](https://github.com/pbrisbin/load-env/compare/v0.2.0.2...v0.2.1.0)++- Don't override values already set in the environment++ Given a hypothetical program `load-env`, which uses one of our `loadEnv`+ functions on `stdin`:++ Previously,++ ```+ FOO=bar load-env <<EOM+ FOO=bat+ EOM+ ```++ would override `FOO` to `bat` when `load-env` ran. But now, it will see `FOO`+ is already `bar` and leave it.++ This is better behavior under the assumption that a `.env` file is meant to+ specify *defaults* in the case of nothing explicit. When there are explicit+ values in the environment, it's most likely that our user indeed wants them+ respected. ## [v0.2.0.2](https://github.com/pbrisbin/load-env/compare/v0.2.0.1...v0.2.0.2)
load-env.cabal view
@@ -1,11 +1,13 @@--- This file has been generated from package.yaml by hpack version 0.28.2.+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.31.1. -- -- see: https://github.com/sol/hpack ----- hash: c39eb0ad2f6482017271ab93ef8f962a61068806291ebaa457c3fa6f3dd34055+-- hash: cb9cf61d4827d4897e0bb920005dafac784c77c15cae85f88bb288ea97ef2613 name: load-env-version: 0.2.0.2+version: 0.2.1.0 synopsis: Load environment variables from a file. description: Parse a .env file and load any declared variables into the current process's environment. This allows for a .env file to specify development-friendly defaults for configuration values normally set in the deployment environment. category: Configuration@@ -16,16 +18,20 @@ license: BSD3 license-file: LICENSE build-type: Simple-cabal-version: >= 1.10 extra-source-files:- CHANGELOG.md README.md+ CHANGELOG.md source-repository head type: git location: https://github.com/pbrisbin/load-env library+ exposed-modules:+ LoadEnv+ LoadEnv.Parse+ other-modules:+ Paths_load_env hs-source-dirs: src ghc-options: -Wall@@ -34,11 +40,6 @@ , directory , filepath , parsec- exposed-modules:- LoadEnv- LoadEnv.Parse- other-modules:- Paths_load_env default-language: Haskell2010 test-suite doctest@@ -57,6 +58,10 @@ test-suite spec type: exitcode-stdio-1.0 main-is: Spec.hs+ other-modules:+ LoadEnv.ParseSpec+ LoadEnvSpec+ Paths_load_env hs-source-dirs: test ghc-options: -Wall@@ -67,8 +72,4 @@ , load-env , parsec , temporary- other-modules:- LoadEnv.ParseSpec- LoadEnvSpec- Paths_load_env default-language: Haskell2010
src/LoadEnv.hs view
@@ -23,14 +23,15 @@ , loadEnvFromAbsolute ) where -import Control.Monad ((<=<))+import Control.Monad (unless, (<=<)) import Data.Bool (bool) import Data.Foldable (for_, traverse_) import Data.List (inits)+import Data.Maybe (isJust) import LoadEnv.Parse import System.Directory (doesFileExist, findFile, getCurrentDirectory, makeAbsolute)-import System.Environment (setEnv)+import System.Environment (lookupEnv, setEnv) import System.FilePath (isRelative, joinPath, splitDirectories) import Text.Parsec.String (parseFromFile) @@ -63,7 +64,12 @@ for_ mFile $ \file -> do result <- parseFromFile parseEnvironment file- either print (traverse_ $ uncurry setEnv) result+ either print (traverse_ $ uncurry defaultEnv) result++defaultEnv :: String -> String -> IO ()+defaultEnv k v = do+ exists <- isJust <$> lookupEnv k+ unless exists $ setEnv k v -- | @'loadEnvFrom'@, but don't traverse up the directory tree loadEnvFromAbsolute :: FilePath -> IO ()
test/LoadEnvSpec.hs view
@@ -25,6 +25,15 @@ mbar `shouldBe` Just "bar" mbat `shouldBe` Just "bat" + it "does not override pre-existing variables" $ do+ writeFile envFile $ unlines ["FOO=bar"]+ setEnv "FOO" "baz"++ loadEnvFrom envFile++ mbar <- lookupEnv "FOO"+ mbar `shouldBe` Just "baz"+ it "does not fail if the file is not present" $ do loadEnvFrom "i-do-not-exist"