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.002';
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, @group, Phase@group or bg, fg, muted,',
36 ' border, edge;',
37 ' KEY=LIGHT,DARK for the two modes; repeatable',
38 ' --blink PHASES phases (or @group, Phase@group) that pulse, comma-separated',
39 ' --blink-seconds N period of the pulse in seconds',
40 ' --help this text',
41 ' --version the version',
42 ''
43 );
44 }
45  
46 # One line on stderr, nothing on stdout, exit 1 — for usage and data alike.
47 31 ▶sub fail {
Subroutine fail: called CC=1 SCAR=0.0
48 31 my ( $message ) = @_;
49 31 $message =~ s/\s+\z//;
50 31 $message =~ s/\s*\n\s*/ /g;
51 31 print STDERR $ME.': '.$message."\n";
52 31 exit 1;
53 }
54  
55 # What an exception says, without where it was thrown.
56 4 ▶sub reason {
Subroutine reason: called CC=1 SCAR=0.0
57 4 my ( $error ) = @_;
58 4 $error = ''.$error;
59 4 $error =~ s/\A(.*) at \S.*? line \d+\.?\s*\z/$1/s;
60 4 return $error;
61 }
62  
63 94 ▶sub options {
Subroutine options: called CC=25 SCAR=32.2
64 94 my ( $argv ) = @_;
65 94 my %opt = ( legend => 1 );
66 94 my @complaints;
67 94 Getopt::Long::Configure(qw( no_ignore_case no_auto_abbrev ));
68 94 my $ok = do {
69 94 ▶ local $SIG{__WARN__} = sub { push @complaints, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
70 94 GetOptionsFromArray( $argv, \%opt,
71 'title=s', 'group-label=s', 'layout=s', 'columns=s', 'rows=s', 'aspect=s', 'size=s',
72 'edges!', 'legend!', 'color=s@', 'blink=s@', 'blink-seconds=s', 'help', 'version'
73 );
74 };
75 94 ▶ fail( ( @complaints ? $complaints[0] : 'bad options' ).' (try --help)' ) unless $ok;
Branch: 2 branches
BranchTrueFalseTotal
unless $ok 3 91 94
@complaints ? : 3 0 3
76 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
77  
78 fail( '--columns needs a positive integer, got "'.$opt{columns}.'"' )
79 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
80 fail( '--layout needs depth or packed, got "'.$opt{layout}.'"' )
81 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
82 fail( '--rows needs a positive integer, got "'.$opt{rows}.'"' )
83 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
84 83 ▶ if ( defined $opt{aspect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"aspect"}) 10 73 83
85 10 my $number = qr/[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9}/;
86 10 my ( $width, $height ) = $opt{aspect} =~ m{\A($number)(?:[:/]($number))?\z};
87 10 ▶ $height = 1 unless defined $height;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $height 4 6 10
88 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
89 unless defined $width && $width > 0 && $height > 0;
90 6 $opt{aspect} = $width / $height;
91 }
92 fail( '--size needs a positive number, got "'.$opt{size}.'"' )
93 if defined $opt{size}
94 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
95 fail( '--blink-seconds needs a positive number, got "'.$opt{'blink-seconds'}.'"' )
96 if defined $opt{'blink-seconds'}
97 && !( $opt{'blink-seconds'} =~ /\A(?:[0-9]{1,9}(?:\.[0-9]{1,9})?|\.[0-9]{1,9})\z/
98 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
99 73 ▶ $opt{blink} = [ grep { length } map { split /,/ } @{ $opt{blink} } ] if $opt{blink};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"blink"} 8 65 73
100 73 ▶ $opt{theme} = theme( $opt{color} ) if $opt{color};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"color"} 19 54 73
101 69 ▶ fail( 'more than one input file (try --help)' ) if @$argv > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$argv > 1 1 68 69
102 68 return \%opt;
103 }
104  
105 # The --color values as a theme: KEY=COLOUR, or KEY=LIGHT,DARK with the comma
106 # outside parentheses, so rgb(1,2,3) stays one colour. Whether a key or a
107 # colour is one the picture knows is left to the module, which ignores what
108 # it does not accept; the last --color of a key wins.
109 19 ▶sub theme {
Subroutine theme: called CC=6 SCAR=17.9
110 19 my ( $colors ) = @_;
111 19 my %theme;
112 19 for my $color (@$colors) {
113 21 my ( $key, $value ) = $color =~ /\A([^=]+)=(.+)\z/s;
114 21 ▶ my @modes = defined $value ? split( /,(?![^(]*\))/, $value, -1 ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 18 3 21
115 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
116 unless @modes == 1 || @modes == 2;
117 17 ▶ $theme{$key} = @modes == 1 ? $modes[0] : { light => $modes[0], dark => $modes[1] };
Branch: 1 branch
BranchTrueFalseTotal
@modes == 1 ? : 10 7 17
118 }
119 15 return \%theme;
120 }
121  
122 68 ▶sub read_input {
Subroutine read_input: called CC=7 SCAR=19.7
123 68 my ( $file ) = @_;
124 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
125 5 binmode STDIN;
126 5 local $/;
127 5 my $bytes = <STDIN>;
128 5 ▶ return ( defined $bytes ? $bytes : '', 'standard input' );
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes ? : 5 0 5
129 }
130 63 my $bytes = eval { path($file)->slurp_raw };
131 63 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 2 61 63
132 fail( 'cannot read '.$file.': '
133 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
134 }
135 61 return ( $bytes, $file );
136 }
137  
138 94 ▶sub main {
Subroutine main: called CC=19 SCAR=29.4
139 94 my @argv = @_;
140 94 utf8::decode($_) for @argv;
141 94 my $opt = options( \@argv );
142  
143 70 ▶ if ( $opt->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"help"}) 1 69 70
144 1 print usage();
145 1 return 0;
146 }
147 69 ▶ if ( $opt->{version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"version"}) 1 68 69
148 1 print $ME.' '.$VERSION."\n";
149 1 return 0;
150 }
151  
152 68 my ( $bytes, $source ) = read_input( $argv[0] );
153 66 my $combs = eval { JSON::MaybeXS->new( utf8 => 1, allow_nonref => 1 )->decode($bytes) };
154 66 ▶ fail( 'invalid JSON in '.$source.': '.reason($@) ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 63 66
155 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
156 unless ref $combs eq 'HASH' || ref $combs eq 'ARRAY';
157  
158 62 my $svg = eval {
159 svg_class()->new(
160 combs => $combs,
161 legend => $opt->{legend},
162 defined $opt->{edges} ? ( edges => $opt->{edges} ) : (),
163 defined $opt->{title} ? ( title => $opt->{title} ) : (),
164 defined $opt->{'group-label'} ? ( group_label => $opt->{'group-label'} ) : (),
165 defined $opt->{layout} ? ( layout => $opt->{layout} ) : (),
166 defined $opt->{columns} ? ( columns => $opt->{columns} + 0 ) : (),
167 defined $opt->{rows} ? ( rows => $opt->{rows} + 0 ) : (),
168 defined $opt->{aspect} ? ( aspect => $opt->{aspect} ) : (),
169 defined $opt->{size} ? ( size => $opt->{size} + 0 ) : (),
170 defined $opt->{theme} ? ( theme => $opt->{theme} ) : (),
171 defined $opt->{blink} ? ( blink => $opt->{blink} ) : (),
172 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
173 )->render;
174 };
175 62 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 61 62
176 1 $error = reason($error);
177 1 $error =~ s/\A[\w:]+->\w+: //;
178 1 fail($error);
179 }
180  
181 61 print $svg;
182 61 return 0;
183 }
184  
185 94 exit main(@ARGV);
186  
187 __END__
↑ Summary blib/lib/Kubernetes/Comb/SVG/Layout.pm »