From 9bb5db95ab148982f993aa4e4c16a63dbfa22b19 Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Fri, 26 May 2023 12:37:41 -0400 Subject: [PATCH 1/4] Use console syntax highlights for console commands --- README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2691a02..81a19fb 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,13 +72,13 @@ 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. -``` +```console $ dotenv -f ".env.local,.env" ./script.rb ``` @@ -223,7 +223,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 +252,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" ``` From 075e0433a401932083376f44bb4e9df76f497995 Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Fri, 26 May 2023 12:38:19 -0400 Subject: [PATCH 2/4] Optionally ignore missing files from CLI This is particularly useful for wrapper scripts (like a `bin/dev` https://stevenharman.net/bin-dev-for-heroku-local) trying to mimic `dotenv-rails`'s ordered loading of multiple files. --- README.md | 8 +++++++- lib/dotenv/cli.rb | 15 ++++++++++----- spec/dotenv/cli_spec.rb | 6 ++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 81a19fb..4a83718 100644 --- a/README.md +++ b/README.md @@ -76,12 +76,18 @@ Alternatively, you can use the `dotenv` executable to launch your application: $ 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 diff --git a/lib/dotenv/cli.rb b/lib/dotenv/cli.rb index 1a586a5..af12051 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,11 @@ def initialize(argv = []) end def run - if @overload - Dotenv.overload!(*@filenames) - else - Dotenv.load!(*@filenames) - end + meth = "load" + meth = "overload" if @overload + meth = "#{meth}!" unless @ignore + + Dotenv.public_send(meth, *@filenames) rescue Errno::ENOENT => e abort e.message else diff --git a/spec/dotenv/cli_spec.rb b/spec/dotenv/cli_spec.rb index 012e525..afcb143 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") From 1a880cd02d06abf4fb0fbf68647aa7786789686d Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Thu, 15 Jun 2023 14:34:57 -0400 Subject: [PATCH 3/4] Appease Standard.rb Use parentheses for ternary expressions with complex conditions. NOTE: I didn't change these lines, but I think an update to Standard is catching these lingering lint violations? --- lib/dotenv/missing_keys.rb | 2 +- lib/dotenv/template.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dotenv/missing_keys.rb b/lib/dotenv/missing_keys.rb index ecedcbf..ad6110b 100644 --- a/lib/dotenv/missing_keys.rb +++ b/lib/dotenv/missing_keys.rb @@ -3,7 +3,7 @@ class Error < StandardError; end class MissingKeys < Error # :nodoc: def initialize(keys) - key_word = "key#{keys.size > 1 ? "s" : ""}" + key_word = "key#{(keys.size > 1) ? "s" : ""}" super("Missing required configuration #{key_word}: #{keys.inspect}") end end diff --git a/lib/dotenv/template.rb b/lib/dotenv/template.rb index 1aa37a4..ce42e36 100644 --- a/lib/dotenv/template.rb +++ b/lib/dotenv/template.rb @@ -20,7 +20,7 @@ def template_line(line) var, value = line.split("=") template = var.gsub(EXPORT_COMMAND, "") is_a_comment = var.strip[0].eql?("#") - value.nil? || is_a_comment ? line : "#{var}=#{template}" + (value.nil? || is_a_comment) ? line : "#{var}=#{template}" end end end From fd0f664d413691c5cdce6df535b9079a27b755c3 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Sat, 20 Jan 2024 07:39:22 -0500 Subject: [PATCH 4/4] Tweak logic for deciding which method to load --- lib/dotenv/cli.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/dotenv/cli.rb b/lib/dotenv/cli.rb index 0708c66..70ca18f 100644 --- a/lib/dotenv/cli.rb +++ b/lib/dotenv/cli.rb @@ -48,11 +48,10 @@ def initialize(argv = []) end def run - meth = "load" - meth = "overload" if @overload - meth = "#{meth}!" unless @ignore + method = @overload ? "overload" : "load" + method = "#{method}!" unless @ignore - Dotenv.public_send(meth, *@filenames) + Dotenv.public_send(method, *@filenames) rescue Errno::ENOENT => e abort e.message else