Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into jenfi-eng/master
Browse files Browse the repository at this point in the history
* origin/master: (29 commits)
  Tweak logic for deciding which method to load
  Bump actions/checkout from 3 to 4
  Remove EOL Ruby versions from build matrix (2.5, 2.6, 2.7)
  Fix lint errors
  Add Ruby 3.3 to the build matrix
  Appease Standard.rb
  Optionally ignore missing files from CLI
  Use console syntax highlights for console commands
  fix: always respect file importance order
  Prepare for 2.8.1 release
  Disable rule that fails on Ruby 2.5
  Specify ruby_version in .standard.yml
  Avoid newer ruby range syntax
  Continue to support EOL'd Ruby versions (sigh)
  Prepare for 2.8.0 release
  Remove unnecessary method
  CL < OptionParser to simplify setup more
  Simplify CLI setup
  Update ruby action
  Update Ruby version matrix
  ...
  • Loading branch information
bkeepers committed Jan 20, 2024
2 parents 7d79f66 + 32c521b commit c95f516
Show file tree
Hide file tree
Showing 24 changed files with 192 additions and 189 deletions.
19 changes: 5 additions & 14 deletions .github/workflows/ci.yml
Expand Up @@ -7,25 +7,16 @@ jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby: ['2.5', '2.6', '2.7', '3.0']
ruby: ['3.0', '3.1', '3.2', '3.3']
steps:
- name: Check out repository code
uses: actions/checkout@v2
- name: Do some action caching
uses: actions/cache@v2.1.6
with:
path: vendor
key: ${{ runner.os }}-gem-${{ hashFiles('Gemfile', '*.gemspec') }}
restore-keys: |
${{ runner.os }}-gem-
uses: actions/checkout@v4
- name: Set up Ruby
uses: actions/setup-ruby@v1
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: ${{ matrix.ruby }}
- name: Install bundler
run: gem install bundler
- name: Run bundler
run: bundle install --jobs 4 --retry 3
- name: Run Rake
run: bundle exec rake
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -4,7 +4,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Release dotenv
uses: cadwallion/publish-rubygems-action@master
env:
Expand Down
20 changes: 0 additions & 20 deletions .rubocop.yml

This file was deleted.

5 changes: 5 additions & 0 deletions .standard.yml
@@ -0,0 +1,5 @@
ruby_version: 2.5

ignore:
- lib/dotenv/parser.rb:
- Lint/InheritException
13 changes: 12 additions & 1 deletion Changelog.md
@@ -1,6 +1,17 @@
# Changelog

