Module: LogStash::Util::EnvironmentVariables
- Included in:
- Config::Mixin, Config::Mixin::DSL, Settings
- Defined in:
- lib/logstash/util/environment_variables.rb
Constant Summary collapse
- ENV_PLACEHOLDER_REGEX =
/\${(?<name>[a-zA-Z_.][a-zA-Z0-9_.]*)(:(?<default>[^}]*))?}/
Instance Method Summary collapse
-
#deep_replace(value) ⇒ Object
Recursive method to replace environment variable references in parameters.
-
#replace_env_placeholders(value) ⇒ Object
Replace all environment variable references in ‘value’ param by environment variable value and return updated value Process following patterns : $VAR, $VAR, $VAR:defaultValue.
Instance Method Details
#deep_replace(value) ⇒ Object
Recursive method to replace environment variable references in parameters
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/logstash/util/environment_variables.rb', line 7 def deep_replace(value) if value.is_a?(Hash) value.each do |valueHashKey, valueHashValue| value[valueHashKey.to_s] = deep_replace(valueHashValue) end else if value.is_a?(Array) value.each_index do | valueArrayIndex| value[valueArrayIndex] = deep_replace(value[valueArrayIndex]) end else return replace_env_placeholders(value) end end end |
#replace_env_placeholders(value) ⇒ Object
Replace all environment variable references in ‘value’ param by environment variable value and return updated value Process following patterns : $VAR, $VAR, $VAR:defaultValue
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/logstash/util/environment_variables.rb', line 25 def replace_env_placeholders(value) return value unless value.is_a?(String) value.gsub(ENV_PLACEHOLDER_REGEX) do |placeholder| # Note: Ruby docs claim[1] Regexp.last_match is thread-local and scoped to # the call, so this should be thread-safe. # # [1] http://ruby-doc.org/core-2.1.1/Regexp.html#method-c-last_match name = Regexp.last_match(:name) default = Regexp.last_match(:default) replacement = ENV.fetch(name, default) if replacement.nil? raise LogStash::ConfigurationError, "Cannot evaluate `#{placeholder}`. Environment variable `#{name}` is not set and there is no default value given." end replacement end end |