include-env 0.1.3.0 → 0.2.0.0
raw patch · 2 files changed
+21/−2 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ IncludeEnv.TH: includeEnvLenient :: String -> String -> Q [Dec]
Files
- include-env.cabal +1/−1
- src/IncludeEnv/TH.hs +20/−1
include-env.cabal view
@@ -1,5 +1,5 @@ name: include-env-version: 0.1.3.0+version: 0.2.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
src/IncludeEnv/TH.hs view
@@ -19,7 +19,7 @@ @ -}-module IncludeEnv.TH (includeEnv) where+module IncludeEnv.TH (includeEnv, includeEnvLenient) where import System.Environment (lookupEnv) @@ -49,5 +49,24 @@ qpat = VarP (mkName n) qbody = NormalB (LitE (StringL x)) +-- | Like 'includeEnv' but only prints a warning if the environment variable cannot be found.+--+-- NB : If the lookup fails, the declared value will contain an empty string.+includeEnvLenient :: String -- ^ name of environment variable to be looked up+ -> String -- ^ name of new value+ -> Q [Dec]+includeEnvLenient e varname = do+ mstr <- runIO $ lookupEnv e+ case mstr of+ Just str -> decl varname str+ Nothing -> do+ runIO $ putStrLn $ unwords ["*** WARNING : Cannot find variable", e, "in the environment."]+ decl varname ""+ where+ decl :: String -> String -> Q [Dec]+ decl n x = pure [dq] where+ dq = ValD qpat qbody []+ qpat = VarP (mkName n)+ qbody = NormalB (LitE (StringL x))