File Coverage

blib/lib/DBIx/Migration/CLI.pm
Criterion Covered Total %
statement 74 74 100.0
branch 23 26 88.4
condition n/a
subroutine 16 16 100.0
pod 1 1 100.0
total 114 117 97.4


line stmt bran cond sub pod time code
1 1     1   200071 use strict;
  1         2  
  1         45  
2 1     1   6 use warnings;
  1         2  
  1         98  
3              
4             #https://stackoverflow.com/questions/24547252/podusage-help-formatting/24812485#24812485
5              
6             package DBIx::Migration::CLI;
7              
8             our $VERSION = '0.32';
9              
10 1     1   587 use DBIx::Migration ();
  1         14  
  1         89  
11 1     1   863 use Getopt::Std qw( getopts );
  1         2829  
  1         91  
12 1     1   9 use Log::Any ();
  1         2  
  1         35  
13 1     1   712 use Log::Any::Adapter ();
  1         599  
  1         37  
14 1     1   826 use Module::Load::Conditional qw( can_load );
  1         33790  
  1         112  
15 1     1   2657 use PerlX::Maybe qw( maybe );
  1         5588  
  1         6  
16 1     1   99 use POSIX qw( EXIT_FAILURE EXIT_SUCCESS );
  1         21  
  1         11  
17 1     1   97 use Try::Tiny qw( catch try );
  1         3  
  1         937  
18              
19             sub run {
20 12     12 1 335150 local @ARGV = @_;
21              
22 12         33 my $opts;
23             my $exitval;
24             {
25 12         35 local $SIG{ __WARN__ } = sub {
26 1     1   70 my $warning = shift;
27 1         2 chomp $warning;
28 1         7 $exitval = _usage( -exitval => 2, -message => $warning );
29 12         115 };
30 12         116 getopts( '-VT:hp:s:t:u:v', $opts = {} );
31             }
32 12 100       650 return $exitval if defined $exitval;
33              
34 11 100       84 if ( $opts->{ V } ) {
    100          
35 1         6 return _usage( -flavour => 'version' );
36             } elsif ( $opts->{ h } ) {
37 1         6 return _usage( -flavour => 'long' );
38             }
39              
40 9 100       32 return _usage( -exitval => 2, -message => 'Missing mandatory arguments' ) unless @ARGV;
41              
42 8         16 my $log_any_adapter_entry;
43             $log_any_adapter_entry = Log::Any::Adapter->set( { category => qr/\ADBIx::Migration/ }, 'Stderr' )
44 8 50       25 if exists $opts->{ v };
45 8         84 my $Logger = Log::Any->get_logger( category => 'DBIx::Migration' );
46             $exitval = try {
47 8     8   520 my $dsn = shift @ARGV;
48 8         59 my $driver = DBIx::Migration->driver( $dsn );
49 7         354 my $class = "DBIx::Migration::$driver";
50 7 50       99 $class = 'DBIx::Migration' unless can_load( modules => { $class => undef } );
51 7         2250 $Logger->infof( "Will use '%s' class to process migrations", $class );
52             my $m = $class->new(
53             dsn => $dsn,
54             maybe
55             password => $opts->{ p },
56             maybe
57             username => $opts->{ u },
58             maybe
59             managed_schema => $opts->{ s },
60             maybe
61             tracking_schema => $opts->{ t },
62             maybe tracking_table => $opts->{ T }
63 7         342 );
64 7 100       37 if ( @ARGV ) {
65 3         96 $m->dir( shift @ARGV );
66              
67 3 100       171 return ( $m->migrate( @ARGV ? shift @ARGV : () ) ? EXIT_SUCCESS : EXIT_FAILURE );
    100          
68             } else {
69 4         19 my $version = $m->version;
70 3 100       458 print STDOUT ( defined $version ? $version : '' ), "\n";
71              
72 3         270 return EXIT_SUCCESS;
73             }
74             } catch {
75 2     2   20556 chomp;
76 2         681 return _usage( -exitval => 2, -message => $_ );
77 8         973 };
78 8 50       289 Log::Any::Adapter->remove( $log_any_adapter_entry ) if defined $log_any_adapter_entry;
79              
80 8         129 return $exitval;
81             }
82              
83             sub _usage {
84 6     6   13 my %args;
85             {
86 1     1   11 use warnings FATAL => qw( misc uninitialized );
  1         2  
  1         308  
  6         14  
87 6         40 %args = ( -exitval => EXIT_SUCCESS, -flavour => 'short', @_ );
88             };
89              
90 6         56 require Pod::Find;
91 6         939 require Pod::Usage;
92              
93 6         71691 my %sections = ( long => 'SYNOPSIS|OPTIONS|ARGUMENTS', short => 'SYNOPSIS', version => 'VERSION' );
94             Pod::Usage::pod2usage(
95             -exitval => 'NOEXIT',
96             -indent => 2,
97             -input => Pod::Find::pod_where( { -inc => 1 }, __PACKAGE__ ),
98             exists $args{ -message } ? ( -message => $args{ -message } ) : (),
99             -output => ( $args{ -exitval } == EXIT_SUCCESS ) ? \*STDOUT : \*STDERR,
100             -sections => $sections{ $args{ -flavour } },
101 6 100       2444 -verbose => 99,
    100          
102             -width => 120
103             );
104              
105 6         147559 return $args{ -exitval };
106             }
107              
108             1;