class CFA::AugeasWriter

The goal of this class is to write the data stored in {AugeasTree} back to Augeas.

It tries to make only the needed changes, as internally Augeas keeps a flag whether data has been modified, and keeps the unmodified parts of the file untouched.

@note internal only, unstable API @api private

Attributes

aug[R]

Public Class Methods

new(aug) click to toggle source

@param aug result of Augeas.create

# File lib/cfa/augeas_parser/writer.rb, line 15
def initialize(aug)
  @aug = aug
end

Public Instance Methods

write(prefix, tree, top_level: true) click to toggle source

Writes the data in tree to a given prefix in Augeas @param prefix [String] where to write tree in Augeas @param tree [CFA::AugeasTree] tree to write

# File lib/cfa/augeas_parser/writer.rb, line 22
def write(prefix, tree, top_level: true)
  @lazy_operations = LazyOperations.new(aug) if top_level
  tree.all_data.each do |entry|
    located_entry = LocatedEntry.new(tree, entry, prefix)
    process_operation(located_entry)
  end
  @lazy_operations.run if top_level
end

Private Instance Methods

modify_entry(located_entry) click to toggle source

Writes value of entry to path and if it has a sub-tree then it calls {#write} on it @param located_entry [LocatedEntry] entry to modify

# File lib/cfa/augeas_parser/writer.rb, line 361
def modify_entry(located_entry)
  value = located_entry.entry_value
  aug.set(located_entry.path, value)
  report_error { aug.set(located_entry.path, value) }
  recurse_write(located_entry)
end
process_operation(located_entry) click to toggle source

Does modification according to the operation defined in {AugeasElement} @param located_entry [LocatedEntry] entry to process

# File lib/cfa/augeas_parser/writer.rb, line 348
def process_operation(located_entry)
  case located_entry.entry[:operation]
  when :add, nil then @lazy_operations.add(located_entry)
  when :remove then @lazy_operations.remove(located_entry)
  when :modify then modify_entry(located_entry)
  when :keep then recurse_write(located_entry)
  else raise "invalid :operation in #{located_entry.inspect}"
  end
end
recurse_write(located_entry) click to toggle source

calls write on entry if entry have sub-tree @param located_entry [LocatedEntry] entry to recursive write

# File lib/cfa/augeas_parser/writer.rb, line 370
def recurse_write(located_entry)
  write(located_entry.path, located_entry.entry_tree, top_level: false)
end
report_error() { || ... } click to toggle source

Calls block and if it failed, raise exception with details from augeas why it failed @yield call to aug that is secured @raise [RuntimeError]

# File lib/cfa/augeas_parser/writer.rb, line 378
def report_error
  return if yield

  error = aug.error
  # zero is no error, so problem in lense
  if aug.error[:code].nonzero?
    raise "Augeas error #{error[:message]}. Details: #{error[:details]}."
  end

  msg = aug.get("/augeas/text/store/error/message")
  location = aug.get("/augeas/text/store/error/lens")
  raise "Augeas serializing error: #{msg} at #{location}"
end