diff --git a/README.md b/README.md index 2691a02c..4a83718c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ gem 'dotenv-rails', groups: [:development, :test] And then execute: -```shell +```console $ bundle ``` @@ -49,7 +49,7 @@ gem 'gem-that-requires-env-variables' Install the gem: -```shell +```console $ gem install dotenv ``` @@ -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 @@ -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. @@ -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" ``` diff --git a/lib/dotenv/cli.rb b/lib/dotenv/cli.rb index 4d3b6f48..70ca18f4 100644 --- a/lib/dotenv/cli.rb +++ b/lib/dotenv/cli.rb @@ -11,6 +11,7 @@ class CLI < OptionParser def initialize(argv = []) @argv = argv.dup @filenames = [] + @ignore = false @overload = false super("Usage: dotenv [options]") @@ -20,6 +21,10 @@ def initialize(argv = []) @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 @@ -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 diff --git a/spec/dotenv/cli_spec.rb b/spec/dotenv/cli_spec.rb index 012e5258..afcb1431 100644 --- a/spec/dotenv/cli_spec.rb +++ b/spec/dotenv/cli_spec.rb @@ -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")