class RCLoadEnv::Loader
A class that loads environment variables from a RuntimeConfig resource.
Attributes
@return [String] The Runtime Config resource name
@return [String] The cloud project
Public Class Methods
Create a loader. Alwaus uses application default credentials.
@param [String] config_name
Name of the runtime config resource @param [Array<String>] exclude Config keys to exclude (optional) @param [Array<String>] include Config keys to include (optional) @param [Boolean] override Whether to override existing environment
variables (default: false)
@param [String,nil] project Optional name of the cloud project. If not
set, attempts to infer one from the environment.
@param [Boolean] debug Whether to print debug output (default: false)
# 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
Public Instance Methods
@private
# File lib/rcloadenv/loader.rb, line 160 def create_service s = Google::Apis::RuntimeconfigV1beta1::CloudRuntimeConfigService.new s.client_options.application_name = "rcloadenv-ruby" s.client_options.application_version = RCLoadEnv::VERSION s.request_options.retries = 3 s.request_options.header ||= {} s.request_options.header["x-goog-api-client"] = \ "gl-ruby/#{RUBY_VERSION} rcloadenv/#{RCLoadEnv::VERSION}" s.authorization = RCLoadEnv::Credentials.default.client s end
@private
# File lib/rcloadenv/loader.rb, line 183 def debug str STDERR.puts "RCLOADENV DEBUG: #{str}" if @debug end
@private
# File lib/rcloadenv/loader.rb, line 152 def default_project ENV["GOOGLE_CLOUD_PROJECT"] || ENV["GCLOUD_PROJECT"] || Google::Cloud.env.project_id || `gcloud config get-value project 2>/dev/null`.strip end
@private
# File lib/rcloadenv/loader.rb, line 188 def error str STDERR.puts "RCLOADENV ERROR: #{str}" end
@private
# File lib/rcloadenv/loader.rb, line 178 def escape_for_dotenv value value.gsub("\\", "\\\\\\\\").gsub("\n", "\\\\n").gsub("$", "\\\\$") end
@private
# File lib/rcloadenv/loader.rb, line 132 def load_raw_variables if project.empty? raise UsageError, "Project name must be provided." end if config_name.empty? raise UsageError, "Config name must be provided." end parent_path = "projects/#{project}/configs/#{config_name}" debug "Loading #{config_name} from project #{project}" response = service.list_project_config_variables \ parent_path, return_values: true raw_variables = {} (response.to_h[:variables] || []).each do |var_info| key = var_info[:name].sub "#{parent_path}/variables/", "" raw_variables[key] = var_info[:text] || var_info[:value] end raw_variables end
@private
# File lib/rcloadenv/loader.rb, line 173 def make_env_key key key.split("/").last.upcase.gsub("-", "_") end
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.
@param [Hash<String,String>] env The environment to modify. @return the modified environment.
# 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
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.
@return [Hash<String,String>] the variables.
# File lib/rcloadenv/loader.rb, line 117 def raw_variables @raw_variables ||= load_raw_variables end
@private
# File lib/rcloadenv/loader.rb, line 122 def service @service ||= create_service end
@private Used to mock the service for testing
# File lib/rcloadenv/loader.rb, line 127 def service= mock @service = mock end
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.
@param [Hash<String,String>] env The environment to modify. @return the modified environment.
# 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