Config-Model
view release on metacpan or search on metacpan
lib/Config/Model/BackendMgr.pm view on Meta::CPAN
$self->auto_delete($file_path, \%backend_args)
if $rw_config->{auto_delete} and not $backend_class->skip_open ;
}
return defined $res ? $res : $@ ? 0 : 1;
};
$logger->trace( "registering write $backend in node " . $self->node->name );
$instance->register_write_back( $self->node->location, $backend, $wb );
return;
}
sub auto_delete ($self, $file_path, $args) {
return unless $file_path;
my $perl_data;
$perl_data = $self->node->dump_as_data( full_dump => $args->{full_dump} // 0)
if defined $self->node;
my $size = ref($perl_data) eq 'HASH' ? scalar keys %$perl_data
: ref($perl_data) eq 'ARRAY' ? scalar @$perl_data
: $perl_data ;
if (not $size) {
$logger->info( "Removing $file_path (no data to store)" );
unlink($file_path);
}
return;
}
sub open_file_to_write ($self, $backend, $file_path, $backup) {
my $do_backup = defined $backup;
$backup ||= 'old'; # use old only if defined
$backup = '.' . $backup unless $backup =~ /^\./;
# make sure that parent dir exists before creating file
$file_path->parent->mkpath;
if ( $do_backup and $file_path->is_file ) {
$file_path->copy( $file_path.$backup ) or die "Backup copy failed: $!";
}
$logger->debug("$backend backend opened file $file_path to write");
return $file_path->filehandle(">",":utf8");
}
sub close_file_to_write ($self, $error, $file_path, $file_mode) {
return unless defined $file_path;
if ($error) {
# restore backup and display error
$logger->warn("Error during write, restoring backup data in $file_path" );
$file_path->append_utf8({ truncate => 1 }, $self->file_backup );
$error->rethrow if ref($error) and $error->can('rethrow');
die $error;
}
# TODO: move chmod in a backend role
$file_path->chmod($file_mode) if $file_mode;
# TODO: move in a backend role
# check file size and remove empty files
$file_path->remove if -z $file_path and not -l $file_path;
return;
}
sub is_auto_write_for_type ($self, $type) {
return $self->{auto_write}{$type} || 0;
}
__PACKAGE__->meta->make_immutable;
1;
# ABSTRACT: Load configuration node on demand
__END__
=pod
=encoding UTF-8
=head1 NAME
Config::Model::BackendMgr - Load configuration node on demand
=head1 VERSION
version 2.162
=head1 SYNOPSIS
# Use BackendMgr to write data in Yaml file
# This example requires Config::Model::Backend::Yaml which is now
# shipped outside of Config::Model. Please get it on CPAN
use Config::Model;
# define configuration tree object
my $model = Config::Model->new;
$model->create_config_class(
name => "Foo",
element => [
[qw/foo bar/] => {
type => 'leaf',
value_type => 'string'
},
]
);
$model->create_config_class(
name => "MyClass",
# rw_config spec is used by Config::Model::BackendMgr
rw_config => {
backend => 'yaml',
config_dir => '/tmp/',
file => 'my_class.yml',
auto_create => 1,
lib/Config/Model/BackendMgr.pm view on Meta::CPAN
C<perl_file>: Perl data structure (perl) in a file. See L<Config::Model::DumpAsData>
for details on the data structure. Now handled by L<Config::Model::Backend::PerlFile>
=back
When needed, C<write_back> method can be called on the instance (See
L<Config::Model::Instance>) to store back all configuration information.
=head1 Backend specification
The backend specification is provided as an attribute of a
L<Config::Model::Node> specification. These attributes are optional:
A node without C<rw_config> attribute must rely on another node to
read or save its data.
When needed (usually for the root node), the configuration class is
declared with a C<rw_config> parameter which specifies the read/write
backend configuration.
=head2 Parameters available for all backends
The following parameters are accepted by all backends:
=over 4
=item config_dir
Specify configuration directory. This parameter is optional as the
directory can be hardcoded in the backend class. C<config_dir> beginning
with 'C<~>' is munged so C<~> is replaced by C<< File::HomeDir->my_data >>.
See L<File::HomeDir> for details.
=item file
Specify configuration file name (without the path). This parameter is
optional as the file name can be hardcoded in the backend class.
The configuration file name can be specified with C<&index> keyword
when a backend is associated to a node contained in a hash. For instance,
with C<file> set to C<&index.conf>:
service # hash element
foo # hash index
nodeA # values of nodeA are stored in foo.conf
bar # hash index
nodeB # values of nodeB are stored in bar.conf
Likewise, the keyword C<&element> can be used to specify the file
name. For instance, with C<file> set to C<&element-&index.conf>:
service # hash element
foo # hash index
nodeA # values of nodeA are stored in service.foo.conf
bar # hash index
nodeB # values of nodeB are stored in service.bar.conf
=item file_mode
C<file_mode> parameter can be used to set the mode of the written
file(s). C<file_mode> value can be in any form supported by
L<Path::Tiny/chmod>. Example:
file_mode => 0664,
file_mode => '0664',
file_mode => 'g+w'
=item os_config_dir
Specify alternate location of a configuration directory depending on the OS
(as returned by C<$^O>, see L<perlport/PLATFORMS>).
For instance:
config_dir => '/etc/ssh',
os_config_dir => { darwin => '/etc' }
=item default_layer
Optional. Specifies where to find a global configuration file that
specifies default values. For instance, this is used by OpenSSH to
specify a global configuration file (C</etc/ssh/ssh_config>) that is
overridden by user's file:
default_layer => {
os_config_dir => { 'darwin' => '/etc' },
config_dir => '/etc/ssh',
file => 'ssh_config'
}
Only the 3 above parameters can be specified in C<default_layer>.
=item auto_create
By default, an exception is thrown if no read was
successful. This behavior can be overridden by specifying
C<< auto_create => 1 >> in one of the backend specification. For instance:
rw_config => {
backend => 'IniFile',
config_dir => '/tmp',
file => 'foo.conf',
auto_create => 1
},
Setting C<auto_create> to 1 is necessary to create a configuration
from scratch
=item auto_delete
Delete configuration files that contains no data. (default is to leave an empty file)
=back
=head2 Config::Model::Backend::* backends
Specify the backend name and the parameters of the backend defined
in their documentation.
For instance:
rw_config => {
backend => 'yaml',
( run in 0.883 second using v1.01-cache-2.11-cpan-39bf76dae61 )