lib/custom_facts/util/parser.rb in facter-4.0.20 vs lib/custom_facts/util/parser.rb in facter-4.0.21
- old
+ new
@@ -2,11 +2,10 @@
# This class acts as the factory and parent class for parsed
# facts such as scripts, text, json and yaml files.
#
# Parsers must subclass this class and provide their own #results method.
-require 'yaml'
module LegacyFacter
module Util
module Parser
@parsers = []
@@ -91,12 +90,21 @@
end
result
end
end
+ # This regex was taken from Psych and adapted
+ # https://github.com/ruby/psych/blob/d2deaa9adfc88fc0b870df022a434d6431277d08/lib/psych/scalar_scanner.rb#L9
+ # It is used to detect Time in YAML, but we use it to wrap time objects in quotes to be treated as strings.
+ TIME =
+ /(\d{4}-\d{1,2}-\d{1,2}(?:[Tt]|\s+)\d{1,2}:\d\d:\d\d(?:\.\d*)?(?:\s*(?:Z|[-+]\d{1,2}:?(?:\d\d)?))?\s*$)/.freeze
+
class YamlParser < Base
def parse_results
- YAML.safe_load(content)
+ # Add quotes to Yaml time
+ cont = content.gsub(TIME, '"\1"')
+
+ YAML.safe_load(cont, [Date])
end
end
register(YamlParser) do |filename|
extension_matches?(filename, 'yaml')