Class: Jets::Dotenv

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/dotenv.rb,
lib/jets/dotenv/ssm.rb,
lib/jets/dotenv/show.rb

Defined Under Namespace

Classes: Show, Ssm

Constant Summary collapse

@@vars =

@@vars cache to prevent multiple calls to Ssm Tricky note: The cache also prevents the second call to Dotenv.load from returning {} vars. Dotenv 3.0 will not return the vars if it has already been loaded in the ENV. We want this side-effect due to the new way Dotenv 3.0 works.

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote = false) ⇒ Dotenv

Returns a new instance of Dotenv.



8
9
10
# File 'lib/jets/dotenv.rb', line 8

def initialize(remote = false)
  @remote = ENV["JETS_ENV_REMOTE"] || remote
end

Class Method Details

.load!(remote = false) ⇒ Object



4
5
6
# File 'lib/jets/dotenv.rb', line 4

def self.load!(remote = false)
  new(remote).load!
end

Instance Method Details

#dotenv_filesObject

dotenv files with the following precedence:

  • .env.development.jets_extra (highest)

  • .env.development.remote (2nd highest, only if JETS_ENV_REMOTE=1)

  • .env.development.local (unless JETS_ENV_REMOTE=1)

  • .env.development

  • .env.local - This file is loaded for all environments except ‘test` or unless JETS_ENV_REMOTE=1

  • .env - The original (lowest)



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jets/dotenv.rb', line 38

def dotenv_files
  files = []

  files << files << root.join(".env.#{Jets.env}.#{Jets.extra}") if Jets.extra
  files << root.join(".env.#{Jets.env}.remote") if @remote
  files << root.join(".env.#{Jets.env}.local") unless @remote
  files << root.join(".env.#{Jets.env}")
  files << root.join(".env.local") unless Jets.env.test? || @remote
  files << root.join(".env")

  files.compact.map(&:to_s)
end

#load!Object



17
18
19
20
21
22
# File 'lib/jets/dotenv.rb', line 17

def load!
  return @@vars if @@vars
  return if on_aws? # this prevents ssm calls if used in dotenv files
  vars = ::Dotenv.load(*dotenv_files)
  @@vars = Ssm.new(vars).interpolate!
end

#on_aws?Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/jets/dotenv.rb', line 24

def on_aws?
  return true if ENV["ON_AWS"]
  !!ENV["_HANDLER"] # https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
end

#rootObject



51
52
53
# File 'lib/jets/dotenv.rb', line 51

def root
  Jets.root || Pathname.new(ENV["JETS_ROOT"] || Dir.pwd)
end