Skip to content

Commit

Permalink
Optionally ignore missing files from CLI
Browse files Browse the repository at this point in the history
This is particularly useful for wrapper scripts (like a `bin/dev`
https://stevenharman.net/bin-dev-for-heroku-local) trying to mimic
`dotenv-rails`'s ordered loading of multiple files.
  • Loading branch information
stevenharman committed May 26, 2023
1 parent 9bb5db9 commit 075e043
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,18 @@ Alternatively, you can use the `dotenv` executable to launch your application:
$ dotenv ./script.rb
```

The `dotenv` executable also accepts a single flag, `-f`. Its value should be a comma-separated list of configuration files, in the order of most important to least. All of the files must exist. There _must_ be a space between the flag and its value.
The `dotenv` executable also accepts the flag `-f`. Its value should be a comma-separated list of configuration files, in the order of most important to least. All of the files must exist. There _must_ be a space between the flag and its value.

```console
$ dotenv -f ".env.local,.env" ./script.rb
```

The `dotenv` executable can optionally ignore missing files with the `-i` or `--ignore` flag. For example, if the `.env.local` file does not exist, the following will ignore the missing file and only load the `.env` file.

```console
$ dotenv -i -f ".env.local,.env" ./script.rb
```

To ensure `.env` is loaded in rake, load the tasks:

```ruby
Expand Down
15 changes: 10 additions & 5 deletions lib/dotenv/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CLI < OptionParser
def initialize(argv = [])
@argv = argv.dup
@filenames = []
@ignore = false
@overload = false

super "Usage: dotenv [options]"
Expand All @@ -20,6 +21,10 @@ def initialize(argv = [])
@filenames = list
end

on("-i", "--ignore", "ignore missing env files") do
@ignore = true
end

on("-o", "--overload", "override existing ENV variables") do
@overload = true
end
Expand All @@ -43,11 +48,11 @@ def initialize(argv = [])
end

def run
if @overload
Dotenv.overload!(*@filenames)
else
Dotenv.load!(*@filenames)
end
meth = "load"
meth = "overload" if @overload
meth = "#{meth}!" unless @ignore

Dotenv.public_send(meth, *@filenames)
rescue Errno::ENOENT => e
abort e.message
else
Expand Down
6 changes: 6 additions & 0 deletions spec/dotenv/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def run(*args)
end.to raise_error(SystemExit, /No such file/)
end

it "ignores missing files when --ignore flag given" do
expect do
run "--ignore", "-f", ".doesnotexist"
end.not_to raise_error
end

it "loads from multiple files specified by -f" do
expect(ENV).not_to have_key("PLAIN")
expect(ENV).not_to have_key("QUOTED")
Expand Down

0 comments on commit 075e043

Please sign in to comment.