Class: LogStash::Modules::CLIParser

Inherits:
Object
  • Object
show all
Includes:
Util::Loggable
Defined in:
lib/logstash/modules/cli_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Loggable

included, #logger, #slow_logger

Constructor Details

#initialize(module_names, module_variables) ⇒ CLIParser

Returns a new instance of CLIParser.



10
11
12
13
14
15
# File 'lib/logstash/modules/cli_parser.rb', line 10

def initialize(module_names, module_variables)
  @output = []
  # The #compact here catches instances when module_variables may be nil or
  # [nil] and sets it to []
  parse_it(module_names, Array(module_variables).compact)
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



9
10
11
# File 'lib/logstash/modules/cli_parser.rb', line 9

def output
  @output
end

Instance Method Details

#get_kv(module_name, unparsed) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/logstash/modules/cli_parser.rb', line 32

def get_kv(module_name, unparsed)
  # Ensure that there is at least 1 equals sign in our variable string
  # Using String#partition to split on the first '='
  # This hackery is to catch the possibility of an equals (`=`) sign
  # in a passphrase, which might result in an incomplete key.  The
  # portion before the first `=` should always be the key, leaving
  # the rest to be the value
  k, op, rest = unparsed.partition('=')
  if rest.size.zero?
    raise LogStash::ConfigLoadingError, I18n.t("logstash.modules.configuration.modules-variables-malformed", :rawvar => (module_name + '.' + unparsed))
  end
  return k.strip, rest.strip
end

#name_splitter(unparsed) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/logstash/modules/cli_parser.rb', line 46

def name_splitter(unparsed)
  # It must have at least `modulename.something`
  module_name, dot, rest = unparsed.partition('.')
  if rest.count('.') >= 1
    return module_name, rest
  else
    raise LogStash::ConfigLoadingError, I18n.t("logstash.modules.configuration.modules-variables-malformed", :rawvar => unparsed)
  end
end

#parse_it(module_list, module_variable_list) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/logstash/modules/cli_parser.rb', line 67

def parse_it(module_list, module_variable_list)
  if module_list.is_a?(Array)
    parse_modules(module_list).each do |module_name|
      @output << parse_vars(module_name, module_variable_list)
    end
  end
end

#parse_modules(module_list) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/logstash/modules/cli_parser.rb', line 17

def parse_modules(module_list)
  parsed_modules = []
  module_list.each do |module_value|
    # Calling --modules but not filling it results in [nil], so skip that.
    next if module_value.nil?
    # Catch if --modules was launched empty but an option/flag (-something)
    # follows immediately after
    if module_value.start_with?('-')
      raise LogStash::ConfigLoadingError, I18n.t("logstash.modules.configuration.modules-invalid-name", :module_name => module_value)
    end
    parsed_modules.concat module_value.split(',')
  end
  parsed_modules
end

#parse_vars(module_name, vars_list) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/logstash/modules/cli_parser.rb', line 56

def parse_vars(module_name, vars_list)
  module_hash = {"name" => module_name}
  vars_list.each do |unparsed|
    extracted_name, modvar = name_splitter(unparsed)
    next if extracted_name != module_name
    k, v = get_kv(extracted_name, modvar)
    module_hash[k] = v
  end
  module_hash
end