load-env (empty) → 0.0.1
raw patch · 6 files changed
+178/−0 lines, 6 filesdep +HUnitdep +basedep +directorysetup-changed
Dependencies added: HUnit, base, directory, hspec, load-env, parsec
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- load-env.cabal +40/−0
- src/LoadEnv.hs +38/−0
- src/LoadEnv/Parse.hs +67/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Pat Brisbin++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 Pat Brisbin 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
+ load-env.cabal view
@@ -0,0 +1,40 @@+Name: load-env+Version: 0.0.1+Author: Pat Brisbin <pbrisbin@gmail.com>+Maintainer: Pat Brisbin <pbrisbin@gmail.com>+License: BSD3+License-File: LICENSE+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.+Cabal-Version: >= 1.10+Build-Type: Simple++Library+ Default-Language: Haskell2010+ HS-Source-Dirs: src+ GHC-Options: -Wall+ Exposed-Modules: LoadEnv+ , LoadEnv.Parse+ Build-Depends: base >= 4.6.0 && < 5+ , parsec++Test-Suite spec+ Type: exitcode-stdio-1.0+ Default-Language: Haskell2010+ Hs-Source-Dirs: test+ Ghc-Options: -Wall+ Main-Is: Spec.hs+ Build-Depends: base+ , load-env+ , directory+ , hspec+ , HUnit+ , parsec++Source-Repository head+ Type: git+ Location: https://github.com/pbrisbin/load-env
+ src/LoadEnv.hs view
@@ -0,0 +1,38 @@+module LoadEnv+ ( loadEnv+ , loadEnvFrom+ ) where+++import System.Environment (setEnv)+import Text.Parsec.String (parseFromFile)++import LoadEnv.Parse++-- |+--+-- Parse @./.env@ for variable declariations. Set those variables in the+-- currently running process's environment. Variables can be declared in the+-- following form:+--+-- > FOO=bar+-- > FOO="bar"+-- > FOO='bar'+--+-- Declarations may optionally be preceded by @export@, which will be ignored.+-- Lines beginning with @#@ and blank lines are ignored. Trailing whitespace is+-- ignored. Quotes inside quoted values or spaces in unquoted values must be+-- escaped with a backlash. All else will result in a parse error being printed+-- to @stdout@.+--+-- If you wish to specify your own file, use @'loadEnvFrom'@. If you wish to+-- pass your own string or work with the parse result directly, use the+-- lower-level functions available in @"System.Env.Parse"@.+--+loadEnv :: IO ()+loadEnv = loadEnvFrom ".env"++loadEnvFrom :: FilePath -> IO ()+loadEnvFrom fp =+ parseFromFile parseEnvironment fp >>= either+ (putStrLn . show) (mapM_ $ uncurry setEnv)
+ src/LoadEnv/Parse.hs view
@@ -0,0 +1,67 @@+module LoadEnv.Parse+ ( Environment+ , Variable+ , parseEnvironment+ , parseVariable+ ) where++import Control.Applicative ((<$>), (<*>))+import Data.Maybe (catMaybes)++import Text.Parsec+import Text.Parsec.String++type Environment = [Variable]+type Variable = (String, String)++parseEnvironment :: Parser Environment+parseEnvironment = fmap catMaybes $ many1 parseLine++parseLine :: Parser (Maybe Variable)+parseLine = try (fmap Just $ parseVariable) <|> ignoreLine++ignoreLine :: Parser (Maybe Variable)+ignoreLine = (commentLine <|> blankLine) >> return Nothing++commentLine :: Parser ()+commentLine = do+ _ <- spaces+ _ <- char '#'+ _ <- manyTill anyToken (char '\n')++ return ()++blankLine :: Parser ()+blankLine = many1 space >> return ()++parseVariable :: Parser Variable+parseVariable = (,) <$> identifier <*> value++identifier :: Parser String+identifier = do+ optional $ between spaces spaces $ string "export"++ i <- many1 letter+ _ <- char '='++ return i++value :: Parser String+value = do+ v <- quotedValue <|> unquotedValue <|> return ""+ _ <- many $ oneOf " \t"+ _ <- char '\n'++ return v++quotedValue :: Parser String+quotedValue = do+ q <- oneOf "'\""++ manyTill (try (escaped q) <|> anyToken) (char q)++unquotedValue :: Parser String+unquotedValue = many1 $ try (escaped ' ') <|> (noneOf "\"' \n")++escaped :: Char -> Parser Char+escaped c = string ("\\" ++ [c]) >> return c
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}