Experiment with shelling out to parse files #474
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was part of an experiment I did a while ago in response to #430 to use shell to parse
.env
files. The basics are working, but there is still a lot of work and some difficult problems.Ideally, dotenv woud just be able to
exec("source .env")
, but Ruby doesn't offer a way to execute a command in the current process and then return control back to ruby (and there are some behavioral differences that I don't think we want, which I'll get to in a minute).exec
will replace the current process and then exit once the command is run.spawn
and backticks return a subshell, so any exported variables don't get returned.So this PR adds
dotenv-source
, which is a shell command that just sources a file and then prints theenv
after.ShellParser#source
calls that shell script, and then parse the printed env into a hash and returns just changed values from the currentENV
(and eventually will updateENV
).Biggest problem: conditional assignment
dotenv (by default) does conditional assignment and you can override defaults at runtime. Given this file:
VAR_1=default VAR_2="$VAR_1 and some stuff"
Here is the current behavior:
But shell/bash uses
: ${VAR:="default"}
for conditional assignment, so sourcing this.env
with theShellParser
has different behavior:So we need to find a way to support the current behavior without forcing people to change syntax.
TODO:
VAR=spaces without quotes
)DOTENV_PARSER=shell
at top of file?)cc @graywolf-at-work
cc #422