include-env 0.4.0.0 → 0.5.0.0
raw patch · 2 files changed
+49/−2 lines, 2 filesdep +containersdep +th-lift-instancesPVP ok
version bump matches the API change (PVP)
Dependencies added: containers, th-lift-instances
API changes (from Hackage documentation)
+ IncludeEnv.TH: includeEnvMap :: Foldable t => t String -> Q Exp
Files
- include-env.cabal +3/−1
- src/IncludeEnv/TH.hs +46/−1
include-env.cabal view
@@ -1,5 +1,5 @@ name: include-env-version: 0.4.0.0+version: 0.5.0.0 synopsis: Include the value of an environment variable at compile time description: Embed secrets (e.g. API keys) inside production artifacts at compile time. homepage: https://github.com/unfoldml/include-env@@ -21,7 +21,9 @@ hs-source-dirs: src exposed-modules: IncludeEnv.TH build-depends: base >= 4.7 && < 5+ , containers , template-haskell >= 2.14+ , th-lift-instances source-repository head type: git
src/IncludeEnv/TH.hs view
@@ -6,8 +6,14 @@ == Rationale Users might want to embed secrets (e.g. API keys, database connection strings) inside production artifacts without checking these into the repository. -== Example+== Examples +NB : all library functions require the `TemplateHaskell` language extension.++=== Include a single variable++In this case, the name of the user's current shell) :+ @ import IncludeEnv.TH (includeEnv) @@ -18,19 +24,40 @@ main = putStrLn $ unwords ["your current shell :", shl] @ +=== Include a group of variables as a name-value map++@+import IncludeEnv.TH (includeEnvMap)++env = $(`includeEnvMap` [\"TERM\", \"USER\"])+@++@+>>> env+fromList [(\"TERM\","dumb"),(\"USER\","marco")]+@+ -} module IncludeEnv.TH ( includeEnv , includeEnvLenient , includeEnvMaybe+ -- * containers+ , includeEnvMap ) where +import Control.Monad (foldM)+ import System.Environment (lookupEnv) +-- containers+import qualified Data.Map.Strict as M (Map, lookup, insert, fromList) -- template-haskell import Language.Haskell.TH (runIO, runQ) import Language.Haskell.TH.Syntax (Q, Exp(..), Dec(..), Pat(..), Name, mkName, Body(..), Lit(..), reportWarning) import Language.Haskell.TH.Lib (valD)+-- th-lift-instances+import Instances.TH.Lift -- | Include the value of an environment variable at compile time.@@ -83,3 +110,21 @@ includeEnvMaybe e = do mstr <- runIO $ lookupEnv e [| mstr |]++-- | Lookup a number of environment variables and populate a 'M.Map' with the result+--+-- NB: if a variable name cannot be found, the corresponding entry will be missing+--+-- @since 0.5.0.0+includeEnvMap :: Foldable t =>+ t String -- ^ names of environment variable to be looked up+ -> Q Exp+includeEnvMap es = do+ mm <- foldM insf mempty es+ [| mm |]+ where+ insf acc k = do+ mv <- runIO $ lookupEnv k+ case mv of+ Nothing -> pure acc+ Just v -> pure $ M.insert k v acc