Class: RCLoadEnv::Loader
- Inherits:
-
Object
- Object
- RCLoadEnv::Loader
- Defined in:
- lib/rcloadenv/loader.rb
Overview
A class that loads environment variables from a RuntimeConfig resource.
Defined Under Namespace
Classes: UsageError
Instance Attribute Summary collapse
-
#config_name ⇒ String
readonly
The Runtime Config resource name.
-
#project ⇒ String
readonly
The cloud project.
Instance Method Summary collapse
-
#initialize(config_name, exclude: [], include: [], override: false, project: nil, debug: false) ⇒ Loader
constructor
Create a loader.
-
#modify_env(env = {}) ⇒ Object
Modify the given environment with the configuration.
-
#raw_variables ⇒ Hash<String,String>
Returns the hash of variables retrieved from the runtime config.
-
#write_dotenv(io = STDOUT) ⇒ Object
Modify the given environment with the configuration.
Constructor Details
#initialize(config_name, exclude: [], include: [], override: false, project: nil, debug: false) ⇒ Loader
Create a loader. Alwaus uses application default credentials.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rcloadenv/loader.rb', line 45 def initialize config_name, exclude: [], include: [], override: false, project: nil, debug: false @config_name = config_name.to_s @project = (project || default_project).to_s @exclude = exclude @include = include @override = override @debug = debug end |
Instance Attribute Details
#config_name ⇒ String (readonly)
Returns The Runtime Config resource name.
57 58 59 |
# File 'lib/rcloadenv/loader.rb', line 57 def config_name @config_name end |
#project ⇒ String (readonly)
Returns The cloud project.
59 60 61 |
# File 'lib/rcloadenv/loader.rb', line 59 def project @project end |
Instance Method Details
#modify_env(env = {}) ⇒ Object
Modify the given environment with the configuration. The given hash is modified in place and returned. If no hash is provided, a new one is created and returned.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rcloadenv/loader.rb', line 69 def modify_env env={} raw_variables.each do |k, v| if !@exclude.empty? && @exclude.include?(k) || !@include.empty? && !@include.include?(k) debug "Skipping config variable #{k}" else debug "Found config variable #{k}" key = make_env_key k if !env.include?(key) debug "Setting envvar: #{key}" env[key] = v elsif @override debug "Overriding envvar: #{key}" env[key] = v else debug "Envvar already set: #{key}" end end end env end |
#raw_variables ⇒ Hash<String,String>
Returns the hash of variables retrieved from the runtime config. Variable names have the parent (project and config name) stripped. Values are all converted to strings.
117 118 119 |
# File 'lib/rcloadenv/loader.rb', line 117 def raw_variables @raw_variables ||= load_raw_variables end |
#write_dotenv(io = STDOUT) ⇒ Object
Modify the given environment with the configuration. The given hash is modified in place and returned. If no hash is provided, a new one is created and returned.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rcloadenv/loader.rb', line 99 def write_dotenv io=STDOUT raw_variables.each do |k, v| key = make_env_key k if key =~ /^[\w\.]+$/ io.puts "#{key}=\"#{escape_for_dotenv v}\"" else error "Bad key: #{key}" end end end |