File Coverage

blib/lib/Ado/Command/generate/apache2vhost.pm
Criterion Covered Total %
statement 26 27 96.3
branch 7 14 50.0
condition 7 19 36.8
subroutine 3 3 100.0
pod 1 1 100.0
total 44 64 68.7


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