From 9bb5db95ab148982f993aa4e4c16a63dbfa22b19 Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Fri, 26 May 2023 12:37:41 -0400 Subject: [PATCH 1/8] 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 2691a02c..81a19fb5 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/8] 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 81a19fb5..4a83718c 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 1a586a51..af120510 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 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") From 1a880cd02d06abf4fb0fbf68647aa7786789686d Mon Sep 17 00:00:00 2001 From: Steven Harman Date: Thu, 15 Jun 2023 14:34:57 -0400 Subject: [PATCH 3/8] 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 ecedcbf6..ad6110b8 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 1aa37a4b..ce42e36c 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 4b6e522fc4fe3a64d22e00eff633758cde14a72d Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Sat, 20 Jan 2024 07:15:56 -0500 Subject: [PATCH 4/8] Add Ruby 3.3 to the build matrix --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3d661b6d..36684a89 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: false matrix: - ruby: ['2.5', '2.6', '2.7', '3.0', '3.1', '3.2'] + ruby: ['2.5', '2.6', '2.7', '3.0', '3.1', '3.2', '3.3'] steps: - name: Check out repository code uses: actions/checkout@v3 From 10081be705c2658c3ff670e2580997dc87f51065 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Sat, 20 Jan 2024 07:19:53 -0500 Subject: [PATCH 5/8] Fix lint errors --- lib/dotenv/cli.rb | 2 +- lib/dotenv/missing_keys.rb | 2 +- lib/dotenv/template.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/dotenv/cli.rb b/lib/dotenv/cli.rb index 1a586a51..4d3b6f48 100644 --- a/lib/dotenv/cli.rb +++ b/lib/dotenv/cli.rb @@ -13,7 +13,7 @@ def initialize(argv = []) @filenames = [] @overload = false - super "Usage: dotenv [options]" + super("Usage: dotenv [options]") separator "" on("-f FILES", Array, "List of env files to parse") do |list| diff --git a/lib/dotenv/missing_keys.rb b/lib/dotenv/missing_keys.rb index ecedcbf6..ad6110b8 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 1aa37a4b..ce42e36c 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 e0463a2c09fb8e2fa70532ce6ac8839684d30dab Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Sat, 20 Jan 2024 07:26:08 -0500 Subject: [PATCH 6/8] Remove EOL Ruby versions from build matrix (2.5, 2.6, 2.7) --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 36684a89..88503bdf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: false matrix: - ruby: ['2.5', '2.6', '2.7', '3.0', '3.1', '3.2', '3.3'] + ruby: ['3.0', '3.1', '3.2', '3.3'] steps: - name: Check out repository code uses: actions/checkout@v3 From 7230c4f37666a3c9d4a443b8980106be0f3b4500 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 20 Jan 2024 12:28:54 +0000 Subject: [PATCH 7/8] Bump actions/checkout from 3 to 4 Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v3...v4) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88503bdf..b3d77ebf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: ruby: ['3.0', '3.1', '3.2', '3.3'] steps: - name: Check out repository code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d8b2ec0b..31c83357 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,7 +4,7 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Release dotenv uses: cadwallion/publish-rubygems-action@master env: From fd0f664d413691c5cdce6df535b9079a27b755c3 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Sat, 20 Jan 2024 07:39:22 -0500 Subject: [PATCH 8/8] 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 0708c665..70ca18f4 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