Skip to content

Commit 0f49e3e

Browse files
committed
First
0 parents  commit 0f49e3e

File tree

12 files changed

+477
-0
lines changed

12 files changed

+477
-0
lines changed

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: haskell
2+
ghc:
3+
- 7.4
4+
- 7.6
5+
- 7.8

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2015 Justin Leitgeb
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included
12+
in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Dotenv files for Haskell
2+
3+
In most applications,
4+
[configuration should be separated from code](http://12factor.net/config). While
5+
it usually works well to keep configuration in the environment, there
6+
are cases where you may want to store configuration in a file outside
7+
of version control.
8+
9+
"Dotenv" files have become popular for storing configuration,
10+
especially in development and test environments. In
11+
[Ruby](https://github.com/bkeepers/dotenv),
12+
[Python](https://github.com/theskumar/python-dotenv) and
13+
[Javascript](https://www.npmjs.com/package/dotenv) there are libraries
14+
to facilitate loading of configuration options from configuration
15+
files. This library loads configuration to environment variables for
16+
programs written in Haskell.
17+
18+
## Installation
19+
20+
In most cases you will just add `dotenv` to your cabal file. You can
21+
also install the library and executable by invoking `cabal install dotenv`.
22+
23+
## Usage
24+
25+
Set configuration variables in a file following the format below:
26+
27+
```
28+
S3_BUCKET=YOURS3BUCKET
29+
SECRET_KEY=YOURSECRETKEYGOESHERE
30+
```
31+
32+
Then, calling Dotenv.load from your Haskell program reads the above
33+
settings into the environment.:
34+
35+
```haskell
36+
import Configuration.Dotenv
37+
Dotenv.loadFile False "/path/to/your/file"
38+
```
39+
40+
## Configuration
41+
42+
The first argument to `loadFile` specifies whether you want to
43+
override system settings. `False` means Dotenv will respect
44+
already-defined variables, and `True` means Dotenv will overwrite
45+
already-defined variables.
46+
47+
## Advanced Dotenv File Syntax
48+
49+
You can add comments to your Dotenv file, on separate lines or after
50+
values. Values can be wrapped in single or double quotes. Multi-line
51+
values can be specified by wrapping the value in double-quotes, and
52+
using the "\n" character to represent newlines.
53+
54+
The [spec file](spec/Configuration/Dotenv/ParseSpec.hs) is the best
55+
place to understand the nuances of Dotenv file parsing.
56+
57+
## Command-Line Usage
58+
59+
You can call dotenv from the command line in order to load settings
60+
from one or more dotenv file before invoking an executable:
61+
62+
```
63+
dotenv -f mydotenvfile myprogram
64+
```
65+
66+
Hint: The `env` program in most Unix-like environments prints out the
67+
current environment settings. By invoking the program `env` in place
68+
of `myprogram` above you can see what the environment will look like
69+
after evaluating multiple Dotenv files.
70+
71+
## Author
72+
73+
Justin Leitgeb
74+
75+
## License
76+
77+
MIT
78+
79+
## Copyright
80+
81+
(C) 2015 Stack Builders Inc.

Setup.hs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

dotenv.cabal

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: dotenv
2+
version: 0.1.0.0
3+
synopsis: Loads environment variables from `.env`
4+
description:
5+
.
6+
This project is modelled after the Ruby and Python packages for
7+
loading files from a dotfile. It can be used as a standalone program,
8+
or embedded inside of web servers like Yesod and Snap.
9+
license: MIT
10+
license-file: LICENSE
11+
author: Justin Leitgeb
12+
maintainer: [email protected]
13+
copyright: 2015 Stack Builders Inc.
14+
category: Configuration
15+
build-type: Simple
16+
-- extra-source-files:
17+
cabal-version: >=1.10
18+
19+
executable dotenv
20+
main-is: Main.hs
21+
-- other-modules:
22+
-- other-extensions:
23+
build-depends: base >=4.5 && <4.8
24+
, optparse-applicative >=0.11 && <0.12
25+
, parsec >= 3.1.0 && <= 3.2
26+
, process
27+
28+
hs-source-dirs: src
29+
default-language: Haskell2010
30+
31+
library
32+
exposed-modules: Configuration.Dotenv.Parse
33+
, Configuration.Dotenv
34+
35+
build-depends: base >=4.5 && <4.8
36+
, parsec >= 3.1.0 && <= 3.2
37+
38+
hs-source-dirs: src
39+
default-language: Haskell2010
40+
ghc-options: -Wall
41+
42+
43+
test-suite dotenv-test
44+
type: exitcode-stdio-1.0
45+
hs-source-dirs: spec, src
46+
main-is: Spec.hs
47+
build-depends: base >=4.5 && <4.8
48+
, parsec >= 3.1.0 && <= 3.2
49+
50+
, hspec
51+
52+
default-language: Haskell2010
53+
ghc-options: -Wall
54+
55+
source-repository head
56+
type: git
57+
location: https://github.com/stackbuilders/dotenv-hs
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{-# OPTIONS_GHC -fno-warn-orphans #-}
2+
3+
module Configuration.Dotenv.ParseSpec (spec) where
4+
5+
import Configuration.Dotenv.Parse (configParser)
6+
7+
import Test.Hspec (it, describe, shouldBe, Spec)
8+
9+
import Text.Parsec (ParseError, parse)
10+
import Text.ParserCombinators.Parsec.Error(errorMessages)
11+
12+
13+
spec :: Spec
14+
spec = describe "parse" $ do
15+
it "parses unquoted values" $
16+
parseConfig "FOO=bar" `shouldBe` Right [("FOO", "bar")]
17+
18+
it "parses values with spaces around equal signs" $ do
19+
parseConfig "FOO =bar" `shouldBe` Right [("FOO", "bar")]
20+
parseConfig "FOO= bar" `shouldBe` Right [("FOO", "bar")]
21+
parseConfig "FOO = bar" `shouldBe` Right [("FOO", "bar")]
22+
23+
it "parses double-quoted values" $
24+
parseConfig "FOO=\"bar\"" `shouldBe` Right [("FOO", "bar")]
25+
26+
it "parses single-quoted values" $
27+
parseConfig "FOO='bar'" `shouldBe` Right [("FOO", "bar")]
28+
29+
it "parses escaped double quotes" $
30+
parseConfig "FOO=\"escaped\\\"bar\"" `shouldBe` Right [("FOO", "escaped\"bar")]
31+
32+
it "parses empty values" $
33+
parseConfig "FOO=" `shouldBe` Right [("FOO", "")]
34+
35+
it "does not parse if line format is incorrect" $
36+
isLeft (parseConfig "lol$wut") `shouldBe` True
37+
38+
it "expands newlines in quoted strings" $
39+
parseConfig "FOO=\"bar\nbaz\"" `shouldBe` Right [("FOO", "bar\nbaz")]
40+
41+
it "parses variables with '.' in the name" $
42+
parseConfig "FOO.BAR=foobar" `shouldBe` Right [("FOO.BAR", "foobar")]
43+
44+
it "strips unquoted values" $
45+
parseConfig "foo=bar " `shouldBe` Right [("foo", "bar")]
46+
47+
it "ignores empty lines" $
48+
parseConfig "\n \t \nfoo=bar\n \nfizz=buzz" `shouldBe`
49+
Right [("foo", "bar"), ("fizz", "buzz")]
50+
51+
it "ignores inline comments" $
52+
parseConfig "FOO=bar # this is foo" `shouldBe` Right [("FOO", "bar")]
53+
54+
it "allows # in quoted values" $
55+
parseConfig "foo=\"bar#baz\" # comment" `shouldBe`
56+
Right [("foo", "bar#baz")]
57+
58+
it "ignores comment lines" $
59+
parseConfig "\n\n\n # HERE GOES FOO \nfoo=bar" `shouldBe`
60+
Right [("foo", "bar")]
61+
62+
63+
isLeft :: Either a b -> Bool
64+
isLeft ( Left _ ) = True
65+
isLeft _ = False
66+
67+
68+
instance Eq ParseError where
69+
a == b = errorMessages a == errorMessages b
70+
71+
parseConfig :: String -> Either ParseError [(String, String)]
72+
parseConfig = parse configParser "(unknown)"

spec/Configuration/DotenvSpec.hs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module Configuration.DotenvSpec (spec) where
2+
3+
import Configuration.Dotenv (load, loadFile)
4+
5+
import Test.Hspec (it, describe, shouldBe, Spec)
6+
7+
import System.Environment (lookupEnv, setEnv, unsetEnv)
8+
9+
spec :: Spec
10+
spec = do
11+
describe "load" $ do
12+
it "loads the given list of configuration options to the environment" $ do
13+
beforeSet <- lookupEnv "foo"
14+
beforeSet `shouldBe` Nothing
15+
16+
load False [("foo", "bar")]
17+
18+
afterSet <- lookupEnv "foo"
19+
afterSet `shouldBe` Just "bar"
20+
21+
unsetEnv "foo" -- unset tested vars to clean test state
22+
23+
it "preserves existing settings when overload is false" $ do
24+
setEnv "foo" "preset"
25+
26+
load False [("foo", "new setting")]
27+
28+
afterSet <- lookupEnv "foo"
29+
afterSet `shouldBe` Just "preset"
30+
31+
unsetEnv "foo" -- unset tested vars to clean test state
32+
33+
it "overrides existing settings when overload is true" $ do
34+
setEnv "foo" "preset"
35+
36+
load True [("foo", "new setting")]
37+
38+
afterSet <- lookupEnv "foo"
39+
afterSet `shouldBe` Just "new setting"
40+
41+
unsetEnv "foo" -- unset tested vars to clean test state
42+
43+
describe "loadFile" $ do
44+
it "loads the configuration options to the environment from a file" $ do
45+
beforeSet <- lookupEnv "DOTENV"
46+
beforeSet `shouldBe` Nothing
47+
48+
loadFile False "spec/fixtures/.dotenv"
49+
50+
afterSet <- lookupEnv "DOTENV"
51+
afterSet `shouldBe` Just "true"
52+
53+
unsetEnv "DOTENV"
54+
55+
it "respects predefined settings when overload is false" $ do
56+
setEnv "DOTENV" "preset"
57+
58+
loadFile False "spec/fixtures/.dotenv"
59+
60+
afterSet <- lookupEnv "DOTENV"
61+
afterSet `shouldBe` Just "preset"
62+
63+
unsetEnv "DOTENV"
64+
65+
it "overrides predefined settings when overload is true" $ do
66+
setEnv "DOTENV" "preset"
67+
68+
loadFile True "spec/fixtures/.dotenv"
69+
70+
afterSet <- lookupEnv "DOTENV"
71+
afterSet `shouldBe` Just "true"
72+
73+
unsetEnv "DOTENV"

spec/Spec.hs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

spec/fixtures/.dotenv

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DOTENV=true

src/Configuration/Dotenv.hs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module Configuration.Dotenv (load, loadFile) where
2+
3+
import System.Environment (lookupEnv, setEnv)
4+
5+
import Configuration.Dotenv.Parse (configParser)
6+
7+
import Text.Parsec (parse)
8+
9+
-- | Loads the given list of options into the environment.
10+
-- Any existing settings will take priority.
11+
load ::
12+
Bool -- ^ Override existing settings?
13+
14+
-- ^ A list of tuples in the form (key, value) to be set in the environment
15+
-> [(String, String)]
16+
-> IO ()
17+
load override = mapM_ (applySetting override)
18+
19+
-- | Loads the options in the given file to the environment.
20+
loadFile ::
21+
Bool -- ^ Override existing settings?
22+
-> FilePath -- ^ A file containing options to load into the environment
23+
-> IO ()
24+
loadFile override f = do
25+
contents <- readFile f
26+
27+
case parse configParser f contents of
28+
Left e -> error $ "Failed to read file" ++ show e
29+
Right options -> load override options
30+
31+
32+
applySetting :: Bool -> (String, String) -> IO ()
33+
applySetting override (key, value) =
34+
if override then
35+
setEnv key value
36+
37+
else do
38+
res <- lookupEnv key
39+
40+
case res of
41+
Nothing -> setEnv key value
42+
Just _ -> return ()

0 commit comments

Comments
 (0)