load-env-0.1.1/0000755000000000000000000000000012546313505011410 5ustar0000000000000000load-env-0.1.1/load-env.cabal0000644000000000000000000000276112546313505014107 0ustar0000000000000000name: load-env version: 0.1.1 author: Pat Brisbin maintainer: Pat Brisbin license: BSD3 license-file: LICENSE synopsis: Load environment variables from a file. category: Configuration 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.7.0 && < 5 , directory , 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 load-env-0.1.1/Setup.hs0000644000000000000000000000005612546313505013045 0ustar0000000000000000import Distribution.Simple main = defaultMain load-env-0.1.1/LICENSE0000644000000000000000000000276012546313505012422 0ustar0000000000000000Copyright (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. load-env-0.1.1/test/0000755000000000000000000000000012546313505012367 5ustar0000000000000000load-env-0.1.1/test/Spec.hs0000644000000000000000000000005412546313505013614 0ustar0000000000000000{-# OPTIONS_GHC -F -pgmF hspec-discover #-} load-env-0.1.1/src/0000755000000000000000000000000012546313505012177 5ustar0000000000000000load-env-0.1.1/src/LoadEnv.hs0000644000000000000000000000216412546313505014066 0ustar0000000000000000module LoadEnv ( loadEnv , loadEnvFrom ) where import Control.Monad (when) import System.Directory (doesFileExist) 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. -- Trailing whitespace is ignored. Quotes inside quoted values or spaces in -- unquoted values must be escaped with a backlash. -- -- Invalid lines are silently ignored. -- -- 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 @"LoadEnv.Parse"@. -- loadEnv :: IO () loadEnv = loadEnvFrom ".env" loadEnvFrom :: FilePath -> IO () loadEnvFrom fp = do e <- doesFileExist fp when e $ parseFromFile parseEnvironment fp >>= either print (mapM_ $ uncurry setEnv) load-env-0.1.1/src/LoadEnv/0000755000000000000000000000000012546313505013527 5ustar0000000000000000load-env-0.1.1/src/LoadEnv/Parse.hs0000644000000000000000000000346412546313505015144 0ustar0000000000000000{-# LANGUAGE CPP #-} module LoadEnv.Parse ( Environment , Variable , parseEnvironment , parseVariable ) where #if __GLASGOW_HASKELL__ < 710 import Control.Applicative ((<$>)) #endif import Control.Monad (void) import Data.Maybe (catMaybes) import Text.Parsec import Text.Parsec.String type Environment = [Variable] type Variable = (String, String) parseEnvironment :: Parser Environment parseEnvironment = catMaybes <$> many parseLine parseLine :: Parser (Maybe Variable) parseLine = possibly parseVariable possibly :: Parser a -> Parser (Maybe a) possibly p = try (fmap Just p) <|> ignored where ignored = do void $ manyTill anyToken newline return Nothing parseVariable :: Parser Variable parseVariable = do optional $ between spaces spaces $ string "export" i <- identifier void $ char '=' v <- value void $ many $ oneOf " \t" void $ newline return (i, v) -- Environment variable names used by the utilities in the Shell and Utilities -- volume of IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, -- and the '_' (underscore) from the characters defined in Portable Character -- Set and do not begin with a digit. -- -- -- identifier :: Parser String identifier = do x <- upper <|> underscore ys <- many $ upper <|> digit <|> underscore return (x:ys) where underscore = char '_' value :: Parser String value = quotedValue <|> unquotedValue <|> return "" 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