diff --git a/README.md b/README.md index a424edaf..b1b5a7a3 100644 --- a/README.md +++ b/README.md @@ -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. @@ -233,7 +233,7 @@ S3_BUCKET=YOURS3BUCKET SECRET_KEY=YOURSECRETKEYGOESHERE ``` -Would become +Would become ```shell # .env.template @@ -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). diff --git a/lib/dotenv/cli.rb b/lib/dotenv/cli.rb index da5c9645..368bf2a4 100644 --- a/lib/dotenv/cli.rb +++ b/lib/dotenv/cli.rb @@ -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 @@ -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) @@ -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