Config-Augeas

 view release on metacpan or  search on metacpan

lib/Config/Augeas.pm  view on Meta::CPAN

=head1 NAME

Config::Augeas - Edit configuration files through Augeas C library

=head1 SYNOPSIS

  use Config::Augeas;

  my $aug = Config::Augeas->new( root => $aug_root ) ;

  my $ret = $aug->get("/files/etc/hosts/1/ipaddr") ;
  $aug->set("/files/etc/hosts/2/ipaddr","192.168.0.1") ;

  my @a = $aug->match("/files/etc/hosts/") ;

  my $nb = $aug->count_match("/files/etc/hosts/") ;

  $aug->save ;

=head1 DESCRIPTION

=for comment
Description snatched from Augeas README

Augeas is a library and command line tool that focuses on the most
basic problem in handling Linux configurations programmatically:
editing actual configuration files in a controlled manner.

To that end, Augeas exposes a tree of all configuration settings
(well, all the ones it knows about) and a simple local API for
manipulating the tree. Augeas then modifies underlying configuration
files according to the changes that have been made to the tree; it
does as little modeling of configurations as possible, and focuses
exclusively on transforming the tree-oriented syntax of its public API
to the myriad syntaxes of individual configuration files.

This module provides an object oriented Perl interface for Augeas
configuration edition library with a more "perlish" API than Augeas C
counterpart.

=head1 Constructor

=head1 new ( ... )

Creates a new Config::Augeas object. Optional parameters are:

=over

=item loadpath

a colon-spearated list of directories that lenses should be searched
in. This is in addition to the standard load path and the directories
in specified C<AUGEAS_LENS_LIB> environment variable.

=item root

Use C<root> as the filesystem root. If not specified, use the value of
the environment variable C<AUGEAS_ROOT>. If that doesn't exist either,
use "C</>".

=item save => backup | newfile | noop

Specify how to save the configuration file. Either create a newfile
(with extension C<.augnew>, and do not overwrite the original file) or
move the original file into a backup file (C<.augsave> extension).
C<noop> make saves a no-op process, just record what would have
changed

=item type_check => 1

Typecheck lenses; since it can be very expensive it is not done by
default.

=item no_std_inc

Do not use the builtin load path for modules

=item no_load

Do not load the tree from AUG_INIT

=back

=cut

sub new {
    my $type = shift ;
    my $self = {} ;
    my %args = @_ ;
    my $flags = 0 ;
    my $loadpath = delete $args{loadpath} || '';
    my $root  = delete $args{root} || '';

    my $save = delete $args{save} || '';
    if    ($save eq 'backup')  { $flags |= &AUG_SAVE_BACKUP }
    elsif ($save eq 'newfile') { $flags |= &AUG_SAVE_NEWFILE }
    elsif ($save =~ 'noop')    { $flags |= &AUG_SAVE_NOOP }
    elsif ($save) { 
	croak  __PACKAGE__," new: unexpected save value: $save. ",
	  "Expected backup or newfile";
    }

    $flags |= &AUG_TYPE_CHECK  if ( delete $args{type_check}  || 0 );
    $flags |= &AUG_NO_STDINC   if ( delete $args{no_std_inc}  || 0 ) ;
    $flags |= &AUG_NO_LOAD     if ( delete $args{no_load}     || 0 ) ;
    $flags |= &AUG_ENABLE_SPAN if ( delete $args{enable_span} || 0 ) ;

    croak  __PACKAGE__," new: unexpected parameters: ",
      join (' ',keys %args) 
	if %args ;

    $self->{aug_c} = Config::Augeas::init($root,$loadpath,$flags) ;

    bless $self,$type ;

    return $self
}

=head1 Methods

=head2 defvar( name, [ expr ])

Define a variable C<name> whose value is the result of evaluating
C<expr>. If a variable C<name> already exists, its name will be replaced
with the result of evaluating C<expr>.

If C<expr> is omitted, the variable C<name> will be removed if it is
defined.

Path variables can be used in path expressions later on by prefixing
them with '$'.

Returns -1 on error; on success, returns 0 if C<expr> evaluates to anything
other than a nodeset, and the number of nodes if C<expr> evaluates to a
nodeset

