line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: App::Spec Plugin for formatting data structures |
2
|
2
|
|
|
2
|
|
959
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
61
|
|
3
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
95
|
|
4
|
|
|
|
|
|
|
package App::Spec::Plugin::Format; |
5
|
|
|
|
|
|
|
our $VERSION = '0.013'; # VERSION |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
12
|
use YAML::PP; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
91
|
|
8
|
2
|
|
|
2
|
|
11
|
use Ref::Util qw/ is_arrayref /; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
85
|
|
9
|
2
|
|
|
2
|
|
11
|
use Encode; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
161
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
11
|
use Moo; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
11
|
|
12
|
|
|
|
|
|
|
with 'App::Spec::Role::Plugin::GlobalOptions'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $yaml; |
15
|
|
|
|
|
|
|
my $options; |
16
|
|
|
|
|
|
|
sub _read_data { |
17
|
25
|
100
|
|
25
|
|
120
|
unless ($yaml) { |
18
|
2
|
|
|
|
|
4
|
$yaml = do { local $/; }; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
80
|
|
19
|
2
|
|
|
|
|
11
|
($options) = YAML::PP::Load($yaml); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub install_options { |
25
|
25
|
|
|
25
|
1
|
107
|
my ($class, %args) = @_; |
26
|
25
|
|
|
|
|
119
|
_read_data(); |
27
|
25
|
|
|
|
|
15872
|
return $options; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub init_run { |
31
|
26
|
|
|
26
|
1
|
100
|
my ($self, $run) = @_; |
32
|
26
|
|
|
|
|
154
|
$run->subscribe( |
33
|
|
|
|
|
|
|
print_output => { |
34
|
|
|
|
|
|
|
plugin => $self, |
35
|
|
|
|
|
|
|
method => "print_output", |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub print_output { |
41
|
2
|
|
|
2
|
1
|
6
|
my ($self, %args) = @_; |
42
|
2
|
|
|
|
|
5
|
my $run = $args{run}; |
43
|
2
|
|
|
|
|
4
|
my $opt = $run->options; |
44
|
2
|
|
50
|
|
|
9
|
my $format = $opt->{format} || ''; |
45
|
|
|
|
|
|
|
|
46
|
2
|
|
|
|
|
5
|
my $res = $run->response; |
47
|
2
|
|
|
|
|
5
|
my $outputs = $res->outputs; |
48
|
2
|
|
|
|
|
5
|
for my $out (@$outputs) { |
49
|
2
|
50
|
|
|
|
11
|
next unless $out->type eq 'data'; |
50
|
0
|
|
|
|
|
|
my $content = $out->content; |
51
|
0
|
0
|
0
|
|
|
|
if ($format eq 'YAML') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
$content = encode_utf8 YAML::PP::Dump($content); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
elsif ($format eq 'JSON') { |
55
|
0
|
|
|
|
|
|
require JSON::XS; |
56
|
0
|
|
|
|
|
|
my $coder = JSON::XS->new->ascii->pretty->allow_nonref; |
57
|
0
|
|
|
|
|
|
$content = encode_utf8 $coder->encode($content) . "\n"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
elsif ($format eq 'Table' and is_arrayref($content)) { |
60
|
0
|
|
|
|
|
|
require Text::Table; |
61
|
0
|
|
|
|
|
|
my $header = shift @$content; |
62
|
0
|
|
|
|
|
|
my $tb = Text::Table->new( @$header ); |
63
|
0
|
|
|
|
|
|
$tb->load(@$content); |
64
|
0
|
|
|
|
|
|
$content = encode_utf8 "$tb"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
elsif ($format eq 'Data::Dump') { |
67
|
0
|
|
|
|
|
|
require Data::Dump; |
68
|
0
|
|
|
|
|
|
$content = Data::Dump::dump($content) . "\n"; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
0
|
|
|
|
|
|
$content = Data::Dumper->Dump([$content], ['output']); |
72
|
|
|
|
|
|
|
} |
73
|
0
|
|
|
|
|
|
$out->content( $content ); |
74
|
0
|
|
|
|
|
|
$out->type( "plain" ); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=pod |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 NAME |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
App::Spec::Plugin::Format - App::Spec Plugin for formatting data structures |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head1 DESCRIPTION |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 METHODS |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=over 4 |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item install_options |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
This method is required by L. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
See L. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=item init_run |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
See L |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item print_output |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
This method is called by L right before output. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=back |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__DATA__ |