Class: ConfigFilesApi::AugeasParser

Inherits:
Object
  • Object
show all
Defined in:
lib/config_files_api/augeas_parser.rb

Overview

Examples:

read, print, modify and serialize again

require "config_files/augeas_parser"

parser = ConfigFilesApi::AugeasParser.new("sysconfig.lns")
data = parser.parse(File.read("/etc/default/grub"))

puts data["GRUB_DISABLE_OS_PROBER"]
data["GRUB_DISABLE_OS_PROBER"] = "true"
puts parser.serialize(data)

Instance Method Summary collapse

Constructor Details

#initialize(lens) ⇒ AugeasParser

Returns a new instance of AugeasParser.



185
186
187
# File 'lib/config_files_api/augeas_parser.rb', line 185

def initialize(lens)
  @lens = lens
end

Instance Method Details

#parse(raw_string) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/config_files_api/augeas_parser.rb', line 189

def parse(raw_string)
  @old_content = raw_string

  # open augeas without any autoloading and it should not touch disk and
  # load lenses as needed only
  root = load_path = nil
  Augeas.open(root, load_path, Augeas::NO_MODL_AUTOLOAD) do |aug|
    aug.set("/input", raw_string)
    report_error(aug) unless aug.text_store(@lens, "/input", "/store")

    tree = AugeasTree.new
    tree.load_from_augeas(aug, "/store")

    return tree
  end
end

#serialize(data) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/config_files_api/augeas_parser.rb', line 206

def serialize(data)
  # open augeas without any autoloading and it should not touch disk and
  # load lenses as needed only
  root = load_path = nil
  Augeas.open(root, load_path, Augeas::NO_MODL_AUTOLOAD) do |aug|
    aug.set("/input", @old_content || "")
    data.save_to_augeas(aug, "/store")

    res = aug.text_retrieve(@lens, "/input", "/store", "/output")
    report_error(aug) unless res

    return aug.get("/output")
  end
end