| 1 | package Mail::TLSRPT::App::Command::csv; | ||
| 2 | # ABSTRACT: Process tlsrpt reports into csv | ||
| 3 | our $VERSION = '2.20261112'; # VERSION | ||
| 4 | use 5.20.0; | ||
| 5 | use Mail::TLSRPT::Pragmas; | ||
| 6 | use Mail::TLSRPT::App -command; | ||
| 7 | use Mail::TLSRPT::Report; | ||
| 8 | |||
| 9 | |||
| 10 | sub abstract { 'Process tlsrpt files into csv' } | ||
| 11 | sub description { 'Process tlsrpt files and output as csv' }; | ||
| 12 | sub usage_desc { "%c csv %o FILE <FILE> <FILE>" } | ||
| 13 | |||
| 14 | sub opt_spec { | ||
| 15 | return ( | ||
| 16 | [ 'output=s', 'Write results to filename (defaults to STDOUT)' ], | ||
| 17 | ); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub validate_args($self,$opt,$args) { | ||
| 21 | $self->usage_error('No files specified') if !@$args; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub execute($self,$opt,$args) { | ||
| 25 | |||
| 26 | my $tlsrpt; | ||
| 27 | |||
| 28 | my @all_output; | ||
| 29 | my $add_header = 1; | ||
| 30 | |||
| 31 | foreach my $file ( $args->@* ) { | ||
| 32 | |||
| 33 | $self->usage_error("File $file does not exist") if ! -e $file; | ||
| 34 | |||
| 35 | open my $fh, '<', $file or die 'Could not open input file'; | ||
| 36 | my @file_contents = <$fh>; | ||
| 37 | close $fh; | ||
| 38 | my $payload = join('',@file_contents); | ||
| 39 | |||
| 40 | my $tlsrpt = eval{ Mail::TLSRPT::Report->new_from_json($payload) }; | ||
| 41 | my $error = $@; | ||
| 42 | |||
| 43 | if ( $tlsrpt ) { | ||
| 44 | push @all_output, $tlsrpt->as_csv({add_header=>$add_header}); | ||
| 45 | $add_header = 0; | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | warn "Could not parse file $file: $error"; | ||
| 49 | } | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | my $output= join("\n",@all_output); | ||
| 54 | |||
| 55 | if ( $opt->output ) { | ||
| 56 | open my $fh, '>:encoding(UTF-8)', $opt->output or die 'Could not open output file'; | ||
| 57 | print $fh $output; | ||
| 58 | close $fh; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | say $output; | ||
| 62 | } | ||
| 63 | |||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | __END__ |