Skip to content

Commit

Permalink
Fix: parse exists ENV variable value if included in ENV value. (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
zw963 authored Mar 20, 2023
1 parent c7fbb17 commit c1d3830
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
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

0 comments on commit c1d3830

Please sign in to comment.