blib/lib/Text/Authinfo.pm

filter: Statementstmt 63.5% 54 / 85 Branchbran 23.0% 6 / 26 Conditioncond 25.9% 7 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 84.6% 11 / 13 Podpod 100% 5 / 5 totaltotal 48.5% 83 / 171 CC 28 SCAR 39.9
CC
28
Cov
49%
writeauthinfo
156.0
as_string
30.0
readauthinfo
11.5
CRAP 53.9
↑ Summary
1 package Text::Authinfo;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use File::Copy qw(move);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use vars qw($VERSION @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 3 @EXPORT = qw(readauthinfo writeauthinfo as_string);
14  
15 3 our $VERSION = '0.03';
16 3 our $wildcard = 'ANY';
17 3 our $authinfofile = $ENV{'HOME'} . '/.authinfo';
18  
19  
20 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
21 1 my $self = {};
22 1 my $class = shift;
23  
24 1 ▶ $self->{FILE} = shift || $authinfofile;
Condition33%1/3
shift() || $Text::Authinfo::authinfofile
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || $Text::Authinfo::authinfofile
shift() $Text::Authinfo::authinfofile
Truth table33%1/3
A || B
A= shift()B= $Text::Authinfo::authinfofile
ABresult
1X1
011
000
25 1 $self->{AUTHINFO} = {};
26  
27 1 bless $self,$class ;
28 1 return $self;
29 }
30  
31  
32 1 ▶sub readauthinfo {
Subroutine readauthinfo: called CC=9 SCAR=24.4
Pod documented
33 1 my $self = shift;
34  
35 1 my $ai = {};
36 1 ▶ my $csv = Text::CSV->new({sep_char=> ' '}) || croak 'new Text::CSV';
Condition50%1/2
'Text::CSV'->new({'sep_char', ' '}) || croak('new Text::CSV')
l!l
10
MC/DC0%0/1
'Text::CSV'->new({'sep_char', ' '}) || croak('new Text::CSV')
'Text::CSV'->new({'sep_char', ' '})
Truth table50%1/2
A
A= 'Text::CSV'->new({'sep_char', ' '})
Aresult
11
00
37  
38 1 ▶ open(my $fh,'<',$self->{FILE}) || croak "open $self->{FILE}:$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"FILE"} or croak(qq[open $self->{"FILE"}:$!]) 1 0 1
39  
40 1 ▶ LINE:while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 1 1 2
41 1 chomp $line;
42 1 $csv->parse($line);
43 1 my %l = $csv->fields();
44 # this package requires at least these fields to be on a valid
45 # line:
46 1 ▶ if (defined($l{'machine'}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l{'machine'} and defined $l{'login'} and defined $l{'password'}) { } 1 0 1
Condition33%2/6
defined $l{"machine"} and defined $l{"login"} and defined $l{"password"}
!ll&&!rl&&r
001
defined $l{"machine"} and defined $l{"login"}
!ll&&!rl&&r
001
MC/DC0%0/3
defined $l{"machine"} and defined $l{"login"} and defined $l{"password"}
defined $l{"machine"} defined $l{"login"} defined $l{"password"}
Truth table25%1/4
A and B and C
A= defined $l{"machine"}B= defined $l{"login"}C= defined $l{"password"}
ABCresult
0XX0
10X0
1100
1111
47 defined($l{'login'}) &&
48 defined($l{'password'})) {
49 # allow port to be defined or ANY
50 1 my $port = $wildcard;
51 1 ▶ $port = $l{'port'} if (defined($l{'port'}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $l{"port"} 1 0 1
52 1 $ai->{$l{'machine'}}->{$port}->{$l{'login'}} = $l{'password'};
53 } else {
54 0 carp "$line missing some fields? skipping";
55 0 next LINE;
56 }
57 }
58  
59 1 $self->{AUTHINFO} = $ai;
60  
61 1 ▶ close($fh) || croak "close $authinfofile:$!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("close $Text::Authinfo::authinfofile:$!") 1 0 1
62  
63 1 return 1; # caller can now query authinfo data as a perl assoc array
64 }
65  
66  
67 2 ▶sub getauth() {
Subroutine getauth: called CC=4 SCAR=15.2
Pod documented
68  
69 2 my $self = shift;
70 2 ▶ my $machine = shift || croak 'use: getauth(machine,login,[port])';
Condition50%1/2
shift() || croak('use: getauth(machine,login,[port])')
l!l
20
MC/DC0%0/1
shift() || croak('use: getauth(machine,login,[port])')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
71 2 ▶ my $login = shift || croak 'use: getauth(machine,login,[port])';
Condition50%1/2
shift() || croak('use: getauth(machine,login,[port])')
l!l
20
MC/DC0%0/1
shift() || croak('use: getauth(machine,login,[port])')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
72 2 ▶ my $port = shift || $wildcard;
Condition33%1/3
shift() || $Text::Authinfo::wildcard
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || $Text::Authinfo::wildcard
shift() $Text::Authinfo::wildcard
Truth table33%1/3
A || B
A= shift()B= $Text::Authinfo::wildcard
ABresult
1X1
011
000
73  
74 # retval may be undef
75 2 return $self->{AUTHINFO}->{$machine}->{$port}->{$login};
76 }
77  
78  
79 0 ▶sub as_string {
Subroutine as_string: not called CC=5 SCAR=34.0
Pod documented
80 0 my $self = shift;
81  
82 0 my $c = '';
83 0 for my $machine (keys %{$self->{AUTHINFO}}) {
84 0 for my $port (keys %{$self->{AUTHINFO}->{$machine}}) {
85 0 for my $login (keys %{$self->{AUTHINFO}->{$machine}->{$port}}) {
86 0 my $pass = $self->{AUTHINFO}->{$machine}->{$port}->{$login};
87 0 $c .= 'machine ' . $machine . ' login ' . $login .
88 ' password ' . $pass;
89 0 ▶ $c .= ' port ' . $port if ($port ne $wildcard);
Branch: 1 branch
BranchTrueFalseTotal
if $port ne $Text::Authinfo::wildcard 0 0 0
90 0 $c .= "\n";
91 }
92 }
93 }
94 0 return $c;
95 }
96  
97  
98 0 ▶sub writeauthinfo {
Subroutine writeauthinfo: not called CC=12 SCAR=50.5
Pod documented
99 0 my $self = shift;
100  
101 0 ▶ my $ops = shift || undef;
Condition0%0/2
shift() || undef
l!l
00
MC/DC0%0/1
shift() || undef
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
102 0 ▶ if (defined($ops) && (ref($ops) ne 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ops and ref $ops ne "HASH") 0 0 0
Condition0%0/3
defined $ops and ref $ops ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ops and ref $ops ne "HASH"
defined $ops ref $ops ne "HASH"
Truth table0%0/3
A and B
A= defined $opsB= ref $ops ne "HASH"
ABresult
0X0
100
111
103 0 carp "args are passed to writeauthinfo via a hash ref";
104 }
105  
106 0 ▶ if (-w $self->{FILE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-w $self->{"FILE"}) 0 0 0
107 # there is already a .authinfo file, mv it to .authinfo.bak
108 0 ▶ if (defined($ops->{nobackup})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ops->{'nobackup'}) { } 0 0 0
109 0 ▶ unlink $self->{FILE} || croak "rm old $self->{FILE}:$!"
Condition0%0/2
$self->{'FILE'} || croak("rm old $self->{'FILE'}:$!")
l!l
00
MC/DC0%0/1
$self->{'FILE'} || croak("rm old $self->{'FILE'}:$!")
$self->{'FILE'}
Truth table0%0/2
A
A= $self->{'FILE'}
Aresult
11
00
110 } else { # by default, make a backup old old authinfo file
111 0 my $bak = $self->{FILE} . '.bak';
112 0 ▶ move($self->{FILE},$bak) || croak "mv $self->{FILE} $bak:$!";
Branch: 1 branch
BranchTrueFalseTotal
move($self->{"FILE"}, $bak) or croak(qq[mv $self->{"FILE"} ${bak}:$!]) 0 0 0
113 }
114 }
115  
116 0 my $c = $self->as_string();
117 0 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) 0 0 0
118 0 ▶ open(my $fh,'>',$self->{FILE}) || croak "open $self->{FILE}:$!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $self->{"FILE"} or croak(qq[open $self->{"FILE"}:$!]) 0 0 0
119 0 print $fh $c;
120 0 ▶ close($fh) || croak "close $self->{FILE}:$!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak(qq[close $self->{"FILE"}:$!]) 0 0 0
121 0 ▶ chmod 0600, $self->{FILE} || croak "chmod fail on $self->{FILE}:$!";
Condition0%0/2
$self->{'FILE'} || croak("chmod fail on $self->{'FILE'}:$!")
l!l
00
MC/DC0%0/1
$self->{'FILE'} || croak("chmod fail on $self->{'FILE'}:$!")
$self->{'FILE'}
Truth table0%0/2
A
A= $self->{'FILE'}
Aresult
11
00
122 }
123  
124 0 return 1;
125 }
126  
127 __END__;
↑ Summary