Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overload option for the CLI #445

Merged
merged 4 commits into from
Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,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 @@ -233,7 +233,7 @@ S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
```

Would become
Would become

```shell
# .env.template
Expand All @@ -247,6 +247,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