| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use OptArgs2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Text::vCard::Addressbook; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '1.0.2'; | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | my $opts = optargs( | ||||||||||||||||||||||||||||||||||||||||||
| 12 | comment => 'tidy (normalize) VCARD contact files', | ||||||||||||||||||||||||||||||||||||||||||
| 13 | optargs => [ | ||||||||||||||||||||||||||||||||||||||||||
| 14 | files => { | ||||||||||||||||||||||||||||||||||||||||||
| 15 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | [ ( -t STDIN ) ? ( die OptArgs2::usage(__PACKAGE__) ) : '-' ] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 19 | greedy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 20 | comment => 'file to tidy (default is stdin)', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | do_nothing => { | ||||||||||||||||||||||||||||||||||||||||||
| 23 | isa => '--Flag', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | alias => 'n', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | comment => q{don't modify files, only report errors}, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | no_rev => { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | isa => '--Flag', | ||||||||||||||||||||||||||||||||||||||||||
| 29 | alias => 'R', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | comment => 'do not update REV value' | ||||||||||||||||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | filter => { | ||||||||||||||||||||||||||||||||||||||||||
| 33 | isa => '--ArrayRef', | ||||||||||||||||||||||||||||||||||||||||||
| 34 | isa_name => 'PERL', | ||||||||||||||||||||||||||||||||||||||||||
| 35 | alias => 'f', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | comment => 'Perl filter(s) to run against $_ first', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 39 | version => { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | isa => '--Flag', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | alias => 'V', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | comment => 'print version information and exit', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | trigger => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | require File::Basename; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | die File::Basename::basename($0) | |||||||||||||||||||||||||||||||||||||||||
| 46 | . ' version ' | ||||||||||||||||||||||||||||||||||||||||||
| 47 | . $VERSION . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ], | |||||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | my $dtstamp = localtime->strftime('%Y-%m-%dT%H%M%SZ'); | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | foreach my $f ( @{ $opts->{files} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | $opts->{input} = $f; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | vcardtidy($opts); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | sub vcardtidy { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
vcardtidy: called CC=11 SCAR=28.6
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | my $opts = shift; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | my $data; | |||||||||||||||||||||||||||||||||||||||||
| 64 | my $file; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | if ( $opts->{input} eq '-' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $data = <STDIN>; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | $file = path( $opts->{input} ); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | $data = $file->slurp( { binmode => ':raw:encoding(UTF-8)' } ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | $data = App::vcardtidy::run_filters( $data, @{ $opts->{filter} } ); | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | my $tidy = eval { | |||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | my $ab = Text::vCard::Addressbook->new( { 'source_text' => $data } ); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | my $clean = $ab->export; | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | my $clean2 = $clean =~ s/^REV:.*\015\012//mr; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 1 | my $data2 = $data =~ s/^REV:.*\015\012//mr; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | if ( not( $opts->{no_rev} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not $opts->{"no_rev"} and $clean2 eq $clean || $clean2 ne $data2
$clean2 eq $clean or $clean2 ne $data2
MC/DC0%0/3
not $opts->{"no_rev"} and $clean2 eq $clean || $clean2 ne $data2
not $opts->{"no_rev"}
$clean2 eq $clean
$clean2 ne $data2
Truth table25%1/4
not(A) and B or C
A= not $opts->{"no_rev"}B= $clean2 eq $cleanC= $clean2 ne $data2
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | and ( $clean2 eq $clean or $clean2 ne $data2 ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | foreach my $vcard ( $ab->vcards ) { | |||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | $vcard->REV($dtstamp); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $ab->export; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | $clean; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | warn "Invalid VCARD in '$opts->{input}': $@"; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | elsif ( length($tidy) == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | warn "Invalid VCARD in '$opts->{input}'\n"; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | # Fix for multiple categories | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | while ( $tidy =~ s/^(CATEGORIES:.*?)\\,/$1,/mg ) { } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | if ( $opts->{input} eq '-' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | print $tidy; | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | $file->spew( { binmode => ':raw:encoding(UTF-8)' }, $tidy ) | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | unless $opts->{do_nothing}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | package App::vcardtidy; | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | sub run_filters { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_filters: called CC=3 SCAR=15.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | local $_ = shift; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | foreach my $filter (@_) { | |||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | eval $filter; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | die qq{filter "$filter" failed:\n$@} if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | $_; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||||||||||||||||