[Unreleased changes](https://github.com/bkeepers/dotenv/compare/v2.7.6...master)
[Unreleased changes](https://github.com/bkeepers/dotenv/compare/v2.8.1...master)

## 2.8.1 - July 27, 2022

* Restore support for EOL'd Ruby versions (2.5, 2.6) (#458)[https://github.com/bkeepers/dotenv/pull/458]

## 2.8.0 - July 26, 2022

* Drop support for Ruby 2.4, 2.5, and 2.6, which are EOL
* Fix template (-t) handling of export declarations [#416](https://github.com/bkeepers/dotenv/pull/416)
* Unescape escaped characters when value is not quoted [#421](https://github.com/bkeepers/dotenv/pull/421)
* Add overload option for the CLI (`$ dotenv --overload -f .env.local`) [#445](https://github.com/bkeepers/dotenv/pull/445)

## 2.7.6 - July 11, 2020

Expand Down
2 changes: 1 addition & 1 deletion Guardfile
Expand Up @@ -5,5 +5,5 @@ end
guard "rspec", cmd: "bundle exec rspec" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^spec/spec_helper.rb$}) { "spec" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
end
34 changes: 25 additions & 9 deletions README.md
Expand Up @@ -18,7 +18,7 @@ gem 'dotenv-rails', groups: [:development, :test]

And then execute:

```shell
```console
$ bundle
```

Expand All @@ -30,7 +30,10 @@ dotenv is initialized in your Rails app during the `before_configuration` callba
# config/application.rb
Bundler.require(*Rails.groups)

Dotenv::Railtie.load
# Load dotenv only in development or test environment
if ['development', 'test'].include? ENV['RAILS_ENV']
Dotenv::Railtie.load
end

HOSTNAME = ENV['HOSTNAME']
```
Expand All @@ -46,7 +49,7 @@ gem 'gem-that-requires-env-variables'

Install the gem:

```shell
```console
$ gem install dotenv
```

Expand All @@ -69,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
Expand Down Expand Up @@ -220,10 +229,11 @@ 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.
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 @@ -233,7 +243,7 @@ S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
```

Would become
Would become

```shell
# .env.template
Expand All @@ -247,6 +257,12 @@ 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.

```console
$ 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
19 changes: 11 additions & 8 deletions Rakefile
Expand Up @@ -6,13 +6,17 @@ namespace "dotenv" do
Bundler::GemHelper.install_tasks name: "dotenv"
end

namespace "dotenv-rails" do
class DotenvRailsGemHelper < Bundler::GemHelper
def guard_already_tagged; end # noop
class DotenvRailsGemHelper < Bundler::GemHelper
def guard_already_tagged
# noop
end

def tag_version; end # noop
def tag_version
# noop
end
end

namespace "dotenv-rails" do
DotenvRailsGemHelper.install_tasks name: "dotenv-rails"
end

Expand All @@ -24,11 +28,10 @@ require "rspec/core/rake_task"

desc "Run all specs"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w(--color)
t.rspec_opts = %w[--color]
t.verbose = false
end

require "rubocop/rake_task"
RuboCop::RakeTask.new
require "standard/rake"

task default: [:spec, :rubocop]
task default: [:spec, :standard]
12 changes: 6 additions & 6 deletions dotenv-rails.gemspec
Expand Up @@ -2,12 +2,12 @@ require File.expand_path("../lib/dotenv/version", __FILE__)
require "English"

Gem::Specification.new "dotenv-rails", Dotenv::VERSION do |gem|
gem.authors = ["Brandon Keepers"]
gem.email = ["brandon@opensoul.org"]
gem.description = gem.summary = "Autoload dotenv in Rails."
gem.homepage = "https://github.com/bkeepers/dotenv"
gem.license = "MIT"
gem.files = `git ls-files lib | grep rails`.split(
gem.authors = ["Brandon Keepers"]
gem.email = ["brandon@opensoul.org"]
gem.description = gem.summary = "Autoload dotenv in Rails."
gem.homepage = "https://github.com/bkeepers/dotenv"
gem.license = "MIT"
gem.files = `git ls-files lib | grep rails`.split(
$OUTPUT_RECORD_SEPARATOR
) + ["README.md", "LICENSE"]

Expand Down
16 changes: 8 additions & 8 deletions dotenv.gemspec
Expand Up @@ -2,17 +2,17 @@ require File.expand_path("../lib/dotenv/version", __FILE__)
require "English"

Gem::Specification.new "dotenv", Dotenv::VERSION do |gem|
gem.authors = ["Brandon Keepers"]
gem.email = ["brandon@opensoul.org"]
gem.description = gem.summary = "Loads environment variables from `.env`."
gem.homepage = "https://github.com/bkeepers/dotenv"
gem.license = "MIT"
gem.authors = ["Brandon Keepers"]
gem.email = ["brandon@opensoul.org"]
gem.description = gem.summary = "Loads environment variables from `.env`."
gem.homepage = "https://github.com/bkeepers/dotenv"
gem.license = "MIT"

gem_files = `git ls-files README.md LICENSE lib bin | grep -v rails`
gem.files = gem_files.split($OUTPUT_RECORD_SEPARATOR)
gem_files = `git ls-files README.md LICENSE lib bin | grep -v rails`
gem.files = gem_files.split($OUTPUT_RECORD_SEPARATOR)
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }

gem.add_development_dependency "rake"
gem.add_development_dependency "rspec"
gem.add_development_dependency "rubocop", "~>0.40.0"
gem.add_development_dependency "standard"
end
4 changes: 2 additions & 2 deletions lib/dotenv.rb
Expand Up @@ -29,7 +29,7 @@ def load!(*filenames)

# same as `load`, but will override existing values in `ENV`
def overload(*filenames)
with(*filenames) do |f|
with(*filenames.reverse) do |f|
ignoring_nonexistent_files do
env = Environment.new(f, false)
instrument("dotenv.overload", env: env) { env.apply! }
Expand All @@ -39,7 +39,7 @@ def overload(*filenames)

# same as `overload`, but raises Errno::ENOENT if any files don't exist
def overload!(*filenames)
with(*filenames) do |f|
with(*filenames.reverse) do |f|
env = Environment.new(f, false)
instrument("dotenv.overload", env: env) { env.apply! }
end
Expand Down
77 changes: 29 additions & 48 deletions lib/dotenv/cli.rb
Expand Up @@ -4,77 +4,58 @@
require "optparse"

module Dotenv
# The CLI is a class responsible of handling all the command line interface
# logic.
class CLI
attr_reader :argv, :filenames
# The command line interface
class CLI < OptionParser
attr_reader :argv, :filenames, :overload

def initialize(argv = [])
@argv = argv.dup
@filenames = []
end
@ignore = false
@overload = false

def run
parse_argv!(@argv)
super("Usage: dotenv [options]")
separator ""

begin
Dotenv.load!(*@filenames)
rescue Errno::ENOENT => e
abort e.message
else
exec(*@argv) unless @argv.empty?
on("-f FILES", Array, "List of env files to parse") do |list|
@filenames = list
end
end

private

def parse_argv!(argv)
parser = create_option_parser
add_options(parser)
parser.order!(argv)

@filenames
end

def add_options(parser)
add_files_option(parser)
add_help_option(parser)
add_version_option(parser)
add_template_option(parser)
end
on("-i", "--ignore", "ignore missing env files") do
@ignore = true
end

def add_files_option(parser)
parser.on("-f FILES", Array, "List of env files to parse") do |list|
@filenames = list
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
on("-h", "--help", "Display help") do
puts self
exit
end
end

def add_version_option(parser)
parser.on("-v", "--version", "Show version") do
on("-v", "--version", "Show version") do
puts "dotenv #{Dotenv::VERSION}"
exit
end
end

def add_template_option(parser)
parser.on("-t", "--template=FILE", "Create a template env file") do |file|
on("-t", "--template=FILE", "Create a template env file") do |file|
template = Dotenv::EnvTemplate.new(file)
template.create_template
end

order!(@argv)
end

def create_option_parser
OptionParser.new do |parser|
parser.banner = "Usage: dotenv [options]"
parser.separator ""
end
def run
method = @overload ? "overload" : "load"
method = "#{method}!" unless @ignore

Dotenv.public_send(method, *@filenames)
rescue Errno::ENOENT => e
abort e.message
else
exec(*@argv) unless @argv.empty?
end
end
end

0 comments on commit c95f516

Please sign in to comment.