Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to raise error, if .env file does not exist #1 #2

Merged
merged 6 commits into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Envy.load ".env.development", ".env.redis"

# To parse a `.env` file, returns `Hash(String, String)`. Raises if the file does not exist.
Envy.parse ".env"

# To raise an exception if the .env file does not exist, you can append a block to Envy#load or Envy#load!.
Envy.load! do
{ raise_exception: true }
end
```

## `.env` files
Expand Down Expand Up @@ -60,7 +65,7 @@ EQUAL_SIGNS=are=allowed=
NaMe=value

# Empty value become empty string
EMPTY=
EMPTY=
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ version: 0.1.0
authors:
- Michael Petö <michael@petoe.me>

crystal: 0.23.1
crystal: 0.24.1

license: MIT
38 changes: 38 additions & 0 deletions spec/envy_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@ describe Envy do
ENV["ANOTHER_LINE"].should eq("some_value")
end

context ".env file exists" do
it "load .env file with block" do
ENV["A_KEY"] = "one value"
Envy.load "spec/.env" do
{ raise_exception: true }
end
ENV["A_KEY"].should eq("one value")
ENV["ANOTHER_LINE"].should eq("some_value")
end

it "load! .env file with block" do
ENV["A_KEY"] = "one value"
Envy.load! "spec/.env" do
{ raise_exception: true }
end
ENV["A_KEY"].should eq("value")
ENV["ANOTHER_LINE"].should eq("some_value")
end
end

context ".env file does not exist" do
it "load .env file with block" do
expect_raises(Envy::InvalidFileException) do
Envy.load "spec/.env_file_that_does_not_exist" do
{ raise_exception: true }
end
end
end

it "load! .env file with block" do
expect_raises(Envy::InvalidFileException) do
Envy.load! "spec/.env_file_that_does_not_exist" do
{ raise_exception: true }
end
end
end
end

it "parse" do
env_hash = {"CRYSTAL_ENV" => "development",
"A_KEY" => "value",
Expand Down
15 changes: 13 additions & 2 deletions src/envy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@ module Envy
extend self
DEFAULT_ENV_FILENAME = ".env"

class InvalidFileException < Exception; end

{% for name, comp in {load: :"||=", load!: :"="} %}
def {{name.id}}(*filename)
filename.each do |file|
env_vars = parse file
env_vars = parse(file)
{{name.id}} env_vars
end
{{name.id}} parse(DEFAULT_ENV_FILENAME) if filename.size.zero?
end

def {{name.id}}(*filename, &block)
filename.each do |file|
env_vars = parse(file, yield)
{{name.id}} env_vars
end
{{name.id}} parse(DEFAULT_ENV_FILENAME, yield) if filename.size.zero?
end

def {{name.id}}(hash : Hash(String, String))
hash.each do |key, value|
ENV[key] {{comp.id}} value
end
end
{% end %}

def parse(filename : String) : Hash(String, String)
def parse(filename : String, option = {} of Symbol => Bool) : Hash(String, String)
env_vars = {} of String => String
File.each_line File.expand_path(filename) do |line|
line = line.strip
Expand All @@ -33,6 +43,7 @@ module Envy
end
env_vars
rescue
raise InvalidFileException.new("ENVY - Failed to load #{filename}") if option[:raise_exception]?
puts "ENVY - Failed to load #{filename}"
{} of String => String
end
Expand Down