| 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::ShellVar 2.169; | ||||||||||||
| 11 | |||||||||||||
| 12 | 3 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 3 | ▶ | use Mouse; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 3 | ▶ | use Config::Model::Exception; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 3 | ▶ | use File::Path; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 3 | ▶ | use Log::Log4perl qw(get_logger :levels); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 3 | ▶ | use Config::Model::BackendTrackOrder; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 3 | ▶ | use 5.020; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | 3 | ▶ | use feature qw/postderef signatures/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 3 | ▶ | no warnings qw/experimental::postderef experimental::signatures/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | |||||||||||||
| 23 | 3 | extends 'Config::Model::Backend::Any'; | |||||||||||
| 24 | |||||||||||||
| 25 | 3 | my $logger = get_logger("Backend::ShellVar"); | |||||||||||
| 26 | |||||||||||||
| 27 | 3 | has tracker => ( | |||||||||||
| 28 | is => 'ro', | ||||||||||||
| 29 | isa => 'Config::Model::BackendTrackOrder', | ||||||||||||
| 30 | lazy_build => 1, | ||||||||||||
| 31 | handles => [qw/get_ordered_element_names/], | ||||||||||||
| 32 | ); | ||||||||||||
| 33 | |||||||||||||
| 34 | 8 | ▶ | sub _build_tracker { | ||||||||||
|
Subroutine
_build_tracker: called CC=1 SCAR=0.0
| |||||||||||||
| 35 | 8 | my $self = shift; | |||||||||||
| 36 | 8 | return Config::Model::BackendTrackOrder->new( | |||||||||||
| 37 | backend_obj => $self, | ||||||||||||
| 38 | node => $self->node, | ||||||||||||
| 39 | ) ; | ||||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | 8 | ▶ | sub annotation { return 1; } | ||||||||||
|
Subroutine
annotation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 43 | |||||||||||||
| 44 | ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||||||||||||
| 45 | 8 | ▶ | sub read ($self, %args) { | ||||||||||
|
Subroutine
read: called CC=7 SCAR=21.0
Pod
documented
| |||||||||||||
| 46 | # args are: | ||||||||||||
| 47 | # object => $obj, # Config::Model::Node object | ||||||||||||
| 48 | # root => './my_test', # fake root directory, userd for tests | ||||||||||||
| 49 | # config_dir => /etc/foo', # absolute path | ||||||||||||
| 50 | # file => 'foo.conf', # file name | ||||||||||||
| 51 | # file_path => './my_test/etc/foo/foo.conf' | ||||||||||||
| 52 | # check => yes|no|skip | ||||||||||||
| 53 | |||||||||||||
| 54 | 8 | ▶ | return 0 unless $args{file_path}->exists; # no file to read | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | 8 | ▶ | 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'}
| |||||||||||||
| 56 | |||||||||||||
| 57 | 8 | my @lines = $args{file_path}->lines_utf8; | |||||||||||
| 58 | |||||||||||||
| 59 | # try to get global comments (comments before a blank line) | ||||||||||||
| 60 | 8 | $self->read_global_comments( \@lines, '#' ); | |||||||||||
| 61 | |||||||||||||
| 62 | 8 | my @assoc = $self->associates_comments_with_data( \@lines, '#' ); | |||||||||||
| 63 | 8 | foreach my $item (@assoc) { | |||||||||||
| 64 | 26 | my ( $data, $c ) = @$item; | |||||||||||
| 65 | 26 | my ($k,$v) = split /\s*=\s*/, $data, 2; # make reader quite tolerant | |||||||||||
| 66 | 26 | $v =~ s/^["']|["']$//g; | |||||||||||
| 67 | 26 | ▶ | if ($logger->is_debug) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | my $msg = "Loading key '$k' value '$v'"; | |||||||||||
| 69 | 0 | ▶ | $msg .= " comment: '$c'" if $c; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 0 | $logger->debug($msg); | |||||||||||
| 71 | } | ||||||||||||
| 72 | 26 | $self->tracker->register_element($k); | |||||||||||
| 73 | 26 | my $obj = $self->node->fetch_element($k); | |||||||||||
| 74 | 26 | $obj->store( value => $v, check => $check ); | |||||||||||
| 75 | 26 | ▶ | $obj->annotation($c) if $c; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | 8 | return 1; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 7 | ▶ | sub write ($self, %args) { | ||||||||||
|
Subroutine
write: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||
| 82 | # args are: | ||||||||||||
| 83 | # object => $obj, # Config::Model::Node object | ||||||||||||
| 84 | # root => './my_test', # fake root directory, userd for tests | ||||||||||||
| 85 | # config_dir => /etc/foo', # absolute path | ||||||||||||
| 86 | # file => 'foo.conf', # file name | ||||||||||||
| 87 | # file_path => './my_test/etc/foo/foo.conf' | ||||||||||||
| 88 | # check => yes|no|skip | ||||||||||||
| 89 | |||||||||||||
| 90 | 7 | my $node = $args{object}; | |||||||||||
| 91 | |||||||||||||
| 92 | 7 | my @to_write; | |||||||||||
| 93 | |||||||||||||
| 94 | # Using Config::Model::ObjTreeScanner would be overkill | ||||||||||||
| 95 | 7 | foreach my $elt ( $self->get_ordered_element_names ) { | |||||||||||
| 96 | 49 | my $obj = $node->fetch_element($elt); | |||||||||||
| 97 | 49 | my $v = $node->grab_value($elt); | |||||||||||
| 98 | |||||||||||||
| 99 | 49 | ▶ | next unless defined $v; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | |||||||||||||
| 101 | 25 | push @to_write, [ qq!$elt="$v"!, $obj->annotation ]; | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | 7 | ▶ | if (@to_write) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 105 | 7 | my $res = $self->write_global_comment( '#' ); | |||||||||||
| 106 | 7 | foreach my $line_ref (@to_write) { | |||||||||||
| 107 | 25 | $res .= $self->write_data_and_comments( '#', @$line_ref ); | |||||||||||
| 108 | } | ||||||||||||
| 109 | 7 | $args{file_path}->spew_utf8($res); | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 7 | return 1; | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | 3 | ▶ | no Mouse; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 116 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 117 | |||||||||||||
| 118 | 3 | 1; | |||||||||||
| 119 | |||||||||||||
| 120 | # ABSTRACT: Read and write config as a C<SHELLVAR> data structure | ||||||||||||
| 121 | |||||||||||||
| 122 | __END__ | ||||||||||||