File Coverage

blib/lib/Ado/Command/generate/apache2vhost.pm
Criterion Covered Total %
statement 29 30 96.6
branch 7 14 50.0
condition 7 19 36.8
subroutine 4 4 100.0
pod 1 1 100.0
total 48 68 70.5


line stmt bran cond sub pod time code
1             package Ado::Command::generate::apache2vhost;
2 2     2   1717 use Mojo::Base 'Ado::Command';
  2         4  
  2         8  
3 2     2   222 use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
  2         5  
  2         20  
4 2     2   321 use Mojo::File 'path';
  2         10  
  2         854  
5              
6             has description => "Generates minimal Apache2 Virtual Host configuration file.\n";
7             has usage => sub { shift->extract_usage };
8              
9             my $IS_DOS = ($^O eq 'MSWin32' or $^O eq 'dos' or $^O eq 'os2');
10              
11             sub run {
12 1     1 1 40 my ($self, @args) = @_;
13 1         4 state $app = $self->app;
14 1         9 state $home = $app->home;
15 1         5 state $ado_home = $app->ado_home;
16 1         9 my $args = $self->args;
17             GetOptionsFromArray \@args,
18             'n|ServerName=s' => \$args->{ServerName},
19             'p|port=i' => \($args->{port} //= 80),
20             'A|ServerAlias=s' => \$args->{ServerAlias},
21             'a|ServerAdmin=s' => \$args->{ServerAdmin},
22             'D|DocumentRoot=s' => \($args->{DocumentRoot} //= $home),
23             'c|config_file=s' => \$args->{config_file},
24             'v|verbose' => \$args->{verbose},
25             'u|user=s' => \$args->{user},
26             'g|group=s' => \$args->{group},
27 1   50     13 's|with_suexec' => \$args->{with_suexec};
      33        
28              
29 1 50       763 Carp::croak $self->usage unless $args->{ServerName};
30              
31             $args->{ServerAlias} //=
32 1 50 33     8 $$args{ServerName} =~ /^www\./ ? $$args{ServerName} : 'www.' . $$args{ServerName};
33 1   33     4 $args->{ServerAdmin} //= 'webmaster@' . $args->{ServerName};
34 1   50     418 $args->{user} //= ($ENV{USER} || getlogin || 'nobody');
      33        
35 1   33     6 $args->{group} //= $args->{user};
36 1 50       2 $args->{DocumentRoot} =~ s|\\|/|g if $IS_DOS;
37              
38 1 50       3 say STDERR 'Using arguments:' . $app->dumper($args) if $args->{verbose};
39 1         3 state $rel_file = 'templates/partials/apache2vhost.ep';
40 1 50       5 state $template_file = (
41             -s $home->rel_file($rel_file)
42             ? $home->rel_file($rel_file)
43             : $ado_home->rel_file($rel_file)
44             );
45 1         68 my $config = Mojo::Template->new->render_file($template_file, $args);
46 1 50       20 if ($args->{config_file}) {
47 1 50       4 say STDERR 'Writing ' . $args->{config_file} if $args->{verbose};
48 1         4 path($args->{config_file})->spurt($config);
49             }
50             else {
51 0         0 say $config;
52             }
53 1         161 return $self;
54             }
55              
56             1;
57              
58              
59             =pod
60              
61             =encoding utf8
62              
63             =head1 NAME
64              
65             Ado::Command::generate::apache2vhost - Generates minimal Apache2 Virtual Host configuration file
66              
67             =head1 SYNOPSIS
68              
69             On the command-line:
70              
71             $ bin/ado generate apache2vhost --ServerName example.com -s \
72             > etc/001-example.com.vhost.conf
73              
74             Review your newly generated C<001-example.com.vhost.conf>!!!
75             Create link to your generated configuration.
76              
77             # ln -siv /home/you/dev/Ado/etc/001-example.com.vhost.conf \
78             /etc/apache2/sites-enabled/001-example.com.vhost.conf
79              
80             # service apache2 reload
81              
82             Generate your C<.htaccess> file. Since you own the machine,
83             you can put its content into the C<001-example.com.vhost.conf> file.
84              
85             $ bin/ado generate apache2htaccess --modules fcgi \
86             > $MOJO_HOME/.htaccess
87              
88             Programmatically:
89              
90             use Ado::Command::generate::apache2vhost;
91             my $vhost = Ado::Command::generate::apache2vhost->new;
92             $vhost->run('--ServerName' => 'example.com', '-p' => 8080);
93              
94             =head1 DESCRIPTION
95              
96             L
97             generates a minimal Apache2 Virtual Host configuration file for your L application.
98              
99             This is a core command, that means it is always enabled and its code a good
100             example for learning to build new commands, you're welcome to fork it.
101              
102             =head1 OPTIONS
103              
104             Below are the options this command accepts described in L
105             notation.
106              
107             =head2 n|ServerName=s
108              
109             Fully Qualified Domain Name for the virtual host. B
110             See also documentation for Apache2 directive ServerName.
111              
112             =head2 p|port=i
113              
114             Port on which this host will be served. Defaults to 80.
115              
116             =head2 A|ServerAlias=s
117              
118             Alias for ServerName. Defaults to C<'www.'.$ServerName>.
119             See also documentation for Apache2 directive ServerAlias.
120              
121             =head2 a|ServerAdmin=s
122              
123             Email of the administrator for this host - you.
124             Defaults to webmaster@$ServerName.
125             See also documentation for Apache2 directive ServerAdmin.
126              
127              
128             =head2 D|DocumentRoot=s
129              
130             DocumentRoot for the virtual host. Defaults to C<$ENV{MOJO_HOME}>.
131             See also documentation for Apache2 directive DocumentRoot.
132              
133             =head2 c|config_file=s
134              
135             Full path to the file in which the configuration will be written.
136             If not provided the configuration is printed to the screen.
137              
138             =head2 s|with_suexec
139              
140             Adds C directive which is effective only
141             if C is loaded. The user and the group are guessed from the
142             user running the command.
143              
144             =head3 u|user=s
145              
146             User to be used with suexec.
147              
148             =head3 g|group=s
149              
150             Group to be used with suexec.
151              
152             =head3 v|verbose
153              
154             Verbose output.
155              
156             =head1 ATTRIBUTES
157              
158             L inherits all attributes from
159             L and implements the following new ones.
160              
161             =head2 args
162              
163             Used for storing arguments from the commandline and then passing them to the
164             template
165              
166             my $args = $self->args;
167              
168             =head2 description
169              
170             my $description = $vhost->description;
171             $v = $vhost->description('Foo!');
172              
173             Short description of this command, used for the command list.
174              
175             =head2 env
176              
177             Reference to C<%ENV>.
178              
179              
180             =head2 usage
181              
182             my $usage = $vhost->usage;
183             $v = $vhost->usage('Foo!');
184              
185             Usage information for this command, used for the help screen.
186              
187             =head1 METHODS
188              
189              
190             L inherits all methods from
191             L and implements the following new ones.
192              
193             =head2 run
194              
195             $vhost->run(@ARGV);
196              
197             Run this command. Returns C<$self>.
198              
199              
200             =head1 SEE ALSO
201              
202             L,
203             L,
204             L,
205             L,
206             L, L,
207             L L,
208             L, L
209              
210             =cut