read-env-var (empty) → 0.1.0.0
raw patch · 5 files changed
+169/−0 lines, 5 filesdep +Globdep +basedep +doctestsetup-changed
Dependencies added: Glob, base, doctest
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- read-env-var.cabal +35/−0
- src/System/ReadEnvVar.hs +62/−0
- test/DocTest.hs +40/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ read-env-var.cabal view
@@ -0,0 +1,35 @@+name: read-env-var+version: 0.1.0.0+synopsis: Functions for safely reading environment variables.+description: Please see README.md+homepage: https://github.com/cdepillabout/read-env-var#readme+license: BSD3+license-file: LICENSE+author: Dennis Gosnell+maintainer: cdep.illabout@gmail.com+copyright: 2016 Dennis Gosnell+category: System+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: System.ReadEnvVar+ build-depends: base >= 4.6 && < 5+ default-language: Haskell2010+ ghc-options: -Wall++test-suite read-env-var-doctest+ type: exitcode-stdio-1.0+ main-is: DocTest.hs+ hs-source-dirs: test+ build-depends: base+ , doctest+ , Glob+ default-language: Haskell2010+ ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N++source-repository head+ type: git+ location: git@github.com:cdepillabout/read-env-var.git
+ src/System/ReadEnvVar.hs view
@@ -0,0 +1,62 @@+{-# LANGUAGE CPP #-}++{-|+Module : System.ReadEnvVar+Copyright : (c) Dennis Gosnell, 2016+License : BSD-style (see LICENSE file)+Maintainer : cdep.illabout@gmail.com+Stability : experimental+Portability : POSIX++This Haskell module exports functions for safely reading environment variables.+-}++module System.ReadEnvVar+ ( readEnvVar+ , readEnvVarDef+ ) where++#if __GLASGOW_HASKELL__ < 710+-- We don't need this import for GHC 7.10 as it exports all required functions+-- from Prelude+import Control.Applicative+#endif++import Data.Maybe (fromMaybe)+import System.Environment (lookupEnv)+import Text.Read (readMaybe)++-- | Lookup a value from an environment variable and read it in with+-- 'readMaybe'. If the environment variable doesn't exist, or it can't be+-- 'read', use the default value.+--+-- >>> import System.Environment (setEnv)+-- >>> setEnv "TEST_ENV_VAR" "1000"+-- >>> readEnvVarDef "TEST_ENV_VAR" 5 :: IO Int+-- 1000+-- >>> readEnvVarDef "THIS_ENV_VAR_WILL_NOT_EXIST" 5 :: IO Int+-- 5+readEnvVarDef :: Read a+ => String -- ^ environment variable to lookup+ -> a -- ^ default value to use if the environment variable+ -- either does not exist, or cannot be 'read'+ -> IO a+readEnvVarDef envVar def = fromMaybe def <$> readEnvVar envVar++-- | Lookup a value from an environment variable and read it in with+-- 'readMaybe'.+--+-- >>> import System.Environment (setEnv)+-- >>> setEnv "TEST_ENV_VAR" "2000"+-- >>> readEnvVar "TEST_ENV_VAR" :: IO (Maybe Int)+-- Just 2000+-- >>> readEnvVar "THIS_ENV_VAR_WILL_NOT_EXIST" :: IO (Maybe Int)+-- Nothing+readEnvVar :: Read a+ => String -- ^ environment variable to lookup+ -> IO (Maybe a)+readEnvVar envVar = do+ maybeEnvVal <- lookupEnv envVar+ case maybeEnvVal of+ Nothing -> return Nothing+ Just envVal -> return $ readMaybe envVal
+ test/DocTest.hs view
@@ -0,0 +1,40 @@++module Main (main) where++import Prelude++import Data.Monoid ((<>))+import System.FilePath.Glob (glob)+import Test.DocTest (doctest)++main :: IO ()+main = glob "src/**/*.hs" >>= doDocTest++doDocTest :: [String] -> IO ()+doDocTest options = doctest $ options <> ghcExtensions++ghcExtensions :: [String]+ghcExtensions =+ [+ -- "-XConstraintKinds"+ -- , "-XDataKinds"+ "-XDeriveDataTypeable"+ , "-XDeriveGeneric"+ -- , "-XEmptyDataDecls"+ , "-XFlexibleContexts"+ -- , "-XFlexibleInstances"+ -- , "-XGADTs"+ -- , "-XGeneralizedNewtypeDeriving"+ -- , "-XInstanceSigs"+ -- , "-XMultiParamTypeClasses"+ -- , "-XNoImplicitPrelude"+ , "-XOverloadedStrings"+ -- , "-XPolyKinds"+ -- , "-XRankNTypes"+ -- , "-XRecordWildCards"+ , "-XScopedTypeVariables"+ -- , "-XStandaloneDeriving"+ -- , "-XTupleSections"+ -- , "-XTypeFamilies"+ -- , "-XTypeOperators"+ ]