Small refactor to use String#gsub and named captures for text replacements.

The config/mixin_spec.rb specs are passing for me (20 of 20 passing)

(Improves upon #4710)
This commit is contained in:
Jordan Sissel 2016-03-01 14:00:10 -08:00 committed by Suyog Rao
parent 93b6b6eaf9
commit 74fac6e439

View file

@ -38,8 +38,7 @@ module LogStash::Config::Mixin
PLUGIN_VERSION_1_0_0 = LogStash::Util::PluginVersion.new(1, 0, 0) PLUGIN_VERSION_1_0_0 = LogStash::Util::PluginVersion.new(1, 0, 0)
PLUGIN_VERSION_0_9_0 = LogStash::Util::PluginVersion.new(0, 9, 0) PLUGIN_VERSION_0_9_0 = LogStash::Util::PluginVersion.new(0, 9, 0)
ENV_PLACEHOLDER_REGEX = Regexp.new(/\$\w+|\$\{\w+(\:[^}]*)?\}/) ENV_PLACEHOLDER_REGEX = /\$(?<name>\w+)|\$\{(?<name>\w+)(\:(?<default>[^}]*))?\}/
# This method is called when someone does 'include LogStash::Config' # This method is called when someone does 'include LogStash::Config'
def self.included(base) def self.included(base)
@ -149,37 +148,24 @@ module LogStash::Config::Mixin
# Replace all environment variable references in 'value' param by environment variable value and return updated value # Replace all environment variable references in 'value' param by environment variable value and return updated value
# Process following patterns : $VAR, ${VAR}, ${VAR:defaultValue} # Process following patterns : $VAR, ${VAR}, ${VAR:defaultValue}
def replace_env_placeholders(value) def replace_env_placeholders(value)
if (value.is_a?(String)) return value unless value.is_a?(String)
while value =~ ENV_PLACEHOLDER_REGEX #raise ArgumentError, "Cannot replace ENV placeholders on non-strings. Got #{value.class}" if !value.is_a?(String)
valueParts = value.partition(ENV_PLACEHOLDER_REGEX)
placeHolder = valueParts[1]
if placeHolder.start_with?("${")
envVarName = placeHolder.slice(2..placeHolder.length - 2)
else
envVarName = placeHolder.slice(1..placeHolder.length - 1)
end
if envVarName.include?(':')
placeHolderParts = envVarName.split(':')
envVarName = placeHolderParts[0]
defaultValue = placeHolderParts[1] || ""
else
defaultValue = ""
end
envVarValue = ENV[envVarName] value.gsub(ENV_PLACEHOLDER_REGEX) do |placeholder|
if envVarValue.nil? # Note: Ruby docs claim[1] Regexp.last_match is thread-local and scoped to
envVarValue = defaultValue # the call, so this should be thread-safe.
if defaultValue.empty? #
@logger.warn("In plugin '#{self.class.config_name}', " + # [1] http://ruby-doc.org/core-2.1.1/Regexp.html#method-c-last_match
"referenced environment variable '#{envVarName}' does not exist. " + name = Regexp.last_match(:name)
"This reference has been replaced by empty string.") default = Regexp.last_match(:default)
end
end replacement = ENV.fetch(name, default)
value = valueParts[0] << envVarValue << valueParts[2] #if replacement.nil?
@logger.info("Replacing config environment variable '#{placeHolder}' with #{envVarValue}") #raise LogStash::ConfigurationError, "Cannot evaluate `#{placeholder}`. Environment variable `#{name}` is not set and there is no default value given."
end #end
@logger.info? && @logger.info("Replacing config environment variable '#{placeholder}' with `#{replacement}`")
replacement
end end
return value
end # def replace_env_placeholders end # def replace_env_placeholders
module DSL module DSL