=cut

sub defvar {
    my $self = shift ;
    my $name = shift || croak __PACKAGE__," defvar: undefined name";
    my $expr = shift || 0 ;

    return $self->{aug_c} -> defvar($name, $expr) ;
}

=head2 defnode ( name, expr, value )

Define a variable C<name> whose value is the result of evaluating
C<expr>, which must evaluate to a nodeset. If a variable C<name>
already exists, its name will be replaced with the result of
evaluating C<expr>.

If C<expr> evaluates to an empty nodeset, a node is created, equivalent to
calling C<set( expr, value)> and C<name> will be the nodeset containing
that single node.

Returns undef on error

Returns an array containing:

lib/Config/Augeas.pm  view on Meta::CPAN


Example:

  my $span = $aug->span('/files/etc/passwd/root') ;
  # If filename is undefined, there are no valid span information for this node
  if ($span->{filename}) {
     print "Found root in passwd at character $span->{span_start}\n" ;
  }

WARNING: You must check that $span->{filename} is defined. If it isn't,
the node has no span information and all other values in the hash are wrong.

=cut

sub span {
    my $self = shift ;
    my $path = shift || croak __PACKAGE__," span: undefined path";

    return $self->{aug_c} -> span($path) ;

}

=head2 match ( pattern )

Returns an array of the elements that match of the path expression
C<pattern>. The returned paths are sufficiently qualified to make sure
that they match exactly one node in the current tree.

=cut

sub match {
    my $self = shift ;
    my $pattern = shift || croak __PACKAGE__," match: undefined pattern";

    # Augeas 0.4.0 is a little picky about trailing slashes
    $pattern =~ s!/$!!;
    return $self->{aug_c} -> match($pattern) ;

}

=head2 count_match ( pattern )

Same as match but return the number of matching element in manner more
efficient than using C<scalar match( pattern )>

=cut

sub count_match {
    my $self = shift ;
    my $pattern = shift || croak __PACKAGE__," count_match: undefined pattern";

    # Augeas 0.4.0 is a little picky about trailing slashes
    $pattern =~ s!/$!!;
    return $self->{aug_c} -> count_match($pattern) ;
}

=head2 save

Write all pending changes to disk. Return 0 if an error is
encountered, 1 on success. Only files that had any changes made to
them are written. C<save> will follow backup files as specified with
Config::Augeas::new C<backup> parameter.

=cut

sub save {
    my $self   = shift ;
    my $ret = $self->{aug_c} -> save() ;
    return $ret == 0 ? 1 : 0 ;
}

=head2 load

Load files into the tree. Which files to load and what lenses to use on
them is specified under C</augeas/load> in the tree; each entry
C</augeas/load/NAME> specifies a 'transform', by having itself exactly one
child 'lens' and any number of children labelled 'incl' and 'excl'. The
value of NAME has no meaning.

The 'lens' grandchild of C</augeas/load> specifies which lens to use, and
can either be the fully qualified name of a lens 'Module.lens' or
'C<@Module>'. The latter form means that the lens from the transform marked
for autoloading in C<MODULE> should be used.

The 'incl' and 'excl' grandchildren of C</augeas/load> indicate which files
to transform. Their value are used as glob patterns. Any file that
matches at least one 'incl' pattern and no 'excl' pattern is
transformed. The order of 'incl' and 'excl' entries is irrelevant.

When L<init> is first called, it populates C</augeas/load> with the
transforms marked for autoloading in all the modules it finds.

Before loading any files, C<load> will remove everything underneath
C</augeas/files> and C</files>, regardless of whether any entries have been
modified or not.

Returns 0 on error, 1 on success. Note that success includes the case
where some files could not be loaded. Details of such files can be found
as 'C</augeas//error>'.

=cut

sub load {
    my $self   = shift ;
    my $ret = $self->{aug_c} -> load() ;
    return $ret == 0 ? 1 : 0 ;
}

=head2 print ( [ path  , [ file ] ] )

Print each node matching C<path> and its descendants on STDOUT or in a file

The second parameter can be :

=over

=item *

A file name. 

=item *



( run in 0.737 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )