Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into respect-file-priori…
Browse files Browse the repository at this point in the history
…ty-policy-when-overloading

* origin/master:
  Tweak logic for deciding which method to load
  Bump actions/checkout from 3 to 4
  Remove EOL Ruby versions from build matrix (2.5, 2.6, 2.7)
  Fix lint errors
  Add Ruby 3.3 to the build matrix
  Appease Standard.rb
  Optionally ignore missing files from CLI
  Use console syntax highlights for console commands
  • Loading branch information
bkeepers committed Jan 20, 2024
2 parents fef9939 + e455753 commit 29c9232
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ['2.5', '2.6', '2.7', '3.0', '3.1', '3.2']
ruby: ['3.0', '3.1', '3.2', '3.3']
steps:
- name: Check out repository code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Release dotenv
uses: cadwallion/publish-rubygems-action@master
env:
Expand Down
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ gem 'dotenv-rails', groups: [:development, :test]

And then execute:

```shell
```console
$ bundle
```

Expand Down Expand Up @@ -49,7 +49,7 @@ gem 'gem-that-requires-env-variables'

Install the gem:

```shell
```console
$ gem install dotenv
```

Expand All @@ -72,16 +72,22 @@ Dotenv.load('file1.env', 'file2.env')

Alternatively, you can use the `dotenv` executable to launch your application:

```shell
```console
$ 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 Expand Up @@ -223,7 +229,8 @@ Credentials should only be accessible on the machines that need access to them.


You can use the `-t` or `--template` flag on the dotenv cli to create a template of your `.env` file.
```shell

```console
$ dotenv -t .env
```
A template will be created in your working directory named `{FINAME}.template`. So in the above example, it would create a `.env.template` file.
Expand Down Expand Up @@ -251,7 +258,8 @@ Personally, I prefer to commit the `.env` file with development-only settings. T
By default, it **won't** overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does. To overwrite existing environment variables you can use `Dotenv.overload`.

You can also use the `-o` or `--overload` flag on the dotenv cli to override existing `ENV` variables.
```shell

```console
$ dotenv -o -f ".env.local,.env"
```

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

super "Usage: dotenv [options]"
super("Usage: dotenv [options]")
separator ""

on("-f FILES", Array, "List of env files to parse") do |list|
@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,10 @@ def initialize(argv = [])
end

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

Dotenv.public_send(method, *@filenames)
rescue Errno::ENOENT => e
abort e.message
else
Expand Down
2 changes: 1 addition & 1 deletion lib/dotenv/missing_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Error < StandardError; end

class MissingKeys < Error # :nodoc:
def initialize(keys)
key_word = "key#{keys.size > 1 ? "s" : ""}"
key_word = "key#{(keys.size > 1) ? "s" : ""}"
super("Missing required configuration #{key_word}: #{keys.inspect}")
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dotenv/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def template_line(line)
var, value = line.split("=")
template = var.gsub(EXPORT_COMMAND, "")
is_a_comment = var.strip[0].eql?("#")
value.nil? || is_a_comment ? line : "#{var}=#{template}"
(value.nil? || is_a_comment) ? line : "#{var}=#{template}"
end
end
end
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 29c9232

Please sign in to comment.