Class: LegacyFacter::Util::Parser::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/custom_facts/util/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, content = nil) ⇒ Base

Returns a new instance of Base.



48
49
50
51
# File 'lib/facter/custom_facts/util/parser.rb', line 48

def initialize(filename, content = nil)
  @filename = filename
  @content  = content
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



46
47
48
# File 'lib/facter/custom_facts/util/parser.rb', line 46

def filename
  @filename
end

Instance Method Details

#contentObject



53
54
55
# File 'lib/facter/custom_facts/util/parser.rb', line 53

def content
  @content ||= Facter::Util::FileHelper.safe_read(filename, nil)
end

#log_stderr(msg, command, file) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/facter/custom_facts/util/parser.rb', line 81

def log_stderr(msg, command, file)
  return if !msg || msg.empty?

  file_name = file.split('/').last
  logger = Facter::Log.new(file_name)

  logger.warn(format(STDERR_MESSAGE, command, msg.strip))
end

#parse_executable_output(output) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/facter/custom_facts/util/parser.rb', line 70

def parse_executable_output(output)
  res = nil
  begin
    res = YAML.safe_load(output, [Symbol, Time])
  rescue StandardError => e
    Facter.debug("Could not parse executable fact output as YAML or JSON (#{e.message})")
  end
  res = KeyValuePairOutputFormat.parse output unless res.is_a?(Hash)
  res
end

#parse_resultsObject

Raises:

  • (ArgumentError)


66
67
68
# File 'lib/facter/custom_facts/util/parser.rb', line 66

def parse_results
  raise ArgumentError, 'Subclasses must respond to parse_results'
end

#resultsObject

results on the base class is really meant to be just an exception handler wrapper.



59
60
61
62
63
64
# File 'lib/facter/custom_facts/util/parser.rb', line 59

def results
  parse_results
rescue StandardError => e
  Facter.log_exception(e, "Failed to handle #{filename} as #{self.class} facts: #{e.message}")
  nil
end