Skip to content

Instantly share code, notes, and snippets.

@raphink
Last active July 10, 2018 12:44
Show Gist options
  • Save raphink/f0063d2d3f7db6d753aafd994c1b6911 to your computer and use it in GitHub Desktop.
Save raphink/f0063d2d3f7db6d753aafd994c1b6911 to your computer and use it in GitHub Desktop.
require 'augeas'
aug = Augeas.open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
file = '/tmp/somexml.xml'
aug.transform(
:lens => 'Xml.lns',
:name => 'Xml',
:incl => file,
:excl => [],
)
aug.load!
res = aug.match('//globalVariable').map do |m|
name = aug.get("#{m}/name/#text")
value = aug.get("#{m}/value/#text")
type = aug.get("#{m}/type/#text")
case type
when 'Int'
value = value.to_i
when 'String'
value = value.to_s
else
raise "Unknown type #{type} for value #{value}"
end
[
name,
value,
]
end.to_h
pp res
prop = '/tmp/test.properties'
aug.transform(
:lens => 'Properties.lns',
:name => 'Prop',
:incl => prop,
:excl => [],
)
aug.load!
aug.set('/augeas/context', "/files#{prop}")
props = aug.match('*[label() != "#comment"]').map do |p|
[
aug.label(p),
aug.get(p),
]
end.to_h
pp props
##############################################
prop2 = '/tmp/test2.properties'
aug.transform(
:lens => 'Properties.lns',
:name => 'Prop',
:incl => prop2,
:excl => [],
)
aug.load!
aug.set('/augeas/context', "/files#{prop2}")
prop_h = {
'k1' => 'val1',
'k2' => 'val2',
'k3' => 'val3',
}
prop_h.each { |k, v| aug.set(k, v) }
aug.save!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment