dotenv 0.10.0 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,35 +1,43 @@
1
+ require "English"
2
+
1
3
  module Dotenv
2
4
  module Substitutions
5
+ # Substitute variables in a value.
6
+ #
7
+ # HOST=example.com
8
+ # URL="https://$HOST"
9
+ #
3
10
  module Variable
4
11
  class << self
5
-
6
12
  VARIABLE = /
7
- (\\)?
8
- (\$)
9
- ( # collect braces with var for sub
10
- \{? # allow brace wrapping
11
- ([A-Z0-9_]+) # match the variable
12
- \}? # closing brace
13
- )
13
+ (\\)? # is it escaped with a backslash?
14
+ (\$) # literal $
15
+ (?!\() # shouldnt be followed by paranthesis
16
+ \{? # allow brace wrapping
17
+ ([A-Z0-9_]+)? # optional alpha nums
18
+ \}? # closing brace
14
19
  /xi
15
20
 
16
- def call(value, env)
17
- # Process embedded variables
18
- value.scan(VARIABLE).each do |parts|
19
- if parts.first == '\\'
20
- replace = parts[1...-1].join('')
21
- else
22
- replace = env.fetch(parts.last) { ENV[parts.last] }
23
- end
24
-
25
- value = value.sub(parts[0...-1].join(''), replace || '')
21
+ def call(value, env, is_load)
22
+ combined_env = is_load ? env.merge(ENV) : ENV.to_h.merge(env)
23
+ value.gsub(VARIABLE) do |variable|
24
+ match = $LAST_MATCH_INFO
25
+ substitute(match, variable, combined_env)
26
26
  end
27
+ end
27
28
 
28
- value
29
+ private
30
+
31
+ def substitute(match, variable, env)
32
+ if match[1] == "\\"
33
+ variable[1..]
34
+ elsif match[3]
35
+ env.fetch(match[3], "")
36
+ else
37
+ variable
38
+ end
29
39
  end
30
40
  end
31
-
32
41
  end
33
-
34
42
  end
35
43
  end
data/lib/dotenv/tasks.rb CHANGED
@@ -1,7 +1,7 @@
1
- desc 'Load environment settings from .env'
1
+ desc "Load environment settings from .env"
2
2
  task :dotenv do
3
- require 'dotenv'
3
+ require "dotenv"
4
4
  Dotenv.load
5
5
  end
6
6
 
7
- task :environment => :dotenv
7
+ task environment: :dotenv
@@ -0,0 +1,26 @@
1
+ module Dotenv
2
+ EXPORT_COMMAND = "export ".freeze
3
+ # Class for creating a template from a env file
4
+ class EnvTemplate
5
+ def initialize(env_file)
6
+ @env_file = env_file
7
+ end
8
+
9
+ def create_template
10
+ File.open(@env_file, "r") do |env_file|
11
+ File.open("#{@env_file}.template", "w") do |env_template|
12
+ env_file.each do |line|
13
+ env_template.puts template_line(line)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ def template_line(line)
20
+ var, value = line.split("=")
21
+ template = var.gsub(EXPORT_COMMAND, "")
22
+ is_a_comment = var.strip[0].eql?("#")
23
+ value.nil? || is_a_comment ? line : "#{var}=#{template}"
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module Dotenv
2
- VERSION = '0.10.0'
2
+ VERSION = "2.8.0".freeze
3
3
  end
data/lib/dotenv.rb CHANGED
@@ -1,33 +1,86 @@
1
- require 'dotenv/environment'
1
+ require "dotenv/parser"
2
+ require "dotenv/environment"
3
+ require "dotenv/missing_keys"
2
4
 
5
+ # The top level Dotenv module. The entrypoint for the application logic.
3
6
  module Dotenv
4
- def self.load(*filenames)
5
- default_if_empty(filenames).inject({}) do |hash, filename|
6
- filename = File.expand_path filename
7
- hash.merge(File.exists?(filename) ? Environment.new(filename).apply : {})
7
+ class << self
8
+ attr_accessor :instrumenter
9
+ end
10
+
11
+ module_function
12
+
13
+ def load(*filenames)
14
+ with(*filenames) do |f|
15
+ ignoring_nonexistent_files do
16
+ env = Environment.new(f, true)
17
+ instrument("dotenv.load", env: env) { env.apply }
18
+ end
19
+ end
20
+ end
21
+
22
+ # same as `load`, but raises Errno::ENOENT if any files don't exist
23
+ def load!(*filenames)
24
+ with(*filenames) do |f|
25
+ env = Environment.new(f, true)
26
+ instrument("dotenv.load", env: env) { env.apply }
8
27
  end
9
28
  end
10
29
 
11
30
  # same as `load`, but will override existing values in `ENV`
12
- def self.overload(*filenames)
13
- default_if_empty(filenames).inject({}) do |hash, filename|
14
- filename = File.expand_path filename
15
- hash.merge(File.exists?(filename) ? Environment.new(filename).apply! : {})
31
+ def overload(*filenames)
32
+ with(*filenames) do |f|
33
+ ignoring_nonexistent_files do
34
+ env = Environment.new(f, false)
35
+ instrument("dotenv.overload", env: env) { env.apply! }
36
+ end
16
37
  end
17
38
  end
18
39
 
19
- # same as `load`, but raises Errno::ENOENT if any files don't exist
20
- def self.load!(*filenames)
21
- load(
22
- *default_if_empty(filenames).each do |filename|
23
- filename = File.expand_path filename
24
- raise(Errno::ENOENT.new(filename)) unless File.exists?(filename)
40
+ # same as `overload`, but raises Errno::ENOENT if any files don't exist
41
+ def overload!(*filenames)
42
+ with(*filenames) do |f|
43
+ env = Environment.new(f, false)
44
+ instrument("dotenv.overload", env: env) { env.apply! }
45
+ end
46
+ end
47
+
48
+ # returns a hash of parsed key/value pairs but does not modify ENV
49
+ def parse(*filenames)
50
+ with(*filenames) do |f|
51
+ ignoring_nonexistent_files do
52
+ Environment.new(f, false)
25
53
  end
26
- )
54
+ end
55
+ end
56
+
57
+ # Internal: Helper to expand list of filenames.
58
+ #
59
+ # Returns a hash of all the loaded environment variables.
60
+ def with(*filenames)
61
+ filenames << ".env" if filenames.empty?
62
+
63
+ filenames.reduce({}) do |hash, filename|
64
+ hash.merge!(yield(File.expand_path(filename)) || {})
65
+ end
66
+ end
67
+
68
+ def instrument(name, payload = {}, &block)
69
+ if instrumenter
70
+ instrumenter.instrument(name, payload, &block)
71
+ else
72
+ yield
73
+ end
74
+ end
75
+
76
+ def require_keys(*keys)
77
+ missing_keys = keys.flatten - ::ENV.keys
78
+ return if missing_keys.empty?
79
+ raise MissingKeys, missing_keys
27
80
  end
28
81
 
29
- protected
30
- def self.default_if_empty(filenames)
31
- filenames.empty? ? (filenames << '.env') : filenames
82
+ def ignoring_nonexistent_files
83
+ yield
84
+ rescue Errno::ENOENT
32
85
  end
33
86
  end
metadata CHANGED
@@ -1,41 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Keepers
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-22 00:00:00.000000000 Z
11
+ date: 2022-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: standard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  description: Loads environment variables from `.env`.
@@ -46,65 +60,41 @@ executables:
46
60
  extensions: []
47
61
  extra_rdoc_files: []
48
62
  files:
49
- - .env
50
- - .gitignore
51
- - .travis.yml
52
- - Changelog.md
53
- - Gemfile
54
- - Guardfile
55
63
  - LICENSE
56
64
  - README.md
57
- - Rakefile
58
65
  - bin/dotenv
59
- - dotenv-rails.gemspec
60
- - dotenv.gemspec
61
- - lib/dotenv-rails.rb
62
66
  - lib/dotenv.rb
63
- - lib/dotenv/capistrano.rb
64
- - lib/dotenv/capistrano/recipes.rb
67
+ - lib/dotenv/cli.rb
65
68
  - lib/dotenv/environment.rb
66
- - lib/dotenv/format_error.rb
67
- - lib/dotenv/railtie.rb
69
+ - lib/dotenv/load.rb
70
+ - lib/dotenv/missing_keys.rb
71
+ - lib/dotenv/parser.rb
68
72
  - lib/dotenv/substitutions/command.rb
69
73
  - lib/dotenv/substitutions/variable.rb
70
74
  - lib/dotenv/tasks.rb
75
+ - lib/dotenv/template.rb
71
76
  - lib/dotenv/version.rb
72
- - spec/dotenv/environment_spec.rb
73
- - spec/dotenv_spec.rb
74
- - spec/fixtures/exported.env
75
- - spec/fixtures/plain.env
76
- - spec/fixtures/quoted.env
77
- - spec/fixtures/yaml.env
78
- - spec/spec_helper.rb
79
77
  homepage: https://github.com/bkeepers/dotenv
80
78
  licenses:
81
79
  - MIT
82
80
  metadata: {}
83
- post_install_message:
81
+ post_install_message:
84
82
  rdoc_options: []
85
83
  require_paths:
86
84
  - lib
87
85
  required_ruby_version: !ruby/object:Gem::Requirement
88
86
  requirements:
89
- - - '>='
87
+ - - ">="
90
88
  - !ruby/object:Gem::Version
91
89
  version: '0'
92
90
  required_rubygems_version: !ruby/object:Gem::Requirement
93
91
  requirements:
94
- - - '>='
92
+ - - ">="
95
93
  - !ruby/object:Gem::Version
96
94
  version: '0'
97
95
  requirements: []
98
- rubyforge_project:
99
- rubygems_version: 2.0.3
100
- signing_key:
96
+ rubygems_version: 3.2.32
97
+ signing_key:
101
98
  specification_version: 4
102
99
  summary: Loads environment variables from `.env`.
103
- test_files:
104
- - spec/dotenv/environment_spec.rb
105
- - spec/dotenv_spec.rb
106
- - spec/fixtures/exported.env
107
- - spec/fixtures/plain.env
108
- - spec/fixtures/quoted.env
109
- - spec/fixtures/yaml.env
110
- - spec/spec_helper.rb
100
+ test_files: []
data/.env DELETED
@@ -1 +0,0 @@
1
- DOTENV=true
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- tmp
8
- vendor
data/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.1.0
4
- - 2.0.0
5
- - 1.9.3
6
- - 1.8.7
7
- - ree
8
- - jruby-19mode
9
- - rbx
10
- bundler_args: --without=guard
data/Changelog.md DELETED
@@ -1,95 +0,0 @@
1
- # Changelog
2
-
3
- ## 0.10.0 - Feb 22, 2014
4
-
5
- * Add support for executing interpolated commands. (Ruby >= 1.9 only)
6
-
7
- HEAD_SHA=$(git rev-parse HEAD)
8
-
9
- * Add `dotenv_role` option in Capistrano.
10
-
11
- set :dotenv_role, [:app, web]
12
-
13
- * Add `Dotenv.overload` to overwrite existing environment values.
14
-
15
- ## 0.9.0 - Aug 29, 2013
16
-
17
- * Add support for variable expansion.
18
-
19
- HOST="example.com"
20
- URL="http://${USER}@${HOST}"
21
- ESCAPED_VARIABLE="this is \$NOT replaced"
22
-
23
- * Allow setting variables without a value.
24
-
25
- BLANK=
26
-
27
- * Add `dotenv` executable to load `.env` for other scripts.
28
-
29
- $ dotenv ./script.py
30
-
31
- [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.8.0...v0.9.0)
32
-
33
- ## 0.8.0 - June 12, 2013
34
-
35
- * Added a capistrano recipe to symlink in `.env` on deploy.
36
-
37
- * Allow inline comments
38
-
39
- VARIABLE=value # this is a comment
40
-
41
- * Raises Dotenv::FormatError when parsing fails
42
-
43
- [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.7.0...v0.8.0)
44
-
45
- ## 0.7.0 - April 15, 2013
46
-
47
- * Remove deprectated autoloading. Upgrade to 0.6 first and fix any warnings.
48
-
49
- * Add Dotenv.load! which raises Errno::ENOENT if the file does not exist
50
-
51
- [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.6.0...v0.7.0)
52
-
53
- ## 0.6.0 - Mar 22, 2013
54
-
55
- * Add dotenv-rails gem for autoloading in a Rails app
56
-
57
- * Deprecated autoloading with plain dotenv gem
58
-
59
- * Support for double quotes
60
-
61
- A="some value"
62
- B="with \"escaped\" quotes"
63
- C="and newline\n expansion"
64
-
65
- * Support for pow-style variables prefixed with export
66
-
67
- export VARIABLE="some value"
68
-
69
- [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.5.0...v0.6.0)
70
-
71
- ## 0.5.0 - Jan 25, 2013
72
-
73
- * Load immediately on require in Rails instead of waiting for initialization
74
-
75
- * Add YAML-style variables
76
-
77
- VARIABLE: some value
78
-
79
- [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.4.0...v0.5.0)
80
-
81
- ## 0.4.0 - Nov 13, 2012
82
-
83
- * Add support for quoted options, e.g.:
84
-
85
- VARIABLE='some value'
86
-
87
- * Fix rake deprecation warnings
88
-
89
- [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.3.0...v0.4.0)
90
-
91
- ## 0.3.0 - Oct 25, 2012
92
-
93
- * Avoid overriding existing ENV variables so values set before loading the app are maintained.
94
-
95
- [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.2.0...v0.3.0)
data/Gemfile DELETED
@@ -1,12 +0,0 @@
1
- source 'https://rubygems.org'
2
- gemspec :name => 'dotenv'
3
-
4
- group :guard do
5
- gem 'guard-rspec'
6
- gem 'guard-bundler'
7
- gem 'rb-fsevent'
8
- end
9
-
10
- platforms :rbx do
11
- gem 'rubysl', '~> 2.0' # if using anything in the ruby standard library
12
- end
data/Guardfile DELETED
@@ -1,9 +0,0 @@
1
- guard 'bundler' do
2
- watch('Gemfile')
3
- end
4
-
5
- guard 'rspec', :cli => '--color' do
6
- watch(%r{^spec/.+_spec\.rb$})
7
- watch(%r{^spec/spec_helper.rb$}) { "spec" }
8
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
- end
data/Rakefile DELETED
@@ -1,30 +0,0 @@
1
- #!/usr/bin/env rake
2
-
3
- require 'bundler/gem_helper'
4
-
5
- namespace 'dotenv' do
6
- Bundler::GemHelper.install_tasks :name => 'dotenv'
7
- end
8
-
9
- namespace 'dotenv-rails' do
10
- class DotenvRailsGemHelper < Bundler::GemHelper
11
- def guard_already_tagged; end # noop
12
- def tag_version; end # noop
13
- end
14
-
15
- DotenvRailsGemHelper.install_tasks :name => 'dotenv-rails'
16
- end
17
-
18
- task :build => ["dotenv:build", 'dotenv-rails:build']
19
- task :install => ["dotenv:install", 'dotenv-rails:install']
20
- task :release => ["dotenv:release", 'dotenv-rails:release']
21
-
22
- require 'rspec/core/rake_task'
23
-
24
- desc "Run all specs"
25
- RSpec::Core::RakeTask.new(:spec) do |t|
26
- t.rspec_opts = %w[--color]
27
- t.verbose = false
28
- end
29
-
30
- task :default => :spec
data/dotenv-rails.gemspec DELETED
@@ -1,18 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/dotenv/version', __FILE__)
3
-
4
- Gem::Specification.new do |gem|
5
- gem.version = Dotenv::VERSION
6
- gem.authors = ["Brandon Keepers"]
7
- gem.email = ["brandon@opensoul.org"]
8
- gem.description = %q{Autoload dotenv in Rails.}
9
- gem.summary = %q{Autoload dotenv in Rails.}
10
- gem.homepage = "https://github.com/bkeepers/dotenv"
11
- gem.license = 'MIT'
12
-
13
- gem.files = ["lib/dotenv-rails.rb"]
14
- gem.name = "dotenv-rails"
15
- gem.require_paths = ["lib"]
16
-
17
- gem.add_dependency 'dotenv', Dotenv::VERSION
18
- end
data/dotenv.gemspec DELETED
@@ -1,21 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/dotenv/version', __FILE__)
3
-
4
- Gem::Specification.new do |gem|
5
- gem.version = Dotenv::VERSION
6
- gem.authors = ["Brandon Keepers"]
7
- gem.email = ["brandon@opensoul.org"]
8
- gem.description = %q{Loads environment variables from `.env`.}
9
- gem.summary = %q{Loads environment variables from `.env`.}
10
- gem.homepage = "https://github.com/bkeepers/dotenv"
11
- gem.license = 'MIT'
12
-
13
- gem.files = `git ls-files`.split($\)
14
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
- gem.name = "dotenv"
17
- gem.require_paths = ["lib"]
18
-
19
- gem.add_development_dependency 'rake'
20
- gem.add_development_dependency 'rspec'
21
- end
@@ -1,12 +0,0 @@
1
- Capistrano::Configuration.instance(:must_exist).load do
2
- _cset(:dotenv_path){ "#{shared_path}/.env" }
3
-
4
- symlink_args = (role = fetch(:dotenv_role, nil) ? {:roles => role} : {})
5
-
6
- namespace :dotenv do
7
- desc "Symlink shared .env to current release"
8
- task :symlink, symlink_args do
9
- run "ln -nfs #{dotenv_path} #{release_path}/.env"
10
- end
11
- end
12
- end
@@ -1,11 +0,0 @@
1
- require 'capistrano/version'
2
-
3
- if defined?(Capistrano::VERSION) && Capistrano::VERSION >= '3.0'
4
- raise 'Please read https://github.com/bkeepers/dotenv#capistrano-integration to update your dotenv configuration for new Capistrano version'
5
- else
6
- require 'dotenv/capistrano/recipes'
7
-
8
- Capistrano::Configuration.instance(:must_exist).load do
9
- before "deploy:finalize_update", "dotenv:symlink"
10
- end
11
- end
@@ -1,4 +0,0 @@
1
- module Dotenv
2
- class FormatError < SyntaxError
3
- end
4
- end
@@ -1,14 +0,0 @@
1
- require 'dotenv'
2
-
3
- module Dotenv
4
- class Railtie < Rails::Railtie
5
- rake_tasks do
6
- desc 'Load environment settings from .env'
7
- task :dotenv do
8
- Dotenv.load ".env.#{Rails.env}", '.env'
9
- end
10
- end
11
- end
12
- end
13
-
14
- Dotenv.load ".env.#{Rails.env}", '.env'
data/lib/dotenv-rails.rb DELETED
@@ -1 +0,0 @@
1
- require 'dotenv/railtie'