File Coverage

lib/SweetPea/Cli/Tool.pm
Criterion Covered Total %
statement 21 59 35.5
branch 0 12 0.0
condition 0 3 0.0
subroutine 7 9 77.7
pod 0 2 0.0
total 28 85 32.9


line stmt bran cond sub pod time code
1             package SweetPea::Cli::Tool;
2              
3 1     1   9 use warnings;
  1         2  
  1         48  
4 1     1   6 use strict;
  1         2  
  1         210  
5              
6 1     1   8 use SweetPea;
  1         2  
  1         383  
7 1     1   152 use SweetPea::Cli::Util;
  1         3  
  1         31  
8 1     1   9 use SweetPea::Cli::Flash;
  1         2  
  1         36  
9 1     1   14 use SweetPea::Cli::Error;
  1         2  
  1         21  
10 1     1   5 use SweetPea::Cli::Help;
  1         1  
  1         7428  
11              
12             # SweetPea::Cli::Tool - Misc tools for SweetPea-Cli
13              
14             sub new {
15 0     0 0   my $class = shift;
16 0           my $self = {};
17 0           bless $self, $class;
18            
19 0           my $c = $self->{c} = shift;
20 0           my $f = SweetPea::Cli::Flash->new;
21 0           my $e = SweetPea::Cli::Error->new;
22            
23 0           $self->{commands} = [
24             #{
25             # name => 'sqlc',
26             # code => sub {
27             # $self->sql_client(@_)
28             # },
29             # args => {
30             # 'executable' => {
31             # aliases => ['e']
32             # }
33             # },
34             # help => 'launch the sql client for your database.'
35             #}
36             {
37            
38             }
39             ];
40            
41 0           return $self;
42             }
43              
44             sub sql_client {
45 0     0 0   my $self = shift;
46 0           my $c = shift;
47 0           my $f = SweetPea::Cli::Flash->new;
48 0           my $e = SweetPea::Cli::Error->new;
49 0           my $u = SweetPea::Cli::Util->new;
50 0           my $h = SweetPea::Cli::Help->new;
51 0           my $s = SweetPea::Application::Config->new(
52             SweetPea::Application->new,
53             Cwd::getcwd
54             );
55            
56 0           my $executables = {
57             'DB2' => '?',
58             'Oracle' => '?',
59             'PostgrSQL' => 'psql -d db -u user -p pass',
60             'MySQL' => 'mysql -d db -u user -p pass',
61             'SQLite' => 'sqlite3 db',
62             'SQLServer' => '?',
63             'Sybase' => ''
64             };
65            
66 0 0         unless ( -d "./sweet/configuration" )
67             {
68 0           return $e->error('No application configured.',
69             'Try `help make;` for instructions on creating an application.'
70             )->report($c);
71             }
72            
73 0           my $application = $s->get('/application');
74 0           my $datastore = $s->get('/datastores');
75 0           my $store = $application->{datastore};
76 0           my $data = $datastore->{datastores}->{$store};
77 0           my $conn = $data->{dsn};
78 0           my $user = $data->{username};
79 0           my $pass = $data->{password};
80 0           my ($scheme, $driver, @trash) = DBI->parse_dsn($conn);
81 0           my $client = $executables->{$driver};
82            
83 0 0         unless ( -d "./sweet/configuration/datastores/$store/table" )
84             {
85 0           return $e->error('No database configured.',
86             'Try `help data;` for instructions on configuring your datastores.'
87             )->report($c);
88             }
89            
90 0 0 0       if ($client && $client ne '?') {
91 0 0         $client =~ s/ db/ $scheme/ if $scheme;
92 0 0         $client =~ s/ user/ $user/ if $user;
93 0 0         $client =~ s/ pass/ $pass/ if $pass;
94            
95 0           system($client);
96            
97 0           my @return = (
98             'SQL client was invoked with `' . $client . '`',
99             'Note! SQL client host and port parameters not supported.'
100             );
101 0           return $f->flash(@return)->report($c);
102             }
103             else {
104 0           my @return = (
105             'SQL client not recognized or is not yet supported.',
106             'Note! Sql client host and port parameters not supported.'
107             );
108 0           return $e->error(@return)->report($c);
109             }
110            
111             }
112              
113             1; # End of SweetPea::Cli::Tool