Skip to content

Commit

Permalink
Merge pull request #9 from rodrigopinto/rp-load-multiple-files
Browse files Browse the repository at this point in the history
Add ability to load multiple files
  • Loading branch information
gdotdesign authored Feb 7, 2019
2 parents 3416780 + 579f899 commit e330eec
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 65 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Loads `.env` file.

## Installation


Add this to your application's `shard.yml`:

```yaml
Expand All @@ -15,10 +14,10 @@ dependencies:
github: gdotdesign/cr-dotenv
```
## Usage
Your `.env` file:

```
# Comments can be included for context
#
Expand All @@ -30,6 +29,7 @@ ANOTHER_VAR=awesome-value
```
In your application:
```crystal
require "dotenv"
Expand All @@ -42,12 +42,17 @@ Dotenv.load ".env-other"
# If you load env variable from file and
# you want to raise execption in case of
# missing dotenv file, to make this error
# immediately obvious then use the bang
# immediately obvious then use the bang
# verion of the load method:
Dotenv.load!
# or
Dotenv.load! ".env-other"
# Loading multiple files in order,
# and sets the last loaded value for a varibale
Dotenv.load! %w(.env .env.test)
# From IO
Dotenv.load MemoryIO.new("VAR=test")
Expand All @@ -72,6 +77,7 @@ puts ENV["MY_VARIABLE"] # my-value
## Contributors

- [[gdotdesign]](https://github.com/[gdotdesign]) Gusztáv Szikszai - creator, maintainer
- [[bonyiii]](https://github.com/[bonyiii])
- [[bonyiii]](https://github.com/[bonyiii])
- [[kriskova]](https://github.com/kriskova)
- [[neovintage]](https://github.com/[neovintage]) Rimas Silkaitis
- [[rodrigopinto]](https://github.com/[rodrigopinto]) Rodrigo Pinto
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dotenv
version: 0.1.0
version: 0.2.0

authors:
- Gusztáv Szikszai <guszti5@hotmail.com>
Expand Down
152 changes: 98 additions & 54 deletions spec/dotenv_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,102 @@ require "./spec_helper"
Dotenv.verbose = false

describe Dotenv do
context "Non exsisting file." do
it "should print warning" do
Dotenv.load ".some-non-existent-env-file"
describe "#load" do
context "Non existing file." do
it "should print warning" do
Dotenv.load ".some-non-existent-env-file"
end
end
end

context "Exsisting file" do
File.write ".test-env", "VAR=Hello"
context "Existing file" do
File.write ".test-env", "VAR=Hello"

it "should load env" do
Dotenv.load ".test-env"
ENV["VAR"].should eq "Hello"
end
it "should load env" do
Dotenv.load ".test-env"
ENV["VAR"].should eq "Hello"
end

it "should return hash" do
hash = Dotenv.load ".test-env"
hash["VAR"].should eq "Hello"
end

it "should return hash" do
hash = Dotenv.load ".test-env"
hash["VAR"].should eq "Hello"
File.delete ".test-env"
end

File.delete ".test-env"
end
context "Multiple existing file" do
File.write ".test-env", "VAR=Hello"
File.write ".local-env", "VAR=HelloLocal"

it "should load env" do
Dotenv.load %w(.test-env .local-env)
ENV["VAR"].should eq "HelloLocal"
end

context "Comment Lines and Empty Lines" do
File.write ".test-env", "# This is a comment\nVAR=Dude\n\n"
it "should return hash" do
hash = Dotenv.load %w(.test-env .local-env)
hash["VAR"].should eq "HelloLocal"
end

it "should ignore" do
hash = Dotenv.load ".test-env"
hash.should eq({"VAR" => "Dude"})
File.delete ".test-env"
File.delete ".local-env"
end

File.delete ".test-env"
end
context "Comment Lines and Empty Lines" do
File.write ".test-env", "# This is a comment\nVAR=Dude\n\n"

context "Values with special characters" do
File.write ".test-env", "VAR=postgres://foo@localhost:5432/bar?max_pool_size=10"
it "should ignore" do
hash = Dotenv.load ".test-env"
hash.should eq({"VAR" => "Dude"})
end

it "should allow `=` in values" do
hash = Dotenv.load ".test-env"
hash.should eq({"VAR" => "postgres://foo@localhost:5432/bar?max_pool_size=10"})
File.delete ".test-env"
end

File.delete ".test-env"
end
context "Values with special characters" do
File.write ".test-env", "VAR=postgres://foo@localhost:5432/bar?max_pool_size=10"

context "From IO" do
it "should load env" do
io = IO::Memory.new "VAR2=test\nVAR3=other"
hash = Dotenv.load io
hash["VAR2"].should eq "test"
hash["VAR3"].should eq "other"
ENV["VAR2"].should eq "test"
ENV["VAR3"].should eq "other"
it "should allow `=` in values" do
hash = Dotenv.load ".test-env"
hash.should eq({"VAR" => "postgres://foo@localhost:5432/bar?max_pool_size=10"})
end

File.delete ".test-env"
end

context "From IO" do
it "should load env" do
io = IO::Memory.new "VAR2=test\nVAR3=other"
hash = Dotenv.load io
hash["VAR2"].should eq "test"
hash["VAR3"].should eq "other"
ENV["VAR2"].should eq "test"
ENV["VAR3"].should eq "other"
end
end
end

context "From Hash" do
it "should load env" do
hash = Dotenv.load({"test" => "test"})
hash["test"].should eq "test"
ENV["test"].should eq "test"
context "From Hash" do
it "should load env" do
hash = Dotenv.load({"test" => "test"})
hash["test"].should eq "test"
ENV["test"].should eq "test"
end
end
end

context "Invalid file" do
File.write ".test-env", "VAR1=Hello\nHELLO:asd"
context "Invalid file" do
File.write ".test-env", "VAR1=Hello\nHELLO:asd"

it "should read valid lines only" do
Dotenv.load ".test-env"
ENV["VAR1"].should eq "Hello"
it "should read valid lines only" do
Dotenv.load ".test-env"
ENV["VAR1"].should eq "Hello"

expect_raises do
ENV["HELLO"]
expect_raises(KeyError) do
ENV["HELLO"]
end
end
end

File.delete ".test-env"
File.delete ".test-env"
end
end

describe "#load!" do
Expand All @@ -88,9 +108,15 @@ describe Dotenv do
Dotenv.load! ".test-env"
end
end

it "should raise FileMissing error" do
expect_raises(Dotenv::FileMissing) do
Dotenv.load! %w(.test-env .local-env)
end
end
end

context "Exsisting file" do
context "Existing file" do
File.write ".test-env", "VAR=Hello"

it "should load env" do
Expand All @@ -106,6 +132,24 @@ describe Dotenv do
File.delete ".test-env"
end

context "Multiple existing file" do
File.write ".test-env", "VAR=Hello"
File.write ".local-env", "VAR=HelloLocal"

it "should load env" do
Dotenv.load! %w(.test-env .local-env)
ENV["VAR"].should eq "HelloLocal"
end

it "should return hash" do
hash = Dotenv.load! %w(.test-env .local-env)
hash["VAR"].should eq "HelloLocal"
end

File.delete ".test-env"
File.delete ".local-env"
end

context "From IO" do
it "should load env" do
io = IO::Memory.new "VAR2=test\nVAR3=other"
Expand Down
26 changes: 21 additions & 5 deletions src/dotenv.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ module Dotenv
@@verbose = value
end

def load(path = ".env") : Hash(String, String)
load File.open(File.expand_path(path))
def load(filename = ".env") : Hash(String, String)
load open(filename)
rescue ex
log "DOTENV - Could not open file: #{path}"
log "DOTENV - Could not open file: #{filename}"
{} of String => String
end

def load(filenames : Array(String)) : Hash(String, String)
filenames.each_with_object({} of String => String) do |filename, hash|
hash.merge!(load(filename))
end
end

def load(io : IO) : Hash(String, String)
hash = {} of String => String
io.each_line do |line|
Expand All @@ -35,12 +41,18 @@ module Dotenv
ENV
end

def load!(path = ".env") : Hash(String, String)
load File.open(File.expand_path(path))
def load!(filename = ".env") : Hash(String, String)
load open(filename)
rescue ex
raise FileMissing.new("Missing file!")
end

def load!(filenames : Array(String)) : Hash(String, String)
filenames.each_with_object({} of String => String) do |filename, hash|
hash.merge!(load!(filename))
end
end

def load!(io : IO) : Hash(String, String)
load(io)
end
Expand All @@ -61,4 +73,8 @@ module Dotenv
private def log(message : String)
puts message if @@verbose
end

private def open(filename : String) : File
File.open(File.expand_path(filename))
end
end
2 changes: 1 addition & 1 deletion src/dotenv/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Dotenv
VERSION = "0.1.0"
VERSION = "0.2.0"
end

0 comments on commit e330eec

Please sign in to comment.