lib/Net/SinFP3/Output/CSV.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 97 CC 6 SCAR 20.6
CC
6
Cov
0%
run
20.0
_writeCsvLine
6.0
post
6.0
CRAP 7.9
1 #
2 # $Id: CSV.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $
3 #
4 package Net::SinFP3::Output::CSV;
5 use strict;
6 use warnings;
7  
8 use base qw(Net::SinFP3::Output);
9 our @AS = qw(
10 osOnly
11 file
12 _fd
13 );
14 __PACKAGE__->cgBuildIndices;
15 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
16  
17 use Data::Dumper;
18  
19 sub take {
20 return [
21 'Net::SinFP3::Result::Active',
22 'Net::SinFP3::Result::Unknown',
23 'Net::SinFP3::Result::PortError',
24 ];
25 }
26  
27 sub new {
28 my $self = shift->SUPER::new(
29 osOnly => 1,
30 file => 'sinfp3-output.csv',
31 @_,
32 );
33  
34 return $self;
35 }
36  
37 sub _writeCsvLine {
38 my $self = shift;
39 my ($resultList) = @_;
40  
41 my $global = $self->global;
42 my $log = $global->log;
43 my $next = $global->next;
44  
45 my $buf = '';
46 my $r = $resultList->[0];
47 if ($self->osOnly) {
48 my $osList = $r->getPossibleOsList($resultList);
49  
50 my $ip = $r->ip;
51 my $port = $r->port;
52 my $sOsList = join(',', @$osList);
53 $buf = "Results for target: [$ip]:$port: $sOsList\n";
54  
55 my $fd = $self->_fd;
56  
57 print $fd ref($next).";$ip;$port;".scalar(@$osList).";$sOsList;\n";
58 }
59 else {
60 $log->fatal("Not implemented yet");
61 }
62  
63 return $buf;
64 }
65  
66 sub init {
67 my $self = shift;
68  
69 my $log = $self->global->log;
70  
71 open(my $out, '>>', $self->file)
72 or $log->fatal("Cannot open file: ".$self->file);
73  
74 $self->_fd($out);
75  
76 return 1;
77 }
78  
79 sub post {
80 my $self = shift;
81  
82 my $fd = $self->_fd;
83 if ($fd) {
84 close($fd);
85 }
86  
87 return 1;
88 }
89  
90 sub run {
91 my $self = shift->SUPER::run(@_) or return;
92  
93 my $global = $self->global;
94 my $log = $global->log;
95 my @results = $global->result;
96  
97 for my $r (@results) {
98 my $ref = ref($r);
99 if ($ref =~ /^Net::SinFP3::Result::Unknown$/) {
100 return 1;
101 }
102 elsif ($ref =~ /^Net::SinFP3::Result::PortError$/) {
103 return 1;
104 }
105 elsif ($ref =~ /^Net::SinFP3::Result::Active$/) {
106 my $buf = $self->_writeCsvLine(\@results);
107 print $buf;
108 last;
109 }
110 else {
111 $log->warning("Don't know what to do with this result object ".
112 "with type: [$ref]");
113 next;
114 }
115 }
116  
117 return 1;
118 }
119  
120 1;
121  
122 __END__