bin/comb-svg

filter: Statementstmt 100% 104 / 104 Branchbran 95.9% 71 / 74 Conditioncond 95.2% 40 / 42 MC/DCmcdc 92.0% 23 / 25 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 97.3% 254 / 261 CC 54 SCAR 27.5
CC
54
Cov
98%
options
25.0
main
19.0
read_input
7.2
CRAP 15.7
↑ Summary blib/lib/Kubernetes/Comb/SVG/Layout.pm »
1 #!/usr/bin/env perl
2 # PODNAME: comb-svg
3 # ABSTRACT: Render Kubernetes::Comb custom resources from JSON as an SVG honeycomb
4  
5 94 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 94 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 94 ▶use Getopt::Long qw( GetOptionsFromArray );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 94 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 94 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 94 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 94 ▶use Kubernetes::Comb::SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 94 our $VERSION = '0.001';
14  
15 94 my $ME = 'comb-svg';
16  
17 62 ▶sub svg_class { 'Kubernetes::Comb::SVG' }
Subroutine svg_class: called CC=1 SCAR=0.0
18  
19 1 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
20 1 return join( "\n",
21 'Usage: '.$ME.' [options] [FILE|-]',
22 '',
23 'Reads Comb custom resources as JSON (a List, an array or one resource)',
24 'from FILE or standard input and writes the SVG to standard output.',
25 '',
26 ' --title TEXT heading and <title> of the picture',
27 ' --group-label KEY label key whose value names the group of a Comb',
28 ' --layout NAME depth (rows by dependency, the default) or packed',
29 ' --columns N cells per row before a row wraps',
30 ' --rows N packed: number of rows, without --columns',
31 ' --aspect W:H packed: target shape, without --columns and --rows',
32 ' --size N radius of a hexagon in SVG units',
33 ' --no-edges leave out the dependency edges (--edges draws them)',
34 ' --no-legend leave out the legend',
35 ' --color KEY=COLOUR colour of a phase or of bg, fg, muted, border, edge;',
36 ' KEY=LIGHT,DARK for the two modes; repeatable',
37 ' --blink PHASES phases whose cells pulse, comma-separated',
38 ' --blink-seconds N period of the pulse in seconds',
39 ' --help this text',
40 ' --version the version',
41 ''
42 );
43 }
44  
45 # One line on stderr, nothing on stdout, exit 1 — for usage and data alike.
46 31 ▶sub fail {
Subroutine fail: called CC=1 SCAR=0.0
47 31 my ( $message ) = @_;
48 31 $message =~ s/\s+\z//;
49 31 $message =~ s/\s*\n\s*/ /g;
50 31 print STDERR $ME.': '.$message."\n";
51 31 exit 1;
52 }
53  
54 # What an exception says, without where it was thrown.
55 4 ▶sub reason {
Subroutine reason: called CC=1 SCAR=0.0
56 4 my ( $error ) = @_;
57 4 $error = ''.$error;
58 4 $error =~ s/\A(.*) at \S.*? line \d+\.?\s*\z/$1/s;
59 4 return $error;
60 }
61  
62 94 ▶sub options {
Subroutine options: called CC=25 SCAR=32.2
63 94 my ( $argv ) = @_;
64 94 my %opt = ( legend => 1 );
65 94 my @complaints;
66 94 Getopt::Long::Configure(qw( no_ignore_case no_auto_abbrev ));
67 94 my $ok = do {
68 94 ▶ local $SIG{__WARN__} = sub { push @complaints, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
69 94 GetOptionsFromArray( $argv, \%opt,
70 'title=s', 'group-label=s', 'layout=s', 'columns=s', 'rows=s', 'aspect=s', 'size=s',
71 'edges!', 'legend!', 'color=s@', 'blink=s@', 'blink-seconds=s', 'help', 'version'
72 );
73 };
74 94 ▶ fail( ( @complaints ? $complaints[0] : 'bad options' ).' (try --help)' ) unless $ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 3 91 94
@complaints ? : 3 0 3
75 91 ▶ return \%opt if $opt{help} || $opt{version};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} or $opt{"version"} 2 89 91
Condition100%3/3
$opt{"help"} or $opt{"version"}
l!l&&r!l&&!r
1189
MC/DC100%2/2
$opt{"help"} or $opt{"version"}
$opt{"help"} $opt{"version"}
Truth table100%3/3
A or B
A= $opt{"help"}B= $opt{"version"}
ABresult
1X1
011
000
76  
77 fail( '--columns needs a positive integer, got "'.$opt{columns}.'"' )
78 89 ▶ if defined $opt{columns} && $opt{columns} !~ /\A[1-9][0-9]{0,8}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"columns"} and not $opt{"columns"} =~ /\A[1-9][0-9]{0,8}\z/ 3 86 89
Condition100%3/3
defined $opt{"columns"} and not $opt{"columns"} =~ /\A[1-9][0-9]{0,8}\z/
!ll&&!rl&&r
8333
MC/DC100%2/2
defined $opt{"columns"} and not $opt{"columns"} =~ /\A[1-9][0-9]{0,8}\z/
defined $opt{"columns"} not $opt{"columns"} =~ /\A[1-9][0-9]{0,8}\z/
Truth table100%3/3
A and not(B)
A= defined $opt{"columns"}B= not $opt{"columns"} =~ /\A[1-9][0-9]{0,8}\z/
ABresult
0X0
100
111
79 fail( '--layout needs depth or packed, got "'.$opt{layout}.'"' )
80 86 ▶ if defined $opt{layout} && $opt{layout} !~ /\A(?:depth|packed)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"layout"} and not $opt{"layout"} =~ /\A(?:depth|packed)\z/ 1 85 86
Condition100%3/3
defined $opt{"layout"} and not $opt{"layout"} =~ /\A(?:depth|packed)\z/
!ll&&!rl&&r
70151
MC/DC100%2/2
defined $opt{"layout"} and not $opt{"layout"} =~ /\A(?:depth|packed)\z/
defined $opt{"layout"} not $opt{"layout"} =~ /\A(?:depth|packed)\z/
Truth table100%3/3
A and not(B)
A= defined $opt{"layout"}B= not $opt{"layout"} =~ /\A(?:depth|packed)\z/
ABresult
0X0
100
111
81 fail( '--rows needs a positive integer, got "'.$opt{rows}.'"' )
82 85 ▶ if defined $opt{rows} && $opt{rows} !~ /\A[1-9][0-9]{0,8}\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"rows"} and not $opt{"rows"} =~ /\A[1-9][0-9]{0,8}\z/ 2 83 85
Condition100%3/3
defined $opt{"rows"} and not $opt{"rows"} =~ /\A[1-9][0-9]{0,8}\z/
!ll&&!rl&&r
8212
MC/DC100%2/2
defined $opt{"rows"} and not $opt{"rows"} =~ /\A[1-9][0-9]{0,8}\z/
defined $opt{"rows"} not $opt{"rows"} =~ /\A[1-9][0-9]{0,8}\z/
Truth table100%3/3
A and not(B)
A= defined $opt{"rows"}B= not $opt{"rows"} =~ /\A[1-9][0-9]{0,8}\z/
ABresult
0X0
100
111
83 83 ▶ if ( defined $opt{aspect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"aspect"}) 10 73 83
84 10 my $number = qr/[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9}/;
85 10 my ( $width, $height ) = $opt{aspect} =~ m{\A($number)(?:[:/]($number))?\z};
86 10 ▶ $height = 1 unless defined $height;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $height 4 6 10
87 10 ▶ fail( '--aspect needs a positive number or W:H, got "'.$opt{aspect}.'"' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width and $width > 0 and $height > 0 4 6 10
Condition100%6/6
defined $width and $width > 0 and $height > 0
!ll&&!rl&&r
316
defined $width and $width > 0
!ll&&!rl&&r
217
MC/DC100%3/3
defined $width and $width > 0 and $height > 0
defined $width $width > 0 $height > 0
Truth table100%4/4
A and B and C
A= defined $widthB= $width > 0C= $height > 0
ABCresult
0XX0
10X0
1100
1111
88 unless defined $width && $width > 0 && $height > 0;
89 6 $opt{aspect} = $width / $height;
90 }
91 fail( '--size needs a positive number, got "'.$opt{size}.'"' )
92 if defined $opt{size}
93 79 ▶ && !( $opt{size} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{size} > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"size"} and not $opt{"size"} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{"size"} > 0 2 77 79
Condition100%6/6
defined $opt{"size"} and not $opt{"size"} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{"size"} > 0
!ll&&!rl&&r
7522
$opt{'size'} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{'size'} > 0
!ll&&!rl&&r
112
MC/DC100%3/3
defined $opt{"size"} and not $opt{"size"} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{"size"} > 0
defined $opt{"size"} $opt{'size'} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ $opt{'size'} > 0
Truth table100%4/4
A and not(B && C)
A= defined $opt{"size"}B= $opt{'size'} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/C= $opt{'size'} > 0
ABCresult
0XX0
1110
10X1
1101
94 fail( '--blink-seconds needs a positive number, got "'.$opt{'blink-seconds'}.'"' )
95 if defined $opt{'blink-seconds'}
96 && !( $opt{'blink-seconds'} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/
97 77 ▶ && $opt{'blink-seconds'} > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"blink-seconds"} and not $opt{"blink-seconds"} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{"blink-seconds"} > 0 4 73 77
Condition100%6/6
defined $opt{"blink-seconds"} and not $opt{"blink-seconds"} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{"blink-seconds"} > 0
!ll&&!rl&&r
7034
$opt{'blink-seconds'} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{'blink-seconds'} > 0
!ll&&!rl&&r
313
MC/DC100%3/3
defined $opt{"blink-seconds"} and not $opt{"blink-seconds"} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ && $opt{"blink-seconds"} > 0
defined $opt{"blink-seconds"} $opt{'blink-seconds'} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/ $opt{'blink-seconds'} > 0
Truth table100%4/4
A and not(B && C)
A= defined $opt{"blink-seconds"}B= $opt{'blink-seconds'} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/C= $opt{'blink-seconds'} > 0
ABCresult
0XX0
1110
10X1
1101
98 73 ▶ $opt{blink} = [ grep { length } map { split /,/ } @{ $opt{blink} } ] if $opt{blink};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"blink"} 8 65 73
99 73 ▶ $opt{theme} = theme( $opt{color} ) if $opt{color};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"color"} 19 54 73
100 69 ▶ fail( 'more than one input file (try --help)' ) if @$argv > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$argv > 1 1 68 69
101 68 return \%opt;
102 }
103  
104 # The --color values as a theme: KEY=COLOUR, or KEY=LIGHT,DARK with the comma
105 # outside parentheses, so rgb(1,2,3) stays one colour. Whether a key or a
106 # colour is one the picture knows is left to the module, which ignores what
107 # it does not accept; the last --color of a key wins.
108 19 ▶sub theme {
Subroutine theme: called CC=6 SCAR=17.9
109 19 my ( $colors ) = @_;
110 19 my %theme;
111 19 for my $color (@$colors) {
112 21 my ( $key, $value ) = $color =~ /\A([^=]+)=(.+)\z/s;
113 21 ▶ my @modes = defined $value ? split( /,(?![^(]*\))/, $value, -1 ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 18 3 21
114 21 ▶ fail( '--color needs KEY=COLOUR or KEY=LIGHT,DARK, got "'.$color.'"' )
Branch: 1 branch
BranchTrueFalseTotal
unless @modes == 1 or @modes == 2 4 17 21
Condition100%3/3
@modes == 1 or @modes == 2
l!l&&r!l&&!r
1074
MC/DC100%2/2
@modes == 1 or @modes == 2
@modes == 1 @modes == 2
Truth table100%3/3
A or B
A= @modes == 1B= @modes == 2
ABresult
1X1
011
000
115 unless @modes == 1 || @modes == 2;
116 17 ▶ $theme{$key} = @modes == 1 ? $modes[0] : { light => $modes[0], dark => $modes[1] };
Branch: 1 branch
BranchTrueFalseTotal
@modes == 1 ? : 10 7 17
117 }
118 15 return \%theme;
119 }
120  
121 68 ▶sub read_input {
Subroutine read_input: called CC=7 SCAR=19.7
122 68 my ( $file ) = @_;
123 68 ▶ if ( !defined $file || $file eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $file or $file eq "-") 5 63 68
Condition100%3/3
not defined $file or $file eq "-"
l!l&&r!l&&!r
4163
MC/DC100%2/2
not defined $file or $file eq "-"
not defined $file $file eq "-"
Truth table100%3/3
not(A) or B
A= not defined $fileB= $file eq "-"
ABresult
1X1
011
000
124 5 binmode STDIN;
125 5 local $/;
126 5 my $bytes = <STDIN>;
127 5 ▶ return ( defined $bytes ? $bytes : '', 'standard input' );
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes ? : 5 0 5
128 }
129 63 my $bytes = eval { path($file)->slurp_raw };
130 63 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 2 61 63
131 fail( 'cannot read '.$file.': '
132 2 ▶ .( blessed $error && $error->isa('Path::Tiny::Error') ? $error->{err} : reason($error) ) );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($error) && $error->isa('Path::Tiny::Error') ? : 2 0 2
Condition33%1/3
builtin::blessed($error) and $error->isa("Path::Tiny::Error")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($error) and $error->isa("Path::Tiny::Error")
builtin::blessed($error) $error->isa("Path::Tiny::Error")
Truth table33%1/3
A and B
A= builtin::blessed($error)B= $error->isa("Path::Tiny::Error")
ABresult
0X0
100
111
133 }
134 61 return ( $bytes, $file );
135 }
136  
137 94 ▶sub main {
Subroutine main: called CC=19 SCAR=29.4
138 94 my @argv = @_;
139 94 utf8::decode($_) for @argv;
140 94 my $opt = options( \@argv );
141  
142 70 ▶ if ( $opt->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"help"}) 1 69 70
143 1 print usage();
144 1 return 0;
145 }
146 69 ▶ if ( $opt->{version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"version"}) 1 68 69
147 1 print $ME.' '.$VERSION."\n";
148 1 return 0;
149 }
150  
151 68 my ( $bytes, $source ) = read_input( $argv[0] );
152 66 my $combs = eval { JSON::MaybeXS->new( utf8 => 1, allow_nonref => 1 )->decode($bytes) };
153 66 ▶ fail( 'invalid JSON in '.$source.': '.reason($@) ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 63 66
154 63 ▶ fail( $source.' is neither a List, an array of Combs nor a Comb' )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $combs eq "HASH" or ref $combs eq "ARRAY" 1 62 63
Condition100%3/3
ref $combs eq "HASH" or ref $combs eq "ARRAY"
l!l&&r!l&&!r
2601
MC/DC100%2/2
ref $combs eq "HASH" or ref $combs eq "ARRAY"
ref $combs eq "HASH" ref $combs eq "ARRAY"
Truth table100%3/3
A or B
A= ref $combs eq "HASH"B= ref $combs eq "ARRAY"
ABresult
1X1
011
000
155 unless ref $combs eq 'HASH' || ref $combs eq 'ARRAY';
156  
157 62 my $svg = eval {
158 svg_class()->new(
159 combs => $combs,
160 legend => $opt->{legend},
161 defined $opt->{edges} ? ( edges => $opt->{edges} ) : (),
162 defined $opt->{title} ? ( title => $opt->{title} ) : (),
163 defined $opt->{'group-label'} ? ( group_label => $opt->{'group-label'} ) : (),
164 defined $opt->{layout} ? ( layout => $opt->{layout} ) : (),
165 defined $opt->{columns} ? ( columns => $opt->{columns} + 0 ) : (),
166 defined $opt->{rows} ? ( rows => $opt->{rows} + 0 ) : (),
167 defined $opt->{aspect} ? ( aspect => $opt->{aspect} ) : (),
168 defined $opt->{size} ? ( size => $opt->{size} + 0 ) : (),
169 defined $opt->{theme} ? ( theme => $opt->{theme} ) : (),
170 defined $opt->{blink} ? ( blink => $opt->{blink} ) : (),
171 62 ▶ defined $opt->{'blink-seconds'} ? ( blink_seconds => $opt->{'blink-seconds'} + 0 ) : ()
Branch: 11 branches
BranchTrueFalseTotal
defined $opt->{'edges'} ? : 5 57 62
defined $opt->{'title'} ? : 2 60 62
defined $opt->{'group-label'} ? : 3 59 62
defined $opt->{'layout'} ? : 15 47 62
defined $opt->{'columns'} ? : 3 59 62
defined $opt->{'rows'} ? : 1 61 62
defined $opt->{'aspect'} ? : 6 56 62
defined $opt->{'size'} ? : 2 60 62
defined $opt->{'theme'} ? : 15 47 62
defined $opt->{'blink'} ? : 8 54 62
defined $opt->{'blink-seconds'} ? : 3 59 62
172 )->render;
173 };
174 62 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 61 62
175 1 $error = reason($error);
176 1 $error =~ s/\A[\w:]+->\w+: //;
177 1 fail($error);
178 }
179  
180 61 print $svg;
181 61 return 0;
182 }
183  
184 94 exit main(@ARGV);
185  
186 __END__
↑ Summary blib/lib/Kubernetes/Comb/SVG/Layout.pm »