Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into standard
Browse files Browse the repository at this point in the history
* origin/master:
  Bump actions/cache from 3.0.4 to 3.0.5
  Bump actions/cache from 2.1.6 to 3.0.4
  Update README
  Update README
  Fix lint failures
  Add overload option for the CLI
  Bump actions/checkout from 2 to 3
  • Loading branch information
bkeepers committed Jul 26, 2022
2 parents 942b4e6 + 8c9a5f0 commit d4f3c05
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
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@v1
- uses: actions/checkout@v3
- name: Release dotenv
uses: cadwallion/publish-rubygems-action@master
env:
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ You can use the `-t` or `--template` flag on the dotenv cli to create a template
```shell
$ 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.
A template will be created in your working directory named `{FINAME}.template`. So in the above example, it would create a `.env.template` file.

The template will contain all the environment variables in your `.env` file but with their values set to the variable names.

Expand All @@ -236,7 +236,7 @@ S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
```

Would become
Would become

```shell
# .env.template
Expand All @@ -250,6 +250,11 @@ 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
$ dotenv -o -f ".env.local,.env"
```

## Contributing

If you want a better idea of how dotenv works, check out the [Ruby Rogues Code Reading of dotenv](https://www.youtube.com/watch?v=lKmY_0uY86s).
Expand Down
20 changes: 18 additions & 2 deletions lib/dotenv/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ module Dotenv
# The CLI is a class responsible of handling all the command line interface
# logic.
class CLI
attr_reader :argv, :filenames
attr_reader :argv, :filenames, :overload

def initialize(argv = [])
@argv = argv.dup
@filenames = []
@overload = false
end

def run
parse_argv!(@argv)

begin
Dotenv.load!(*@filenames)
load_dotenv(@overload, @filenames)
rescue Errno::ENOENT => e
abort e.message
else
Expand All @@ -36,8 +37,17 @@ def parse_argv!(argv)
@filenames
end

def load_dotenv(overload, filenames)
if overload
Dotenv.overload!(*filenames)
else
Dotenv.load!(*filenames)
end
end

def add_options(parser)
add_files_option(parser)
add_overload_option(parser)
add_help_option(parser)
add_version_option(parser)
add_template_option(parser)
Expand All @@ -49,6 +59,12 @@ def add_files_option(parser)
end
end

def add_overload_option(parser)
parser.on("-o", "--overload", "override existing ENV variables") do
@overload = true
end
end

def add_help_option(parser)
parser.on("-h", "--help", "Display help") do
puts parser
Expand Down

0 comments on commit d4f3c05

Please sign in to comment.