| 1 | package Data::Collector::App; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 1 | $Data::Collector::App::VERSION = '0.15'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | # ABSTRACT: An application implementation for Data::Collector | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use File::HomeDir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use List::MoreUtils 'none'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Module::Pluggable::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use MooseX::Types::Path::Class 'File'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Data::Collector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | with qw/ MooseX::SimpleConfig MooseX::Getopt::Dashes /; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has '+configfile' => ( | ||||||||||||||||||||||||||||
| 20 | isa => 'Maybe[MooseX::Types::Path::Class::File]', | ||||||||||||||||||||||||||||
| 21 | 0 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 22 | 0 | my @files = ( | |||||||||||||||||||||||||||
| 23 | File::Spec->catfile( File::HomeDir->my_home, '.data_collector.yaml' ), | ||||||||||||||||||||||||||||
| 24 | '/etc/data_collector.yaml', | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | foreach my $file (@files) { | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | -e $file && -r $file and return file($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $file and -r $file
MC/DC0%0/2
-e $file and -r $file
-e $file
-r $file
Truth table0%0/3
A and B
A= -e $fileB= -r $file
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | return; | |||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | 1 | ); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | has 'engine' => ( is => 'ro', isa => 'Str', default => 'OpenSSH' ); | |||||||||||||||||||||||||||
| 36 | 1 | has 'format' => ( is => 'ro', isa => 'Str', default => 'JSON' ); | |||||||||||||||||||||||||||
| 37 | 1 | has 'os' => ( is => 'ro', isa => 'Str', default => 'CentOS' ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | has 'output' => ( | |||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||
| 41 | isa => File, | ||||||||||||||||||||||||||||
| 42 | predicate => 'has_output', | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | has [ qw/ engine_args format_args info_args / ] => ( | ||||||||||||||||||||||||||||
| 46 | is => 'ro', | ||||||||||||||||||||||||||||
| 47 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 1 | ); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | my @classes = Module::Pluggable::Object->new( | |||||||||||||||||||||||||||
| 52 | search_path => 'Data::Collector::Info', | ||||||||||||||||||||||||||||
| 53 | require => 1, # can use Class::MOP::load_class instead | ||||||||||||||||||||||||||||
| 54 | )->plugins; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | my @ignore_classes = qw/ | |||||||||||||||||||||||||||
| 57 | Data::Collector::Commands | ||||||||||||||||||||||||||||
| 58 | Data::Collector::Info | ||||||||||||||||||||||||||||
| 59 | /; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | foreach my $class (@classes) { | |||||||||||||||||||||||||||
| 62 | 0 | foreach my $attribute ( $class->meta->get_all_attributes ) { | |||||||||||||||||||||||||||
| 63 | 0 | my $name = $attribute->name; | |||||||||||||||||||||||||||
| 64 | 0 | my $assoc = $attribute->associated_class->name; | |||||||||||||||||||||||||||
| 65 | 0 | my $package = $attribute->definition_context->{'package'}; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( none { $package eq $_ } @ignore_classes ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | my @levels = split /\:\:/, $class; | |||||||||||||||||||||||||||
| 69 | 0 | my $level = lc $levels[-1]; | |||||||||||||||||||||||||||
| 70 | 0 | my $attr = "info_${level}_${name}"; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ( __PACKAGE__->meta->get_attribute($attr) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | die "Already have attribute by the name of $attr\n"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | __PACKAGE__->meta->add_attribute( | ||||||||||||||||||||||||||||
| 77 | 0 | $attribute->clone( name => $attr ) | |||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 0 | my $regex = qr/^info_(.+?)_(.+)$/; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | foreach my $attr ( $self->meta->get_attribute_list ) { | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ( $attr =~ $regex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | # bad jojo magambo | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if ( exists $self->{$attr} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | $self->info_args->{$1}{$2} = $self->{$attr}; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 99 | 0 | my $collector = Data::Collector->new( | |||||||||||||||||||||||||||
| 100 | os => $self->os, | ||||||||||||||||||||||||||||
| 101 | engine => $self->engine, | ||||||||||||||||||||||||||||
| 102 | engine_args => $self->engine_args, | ||||||||||||||||||||||||||||
| 103 | format => $self->format, | ||||||||||||||||||||||||||||
| 104 | format_args => $self->format_args, | ||||||||||||||||||||||||||||
| 105 | info_args => $self->info_args, | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $data = $collector->collect; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ( $self->has_output ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | my $file = $self->output; | |||||||||||||||||||||||||||
| 112 | 0 | write_file( $file, $data ); | |||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 0 | print "$data\n"; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | return 1; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 121 | 1 | 1; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =pod | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 NAME | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Data::Collector::App - An application implementation for Data::Collector | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 VERSION | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | version 0.15 | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | use Data::Collector::App; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | my $collector = Data::Collector::App->new_with_options(); | ||||||||||||||||||||||||||||
| 140 | $collector->run(); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | This module integrates all the checks and logics of an application. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | It supports getopt command line parsing and optional configuration files. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Using this implementation, one can write an application. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 configfile | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | An optional configuration file. If it exists, it is read and used for the | ||||||||||||||||||||||||||||
| 153 | value of the rest of these attributes (if they are present in the file). | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Default: C</etc/data_collector.yaml>. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 engine | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Type of engine (OpenSSH, for example). | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 engine_args | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Any additional arguments the engine might want. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 format | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Type of serialization (C<JSON> or C<YAML>, for example). | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =head2 format_args | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Any additional arguments the serializer might want. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head2 info_args | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Any additional arguments the Info module might want. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | You generally don't want to play with it, trust me. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 output | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | A file to output to. If one is not provided, it will output the serialized | ||||||||||||||||||||||||||||
| 182 | result to stdout. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head2 new | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Creates a new instance of the application interface. This is the clean way of | ||||||||||||||||||||||||||||
| 189 | doing it. You would probably prefer C<new_with_options> described below. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head2 new_with_options | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | The same as C<new>, only it parses command line arguments and takes care of | ||||||||||||||||||||||||||||
| 194 | reading a configuration file (if the correct argument for it is provided). | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head2 run | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Runs the application: starts a new collector, collects the informtion and - | ||||||||||||||||||||||||||||
| 199 | depending on the options - either outputs the result to the screen or to a | ||||||||||||||||||||||||||||
| 200 | file. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head2 BUILD | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Subroutine run after initialization. Used to create the C<info_args> attribute | ||||||||||||||||||||||||||||
| 205 | for the main C<Data::Collector>. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | Sawyer X <xsawyerx@cpan.org> | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | This software is copyright (c) 2012 by Sawyer X. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 216 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =cut | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | __END__ | ||||||||||||||||||||||||||||