Backend/OpenSsh/Role/Writer.pm

filter: Statementstmt 96.2% 76 / 79 Branchbran 86.3% 19 / 22 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 5 totaltotal 89.6% 113 / 126 CC 17 SCAR 20.2
CC
17
Cov
93%
write_node_content
14.4
ssh_write
3.0
write_line
2.0
CRAP 7.5
« Backend/OpenSsh/Role/Reader.pm ↑ Summary Backend/OpenSsh/Ssh.pm »
1 #
2 # This file is part of Config-Model-OpenSsh
3 #
4 # This software is Copyright (c) 2008-2025 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 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package Config::Model::Backend::OpenSsh::Role::Writer ;
14 4 $Config::Model::Backend::OpenSsh::Role::Writer::VERSION = '2.10.2.1';
15 4 ▶use Mouse::Role ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 with 'Config::Model::Backend::OpenSsh::Role::MatchBlock';
18  
19 4 requires qw(write_global_comments write_data_and_comments);
20  
21 4 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 ▶use Config::Model 2.128;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use IO::File ;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use Log::Log4perl 1.11;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 my $logger = Log::Log4perl::get_logger("Backend::OpenSsh");
30  
31 13 ▶sub ssh_write {
Subroutine ssh_write: called CC=3 SCAR=11.0
Pod undocumented
32 13 my $self = shift ;
33 13 my %args = @_ ;
34  
35 my $config_root = $args{object}
36 13 ▶ || croak __PACKAGE__," ssh_write: undefined config root object";
Condition50%1/2
$args{'object'} || croak(__PACKAGE__, ' ssh_write: undefined config root object')
l!l
130
MC/DC0%0/1
$args{'object'} || croak(__PACKAGE__, ' ssh_write: undefined config root object')
$args{'object'}
Truth table50%1/2
A
A= $args{'object'}
Aresult
11
00
37  
38 13 $logger->info("writing config file $args{file_path}");
39  
40 13 my $comment = $self->write_global_comment('#') ;
41  
42 13 my $result = $self->write_node_content($config_root,$args{ssh_mode});
43  
44 13 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) 11 2 13
45 11 $args{file_path}->spew_utf8($comment.$result);
46 11 return 1;
47 }
48  
49 2 return 0;
50 }
51  
52  
53 3372 ▶sub write_line {
Subroutine write_line: called CC=2 SCAR=6.9
Pod undocumented
54 3372 my ($self, $k, $v, $note) = @_ ;
55 3372 ▶ return '' unless length($v) ;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 3228 144 3372
56 144 return $self->write_data_and_comments('#',sprintf("%-20s %s",$k,$v),$note) ;
57 }
58  
59 316 ▶sub write_list {
Subroutine write_list: called CC=1 SCAR=0.0
Pod undocumented
60 316 my ($self,$name,$mode,$elt) = @_;
61 316 my @r = map { $self->write_line($name,$_->fetch($mode), $_->annotation) ;} $elt->fetch_all() ;
62 316 return join('',@r) ;
63 }
64  
65  
66 8 ▶sub write_list_in_one_line {
Subroutine write_list_in_one_line: called CC=1 SCAR=0.0
Pod undocumented
67 8 my ($self,$name,$mode,$elt) = @_;
68 8 my @v = $elt->fetch_all_values(mode => $mode) ;
69 8 return $self->write_line($name,join(' ',@v)) ;
70 }
71  
72 # list there list element that must be written on one line with items
73 # separated by a white space
74 4 my %list_as_one_line = (
75 'AuthorizedKeysFile' => 1 ,
76 ) ;
77  
78 36 ▶sub write_node_content {
Subroutine write_node_content: called CC=14 SCAR=26.7
Pod undocumented
79 36 my $self= shift ;
80 36 my $node = shift ;
81 36 ▶ my $mode = shift || '';
Condition100%2/2
shift() || ''
l!l
288
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
82  
83 36 my $result = '' ;
84 36 my $match = '' ;
85  
86 36 foreach my $name ($node->get_element_name() ) {
87 3711 ▶ next unless $node->is_element_defined($name) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $node->is_element_defined($name) 0 3711 3711
88 3711 my $elt = $node->fetch_element($name) ;
89 3711 my $type = $elt->get_type;
90 3711 my $note = $elt->annotation ;
91  
92 #print "got $key type $type and ",join('+',@arg),"\n";
93 3711 ▶ if ($name eq 'Match') {
Branch: 7 branches
BranchTrueFalseTotal
if ($name eq 'Match') { } 13 3698 3711
elsif ($name eq 'Host') { } 9 3689 3698
elsif ($name =~ /^(Local|Remote)Forward$/) { } 56 3633 3689
elsif ($type eq 'leaf') { } 3305 328 3633
elsif ($type eq 'check_list') { } 0 328 328
elsif ($type eq 'list') { } 324 4 328
elsif ($type eq 'hash') { } 4 0 4
94 13 $match .= $self->write_all_match_block($elt,$mode) ;
95 }
96 elsif ($name eq 'Host') {
97 9 $match .= $self->write_all_host_block($elt,$mode) ;
98 }
99 elsif ($name =~ /^(Local|Remote)Forward$/) {
100 56 foreach ($elt->fetch_all()) {
101 8 $result .= $self->write_forward($_,$mode);
102 }
103 }
104 elsif ($type eq 'leaf') {
105 3305 my $v = $elt->fetch($mode) ;
106 3305 $result .= $self->write_line($name,$v,$note);
107 }
108 elsif ($type eq 'check_list') {
109 0 my $v = $elt->fetch($mode) ;
110 0 $result .= $self->write_line($name,$v,$note);
111 }
112 elsif ($type eq 'list') {
113 324 $result .= $self->write_data_and_comments('#', undef, $note) ;
114 324 ▶ $result .= $list_as_one_line{$name} ? $self->write_list_in_one_line($name,$mode,$elt)
Branch: 1 branch
BranchTrueFalseTotal
$list_as_one_line{$name} ? : 8 316 324
115 : $self->write_list($name,$mode,$elt) ;
116 }
117 elsif ($type eq 'hash') {
118 4 foreach my $k ( $elt->fetch_all_indexes ) {
119 2 my $o = $elt->fetch_with_id($k);
120 2 my $v = $o->fetch($mode) ;
121 2 $result .= $self->write_line($name,"$k $v", $o->annotation) ;
122 }
123 }
124 else {
125 0 die "OpenSsh::write did not expect $type for $name\n";
126 }
127 }
128  
129 36 return $result.$match ;
130 }
131  
132 4 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
133  
134 4 1;
135  
136 # ABSTRACT: Role to write OpenSsh config files
137  
138 __END__
« Backend/OpenSsh/Role/Reader.pm ↑ Summary Backend/OpenSsh/Ssh.pm »