Skip to content

Commit

Permalink
Merge pull request #445 from AhmedKamal20/cli-overload
Browse files Browse the repository at this point in the history
Add overload option for the CLI
  • Loading branch information
bkeepers authored Jun 25, 2022
2 parents 8723806 + 60b2135 commit c6e583a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
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 c6e583a

Please sign in to comment.