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

Fix: parse exists ENV variable value if included in ENV value. #31

Merged
merged 1 commit into from
Mar 20, 2023
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ Create your "env" file. Name it whatever you want. Most common is `.env`.
The file is created with key/value pairs separated by `=`.

```text
APP_NAME=my_app
LUCKY_ENV=development
DEV_PORT=3002
DB_NAME=${APP_NAME}_${LUCKY_ENV}
```

### Crystal code
Expand Down
3 changes: 3 additions & 0 deletions spec/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ describe LuckyEnv::Parser do
data["SECRET_KEY_BASE"].should eq "j5I0VrpzT1Of7dhCA="
data["ASSET_HOST"].should eq "https://luckyframework.org"
data["ENV_WITH_SPACE"].should eq "start_end"
data["APP_NAME"].should eq "my_app"
data["DB_NAME"].should eq "my_app_development"
data["LITERAL"].should eq "${NOT_EXISTS_ENV}"
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion spec/support/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ LUCKY_ENV=development
DEV_PORT=3500
SECRET_KEY_BASE=j5I0VrpzT1Of7dhCA=
ASSET_HOST="https://luckyframework.org"
ENV_WITH_SPACE= start_end
ENV_WITH_SPACE= start_end
APP_NAME=my_app
DB_NAME=${APP_NAME}_${LUCKY_ENV}
LITERAL=${NOT_EXISTS_ENV}
14 changes: 13 additions & 1 deletion src/lucky_env/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,19 @@ module LuckyEnv
hash[key] = value
end

hash
keys = hash.keys

hash.transform_values do |value|
keys.each do |key|
if value =~ /\$\{#{key}\}/
value = value.sub("${#{key}}", hash[key])
else
value
end
end

value
end
else
raise MissingFileError.new <<-ERROR
The file #{file_path} could not be found.
Expand Down