2023-10-13 21:16:43 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rexml/document'
|
2023-10-14 00:12:28 -06:00
|
|
|
require 'rexml/formatters/pretty'
|
|
|
|
require_relative '../modules/utils'
|
2023-10-13 21:16:43 -06:00
|
|
|
|
|
|
|
module ConfigMan
|
|
|
|
module Parsers
|
|
|
|
module XML
|
|
|
|
CONFIG_FILE_PATH = File.join(Dir.pwd, '.config').freeze
|
|
|
|
|
2023-10-14 00:54:03 -06:00
|
|
|
# Parse the .config file and return a hash of the configuration values
|
2023-10-13 21:16:43 -06:00
|
|
|
# Parse the .config file and return a hash of the configuration values
|
|
|
|
def self.parse(file_path)
|
|
|
|
raise ArgumentError, "File not found: #{file_path}" unless File.exist?(file_path)
|
|
|
|
|
|
|
|
xml_file = File.new(file_path)
|
|
|
|
document = REXML::Document.new(xml_file)
|
|
|
|
parsed_config = {}
|
|
|
|
|
2023-10-14 00:54:03 -06:00
|
|
|
# Recursive lambda to parse nested elements
|
|
|
|
parse_element = lambda do |element|
|
|
|
|
if element.has_elements?
|
|
|
|
element_hash = {}
|
|
|
|
element.each_element do |child|
|
|
|
|
element_hash[child.name] = parse_element.call(child)
|
|
|
|
end
|
|
|
|
element_hash
|
|
|
|
else
|
|
|
|
element.text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-13 21:16:43 -06:00
|
|
|
document.elements.each('config/*') do |element|
|
2023-10-14 00:54:03 -06:00
|
|
|
parsed_config[element.name] = parse_element.call(element)
|
2023-10-13 21:16:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
raise ArgumentError, "Invalid XML format in #{file_path}" unless parsed_config.is_a?(Hash)
|
|
|
|
|
|
|
|
parsed_config
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update(key, new_value)
|
|
|
|
existing_config = parse(CONFIG_FILE_PATH)
|
|
|
|
existing_config[key] = new_value
|
|
|
|
|
|
|
|
doc = REXML::Document.new
|
|
|
|
doc.add_element('config')
|
|
|
|
existing_config.each { |k, v| doc.root.add_element(k).text = v }
|
|
|
|
|
2023-10-14 00:12:28 -06:00
|
|
|
formatter = REXML::Formatters::Pretty.new
|
|
|
|
File.open(CONFIG_FILE_PATH, 'w') do |file|
|
|
|
|
formatter.write(doc, file)
|
|
|
|
end
|
2023-10-13 21:16:43 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.write(config_hash)
|
2023-10-14 00:12:28 -06:00
|
|
|
# Access the loaded modules and expected keys from the main class
|
|
|
|
loaded_modules = ConfigMan.used_modules
|
|
|
|
expected_keys = ConfigMan.expected_keys
|
|
|
|
|
|
|
|
# Sort the keys into their respective sections
|
|
|
|
sorted_config = Utils.sort_into_sections(config_hash, expected_keys, loaded_modules)
|
|
|
|
|
2023-10-13 21:16:43 -06:00
|
|
|
doc = REXML::Document.new
|
|
|
|
doc.add_element('config')
|
|
|
|
|
2023-10-14 00:12:28 -06:00
|
|
|
sorted_config.each do |section, section_data|
|
|
|
|
section_element = doc.root.add_element(section)
|
|
|
|
section_data.each { |k, v| section_element.add_element(k).text = v }
|
|
|
|
end
|
|
|
|
|
|
|
|
formatter = REXML::Formatters::Pretty.new
|
|
|
|
File.open(CONFIG_FILE_PATH, 'w') do |file|
|
|
|
|
formatter.write(doc, file)
|
|
|
|
end
|
2023-10-13 21:16:43 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|