class Augeas::Facade
Wrapper class for the augeas library.
Public Class Methods
Source
# File lib/augeas/facade.rb, line 32 def self.create(opts={}, &block) # aug_flags is a bitmask in the underlying library, we add all the # values of the flags which were set to true to the default value # Augeas::NONE (which is 0) aug_flags = defined?(Augeas::NO_ERR_CLOSE) ? Augeas::NO_ERR_CLOSE : Augeas::NONE flags = { :type_check => Augeas::TYPE_CHECK, :no_stdinc => Augeas::NO_STDINC, :no_load => Augeas::NO_LOAD, :no_modl_autoload => Augeas::NO_MODL_AUTOLOAD, :enable_span => Augeas::ENABLE_SPAN } save_modes = { :backup => Augeas::SAVE_BACKUP, :newfile => Augeas::SAVE_NEWFILE, :noop => Augeas::SAVE_NOOP } opts.each_key do |key| if flags.key? key aug_flags |= flags[key] elsif key == :save_mode if save_modes[opts[:save_mode]] aug_flags |= save_modes[opts[:save_mode]] else raise ArgumentError, "Invalid save mode #{opts[:save_mode]}." end elsif key != :root && key != :loadpath raise ArgumentError, "Unknown argument #{key}." end end aug = Augeas::Facade::open3(opts[:root], opts[:loadpath], aug_flags) begin aug.send(:raise_last_error) rescue aug.close raise end if block_given? begin yield aug ensure aug.close end else return aug end end
Public Instance Methods
Source
# File lib/augeas/facade.rb, line 149 def clear(path) augeas_set(path, nil) end
Clear the path, i.e. make its value nil
Source
# File lib/augeas/facade.rb, line 180 def clear_transforms rm("/augeas/load/*") end
Clear all transforms under /augeas/load. If load is called right after this, there will be no files under /files
Source
# File lib/augeas/facade.rb, line 197 def clearm(path, sub) setm(path, sub, nil) end
Source
# File lib/augeas/facade.rb, line 293 def context get('/augeas/context') end
Get path expression context (from /augeas/context)
Source
# File lib/augeas/facade.rb, line 288 def context=(path) set('/augeas/context', path) end
Set path expression context to path (in /augeas/context)
Source
# File lib/augeas/facade.rb, line 144 def defnode(name, expr, value=nil) run_command :augeas_defnode, name, expr, value end
Define the variable name to the result of evaluating expr, which must be a nodeset. If no node matching expr exists yet, one is created and name will refer to it. When a node is created and value is given, the new node’s value is set to value.
Source
# File lib/augeas/facade.rb, line 136 def defvar(name, expr) run_command :augeas_defvar, name, expr end
Evaluate expr and set the variable name to the resulting nodeset. The variable can be used in path expressions as $name. Note that expr is evaluated when the variable is defined, not when it is used.
Source
# File lib/augeas/facade.rb, line 90 def exists(path) run_command :augeas_exists, path end
Return true if there is an entry for this path, false otherwise
Source
# File lib/augeas/facade.rb, line 85 def get(path) run_command :augeas_get, path end
Get the value associated with path.
Source
# File lib/augeas/facade.rb, line 283 def insert(path, label, before) run_command :augeas_insert, path, label, before end
Make label a sibling of path by inserting it directly before or after path. The boolean before determines if label is inserted before or after path.
Source
# File lib/augeas/facade.rb, line 254 def label(path) run_command :augeas_label, path end
Lookup the label associated with path Raises Augeas::NoMatchError if the path node does not exist
Source
# File lib/augeas/facade.rb, line 209 def load begin run_command :augeas_load rescue Augeas::CommandExecutionError => e raise e, "Loading failed. Search the augeas tree in /augeas//error"+ "for the actual errors." end nil end
Load files according to the transforms in /augeas/load or those defined via transform. A transform Foo is represented with a subtree /augeas/load/Foo. Underneath /augeas/load/Foo, one node labeled ‘lens’ must exist, whose value is the fully qualified name of a lens, for example ‘Foo.lns’, and multiple nodes ‘incl’ and ‘excl’ whose values are globs that determine which files are transformed by that lens. It is an error if one file can be processed by multiple transforms.
Source
# File lib/augeas/facade.rb, line 123 def match(path) run_command :augeas_match, path end
Return an Array of all the paths that match the path expression path
Returns an empty Array if no paths were found. Raises an Augeas::InvalidPathError when the path is invalid.
Source
# File lib/augeas/facade.rb, line 231 def mv(src, dst) run_command :augeas_mv, src, dst end
Move node src to dst. src must match exactly one node in the tree. dst must either match exactly one node in the tree, or may not exist yet. If dst exists already, it and all its descendants are deleted. If dst does not exist yet, it and all its missing ancestors are created.
Raises Augeas::NoMatchError if the src node does not exist Raises Augeas::MultipleMatchesError if there were multiple matches in src Raises Augeas::DescendantError if the dst node is a descendant of the src node.
Source
# File lib/augeas/facade.rb, line 261 def rename(path, label) run_command :augeas_rename, path, label end
Rename the label of all nodes matching path to label Raises Augeas::NoMatchError if the path node does not exist Raises Augeas::InvalidLabelError if label is invalid
Source
# File lib/augeas/facade.rb, line 115 def rm(path) run_command :augeas_rm, path end
Remove all nodes matching path expression path and all their children. Raises an Augeas::InvalidPathError when the path is invalid.
Source
# File lib/augeas/facade.rb, line 186 def save begin run_command :augeas_save rescue Augeas::CommandExecutionError => e raise e, 'Saving failed. Search the augeas tree in /augeas//error ' << 'for the actual errors.' end nil end
Write all pending changes to disk. Raises Augeas::CommandExecutionError if saving fails.
Source
# File lib/augeas/facade.rb, line 97 def set(path, *values) values.flatten.each { |v| run_command :augeas_set, path, v } end
Set one or multiple elements to path. Multiple elements are mainly sensible with a path like …/array, since this will append all elements.
Source
# File lib/augeas/facade.rb, line 108 def setm(base, sub, value) run_command :augeas_setm, base, sub, value end
base the base node sub the subtree relative to the base value the value for the nodes
Source
# File lib/augeas/facade.rb, line 240 def span(path) run_command :augeas_span, path end
Get the filename, label and value position in the text of this node
Raises Augeas::NoMatchError if the node could not be found Raises Augeas::NoSpanInfo if the node associated with path doesn’t belong to a file or doesn’t exist
Source
# File lib/augeas/facade.rb, line 248 def srun(text) run_command(:augeas_srun, text) end
Run one or more newline-separated commands specified by text, returns an array of [successful_commands_number, output] or
- -2, output
-
in case ‘quit’ command has been encountered.
Raises Augeas::CommandExecutionError if gets an invalid command
Source
# File lib/augeas/facade.rb, line 275 def text_retrieve(lens, node_in, path, node_out) run_command :augeas_text_retrieve, lens, node_in, path, node_out end
Transform the tree at path into a string lens lens and store it in the node node_out, assuming the tree was initially generated using the value of node node_in. path, node_in and node_out are path expressions.
Source
# File lib/augeas/facade.rb, line 268 def text_store(lens, node, path) run_command :augeas_text_store, lens, node, path end
Use the value of node node as a string and transform it into a tree using the lens lens and store it in the tree at path, which will be overwritten. path and node are path expressions.
Source
# File lib/augeas/facade.rb, line 128 def touch(path) set(path, nil) if match(path).empty? end
Create the path with empty value if it doesn’t exist
Source
# File lib/augeas/facade.rb, line 162 def transform(hash) lens = hash[:lens] name = hash[:name] incl = hash[:incl] excl = hash[:excl] raise ArgumentError, "No lens specified" unless lens raise ArgumentError, "No files to include" unless incl name = lens.split(".")[0].sub("@", "") unless name xfm = "/augeas/load/#{name}/" set(xfm + "lens", lens) set(xfm + "incl[last()+1]", incl) set(xfm + "excl[last()+1]", excl) if excl end
Add a transform under /augeas/load
The HASH can contain the following entries
-
:lens- the name of the lens to use -
:name- a unique name; use the module name of the LENS
when omitted
-
:incl- a list of glob patterns for the files to transform -
:excl- a list of the glob patterns to remove from the
list that matches :incl