blib/lib/Galileo/Command/setup.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 121 CC 9 SCAR 40.7
CC
9
Cov
0%
run
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 58.6
1 package Galileo::Command::setup;
2 use Mojo::Base 'Mojolicious::Command';
3  
4 use Mojolicious::Command::daemon;
5  
6 use Mojolicious::Routes;
7 use Mojo::JSON 'j';
8 use Mojo::Util 'spurt';
9  
10 has description => "Configure your Galileo CMS via a web interface\n";
11  
12 sub run {
13 my ($self, @args) = @_;
14  
15 my $app = $self->app;
16 $app->plugin('Galileo::Plugin::Deploy'); # provides dh helper
17  
18 my $r = Mojolicious::Routes->new;
19 $app->routes($r); # remove all routes
20  
21 $app->helper( 'control_group' => sub {
22 my $self = shift;
23 my $contents = pop;
24 my %args = @_;
25  
26 $self->render_to_string(
27 template => 'setup/control_group',
28 'control_group.contents' => ref $contents ? $contents->() : $contents,
29 'control_group.label' => $args{label} || '',
30 'control_group.for' => $args{for} || '',
31 );
32 });
33  
34 $r->any( '/' => 'setup/welcome' );
35 $r->any( '/configure' => 'setup/configure' );
36 $r->any( '/store_config' => sub {
37 my $self = shift;
38 my $names = $self->req->params->names;
39  
40 # map JSON keys to Perl data
41 my %params = map { $_ => scalar $self->param($_) } @$names;
42 foreach my $key ( qw/extra_css extra_js extra_static_paths secrets db_options pagedown_extra_options/ ) {
43 $params{$key} = j($params{$key});
44 }
45  
46 spurt $self->dumper(\%params), $self->app->config_file;
47  
48 $self->app->load_config;
49 $self->humane_flash( 'Configuration saved' );
50 $self->redirect_to('/');
51 });
52  
53 $r->any( '/database' => sub {
54 my $self = shift;
55  
56 my $dh = $self->dh;
57  
58 # Nothing installed
59 my $installed = $dh->installed_version;
60 unless ( $installed ) {
61 return $self->render( 'setup/database' );
62 }
63  
64 # Something is installed, check for upgrades
65 my $available = $dh->schema->schema_version;
66  
67 # Do nothing if version is current
68 if ( $installed == $available ) {
69 $self->flash( 'galileo.message' => 'Database schema is current.' );
70 } else {
71 $dh->do_upgrade;
72 $self->flash( 'galileo.message' => "Upgrade database $installed -> $available." );
73 }
74  
75 $self->redirect_to('finish');
76 });
77  
78 $r->any( '/database_install' => sub {
79 my $self = shift;
80 my $pw1 = $self->param('pw1');
81 my $pw2 = $self->param('pw2');
82 unless ( $pw1 eq $pw2 ) {
83 $self->humane_flash( q{Passwords don't match!} );
84 return $self->redirect_to('database');
85 }
86  
87 my $dh = $self->dh;
88  
89 eval {
90 my $user = $self->param('user') || die "Admin Username is required\n";
91 my $full = $self->param('full');
92 $dh->do_install;
93 $dh->inject_sample_data($user, $pw1, $full);
94 };
95  
96 if ($@) {
97 my $error = "$@";
98 chomp $error;
99 $self->humane_flash( $error );
100 return $self->redirect_to('database');
101 }
102  
103 $self->flash( 'galileo.message' => 'Database has been setup.' );
104 $self->redirect_to('finish');
105 });
106  
107 $r->any('/finish' => sub {
108 my $self = shift;
109 my $message = $self->flash( 'galileo.message' );
110  
111 my $dh = $self->dh;
112 my $installed = $dh->installed_version;
113 my $available = $dh->schema->schema_version;
114 my $has_admin = $dh->has_admin_user;
115  
116 if ($installed) {
117 unless ($has_admin) {
118 $message .= ' No administration user was created.';
119 }
120 unless ($installed == $available) {
121 $message .= " Installed database version ($installed) is older than the newest available ($available).";
122 }
123 $self->stash( 'galileo.success' => 1 );
124 $self->stash( 'galileo.message' => $message );
125 } else {
126 $self->stash( 'galileo.success' => 0 );
127 $self->stash(
128 'galileo.message' =>
129 'It does not appear that your database is setup, please rerun the setup utility'
130 );
131 }
132  
133 $self->humane_stash( 'Goodbye' );
134 $self->render('setup/finish');
135 $self->tx->on( finish => sub { exit } );
136 });
137  
138 $self->Mojolicious::Command::daemon::run(@args);
139 }
140  
141 1;
142