| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Config-Model | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2005-2022 by Dominique Dumont. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # The GNU Lesser General Public License, Version 2.1, February 1999 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | package Config::Model::Backend::PlainFile 2.169; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use 5.10.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Config::Model::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | extends 'Config::Model::Backend::Any'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | with "Config::Model::Role::ComputeFunction"; | |||||||||||||||||||||||||||
| 22 | 3 | with "Config::Model::Role::FileHandler"; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use feature qw/postderef signatures/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | no warnings qw/experimental::postderef experimental::signatures/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | my $logger = get_logger("Backend::PlainFile"); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 29 | ▶ | sub annotation { return 0; } | ||||||||||||||||||||||||||
|
Subroutine
annotation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # remember that a read backend (and its config file(s)) is attached to a node | ||||||||||||||||||||||||||||
| 32 | # OTOH, PlainFile backend deal with files that are attached to elements of a node. | ||||||||||||||||||||||||||||
| 33 | # Hence the files must not be managed by backend manager. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # file not opened by BackendMgr | ||||||||||||||||||||||||||||
| 36 | # file_path is undef | ||||||||||||||||||||||||||||
| 37 | 85 | ▶ | sub skip_open { return 1; } | ||||||||||||||||||||||||||
|
Subroutine
skip_open: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 90 | ▶ | sub get_file_name { | ||||||||||||||||||||||||||
|
Subroutine
get_file_name: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 90 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 90 | my $obj = $args{object}->fetch_element( name => $args{elt} ); | |||||||||||||||||||||||||||
| 43 | 90 | ▶ | return $args{file} ? $obj->compute_string($args{file}) : $args{elt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||||||||||||||||||||||||||||
| 47 | 29 | ▶ | sub read ($self, %args) { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | # args are: | ||||||||||||||||||||||||||||
| 49 | # object => $obj, # Config::Model::Node object | ||||||||||||||||||||||||||||
| 50 | # root => './my_test', # fake root directory, userd for tests | ||||||||||||||||||||||||||||
| 51 | # config_dir => /etc/foo', # absolute path | ||||||||||||||||||||||||||||
| 52 | # file => 'foo.conf', # file name | ||||||||||||||||||||||||||||
| 53 | # file_path => './my_test/etc/foo/foo.conf' | ||||||||||||||||||||||||||||
| 54 | # check => yes|no|skip | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 29 | ▶ | my $check = $args{check} || 'yes'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'check'} || 'yes'
MC/DC0%0/1
$args{'check'} || 'yes'
$args{'check'}
Truth table50%1/2
A
A= $args{'check'}
| |||||||||||||||||||||||||||||
| 57 | 29 | my $node = $args{object}; | |||||||||||||||||||||||||||
| 58 | 29 | $logger->trace( "called on node ", $node->name ); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # read data from leaf element from the node | ||||||||||||||||||||||||||||
| 61 | # do not trigger warp when getting element names | ||||||||||||||||||||||||||||
| 62 | 29 | foreach my $elt ( $node->get_element_names(all => 1) ) { | |||||||||||||||||||||||||||
| 63 | 58 | my $obj = $args{object}->fetch_element( name => $elt ); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 58 | my $file_name = $self->get_file_name(%args, elt => $elt); | |||||||||||||||||||||||||||
| 66 | 58 | my $dir = $self->get_tuned_config_dir(%args); | |||||||||||||||||||||||||||
| 67 | 58 | my $file = $dir->child($file_name); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 58 | $logger->trace("looking to read plainfile $file for ", $obj->location); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 58 | my $type = $obj->get_type; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 58 | ▶ | if ( $type eq 'leaf' ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 74 | 31 | $self->read_leaf( $obj, $elt, $check, $file, \%args ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | elsif ( $type eq 'list' ) { | ||||||||||||||||||||||||||||
| 77 | 27 | $self->read_list( $obj, $elt, $check, $file, \%args ); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | elsif ( $type eq 'hash' ) { | ||||||||||||||||||||||||||||
| 80 | 0 | $self->read_hash( $obj, $elt, $check, $file, \%args ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||
| 83 | 0 | $logger->debug("PlainFile read skipped $type $elt"); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 29 | return 1; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 31 | ▶ | sub read_leaf { | ||||||||||||||||||||||||||
|
Subroutine
read_leaf: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 31 | my ( $self, $obj, $elt, $check, $file, $args ) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 31 | ▶ | return unless $file->exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 4 | my $v = $file->slurp_utf8; | |||||||||||||||||||||||||||
| 97 | 4 | ▶ | chomp($v) unless $obj->value_type eq 'string'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | if ($logger->is_trace) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | (my $str = $v) =~ s/\n.*/[...]/s; | |||||||||||||||||||||||||||
| 100 | 0 | $logger->trace("storing leaf value '$str' from $file "); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 4 | $obj->store( value => $v, check => $check ); | |||||||||||||||||||||||||||
| 103 | 4 | return; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 27 | ▶ | sub read_list { | ||||||||||||||||||||||||||
|
Subroutine
read_list: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 27 | my ( $self, $obj, $elt, $check, $file, $args ) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 27 | ▶ | return unless $file->exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 24 | my @v = $file->lines_utf8({ chomp => 1}); | |||||||||||||||||||||||||||
| 112 | 24 | $logger->trace("storing list value @v from $file "); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 24 | $obj->store_set(@v); | |||||||||||||||||||||||||||
| 115 | 24 | return; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub read_hash { | ||||||||||||||||||||||||||
|
Subroutine
read_hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 0 | my ( $self, $obj, $elt, $check, $file, $args ) = @_; | |||||||||||||||||||||||||||
| 120 | 0 | $logger->debug("PlainFile read skipped hash $elt"); | |||||||||||||||||||||||||||
| 121 | 0 | return; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 13 | ▶ | sub write ($self, %args){ | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | # args are: | ||||||||||||||||||||||||||||
| 126 | # object => $obj, # Config::Model::Node object | ||||||||||||||||||||||||||||
| 127 | # root => './my_test', # fake root directory, userd for tests | ||||||||||||||||||||||||||||
| 128 | # config_dir => /etc/foo', # absolute path read | ||||||||||||||||||||||||||||
| 129 | # file => 'foo.conf', # file name | ||||||||||||||||||||||||||||
| 130 | # file_path => './my_test/etc/foo/foo.conf' | ||||||||||||||||||||||||||||
| 131 | # check => yes|no|skip | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 13 | ▶ | my $check = $args{check} || 'yes'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'check'} || 'yes'
MC/DC0%0/1
$args{'check'} || 'yes'
$args{'check'}
Truth table50%1/2
A
A= $args{'check'}
| |||||||||||||||||||||||||||||
| 134 | 13 | my $cfg_dir = $args{config_dir}; | |||||||||||||||||||||||||||
| 135 | 13 | my $dir = $self->get_tuned_config_dir(%args); | |||||||||||||||||||||||||||
| 136 | 13 | ▶ | $dir->mkpath({ mode => oct(755) } ) unless $dir->is_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 13 | my $node = $args{object}; | |||||||||||||||||||||||||||
| 139 | 13 | $logger->debug( "PlainFile write called on node ", $node->name ); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # write data from leaf element from the node | ||||||||||||||||||||||||||||
| 142 | 13 | foreach my $elt ( $node->get_element_name() ) { | |||||||||||||||||||||||||||
| 143 | 26 | my $obj = $args{object}->fetch_element( name => $elt ); | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 26 | my $file_name = $self->get_file_name(%args, elt => $elt); | |||||||||||||||||||||||||||
| 146 | 26 | my $file = $dir->child($file_name); | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 26 | $logger->trace("looking to write plainfile $file for ", $obj->location); | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 26 | my $type = $obj->get_type; | |||||||||||||||||||||||||||
| 151 | 26 | my @v; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 26 | ▶ | if ( $type eq 'leaf' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 154 | 14 | my $v = $obj->fetch( check => $args{check} ); | |||||||||||||||||||||||||||
| 155 | 14 | ▶ | $v .= "\n" if defined $v and $obj->value_type ne 'string'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $v and $obj->value_type ne "string"
MC/DC50%1/2
defined $v and $obj->value_type ne "string"
defined $v
$obj->value_type ne "string"
Truth table66%2/3
A and B
A= defined $vB= $obj->value_type ne "string"
| |||||||||||||||||||||||||||||
| 156 | 14 | ▶ | push @v, $v if defined $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | elsif ( $type eq 'list' ) { | ||||||||||||||||||||||||||||
| 159 | 12 | @v = map { "$_\n" } $obj->fetch_all_values; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | else { | ||||||||||||||||||||||||||||
| 162 | 0 | $logger->debug("PlainFile write skipped $type $elt"); | |||||||||||||||||||||||||||
| 163 | 0 | next; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 26 | ▶ | if (@v) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 167 | 14 | $logger->trace("PlainFile write opening $file to write $elt"); | |||||||||||||||||||||||||||
| 168 | 14 | $file->spew_utf8(@v); | |||||||||||||||||||||||||||
| 169 | 14 | ▶ | $file->chmod($args{file_mode}) if $args{file_mode}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | elsif ($file->exists) { | ||||||||||||||||||||||||||||
| 172 | 1 | $logger->trace("PlainFile delete $file"); | |||||||||||||||||||||||||||
| 173 | 1 | $file->remove; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 13 | return 1; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||||||||||||||||||||||||||||
| 181 | 3 | ▶ | sub delete ($self, %args) { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 182 | # args are: | ||||||||||||||||||||||||||||
| 183 | # object => $obj, # Config::Model::Node object | ||||||||||||||||||||||||||||
| 184 | # root => './my_test', # fake root directory, userd for tests | ||||||||||||||||||||||||||||
| 185 | # config_dir => /etc/foo', # absolute path read | ||||||||||||||||||||||||||||
| 186 | # file => 'foo.conf', # file name | ||||||||||||||||||||||||||||
| 187 | # file_path => './my_test/etc/foo/foo.conf' | ||||||||||||||||||||||||||||
| 188 | # check => yes|no|skip | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 3 | my $dir = $self->get_tuned_config_dir(%args); | |||||||||||||||||||||||||||
| 191 | 3 | my $node = $args{object}; | |||||||||||||||||||||||||||
| 192 | 3 | $logger->debug( "PlainFile delete called on deleted node"); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # write data from leaf element from the node | ||||||||||||||||||||||||||||
| 195 | 3 | foreach my $elt ( $node->get_element_name() ) { | |||||||||||||||||||||||||||
| 196 | 6 | my $obj = $node->fetch_element( name => $elt ); | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 6 | my $file_name = $self->get_file_name(%args, elt => $elt); | |||||||||||||||||||||||||||
| 199 | 6 | my $file = $dir->child( $file_name ); | |||||||||||||||||||||||||||
| 200 | 6 | $logger->info( "Removing $file (deleted node)" ); | |||||||||||||||||||||||||||
| 201 | 6 | $file->remove; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 3 | return; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 3 | ▶ | no Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 207 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 3 | 1; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # ABSTRACT: Read and write config as plain file | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | __END__ | ||||||||||||||||||||||||||||