| 1 | package Mojo::Command::Generate::InitScript; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use base 'Mojo::Commands'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | use File::Spec; | ||
| 8 | use Getopt::Long 'GetOptions'; | ||
| 9 | use Mojo::ByteStream 'b'; | ||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | __PACKAGE__->attr(description => <<'EOF'); | ||
| 14 | Generate application initscript (also known as rc.d script) | ||
| 15 | EOF | ||
| 16 | __PACKAGE__->attr(usage => <<"EOF"); | ||
| 17 | usage: $0 generate init_script target_os [OPTIONS] | ||
| 18 | |||
| 19 | These options are available: | ||
| 20 | --output <folder> Set folder to output initscripts | ||
| 21 | --deploy Deploy initscripts into OS | ||
| 22 | Either --deploy or --output=dist should be specified | ||
| 23 | |||
| 24 | --name <name> Ovewrite name which is used for initscript filename(s) | ||
| 25 | EOF | ||
| 26 | |||
| 27 | __PACKAGE__->attr(namespaces => sub { ['Mojo::Command::Generate::InitScript'] }); | ||
| 28 | |||
| 29 | |||
| 30 | =head1 NAME | ||
| 31 | |||
| 32 | Mojo::Command::Generate::InitScript - Initscript generator command | ||
| 33 | |||
| 34 | =head1 SYNOPSYS | ||
| 35 | |||
| 36 | $ ./mojo_app.pl generate help init_script | ||
| 37 | usage: ./mojo_app.pl generate init_script target_os [OPTIONS] | ||
| 38 | |||
| 39 | These options are available: | ||
| 40 | --output <folder> Set folder to output initscripts | ||
| 41 | --deploy Deploy initscripts into OS | ||
| 42 | Either --deploy or --output=dist should be specified | ||
| 43 | |||
| 44 | --name <name> Ovewrite name which is used for initscript filename(s) | ||
| 45 | |||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | our $VERSION = '0.03'; | ||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | sub run | ||
| 54 | { | ||
| 55 | my ( $self, $target ) = @_; | ||
| 56 | |||
| 57 | my $opt = {}; | ||
| 58 | |||
| 59 | Getopt::Long::Configure('pass_through'); | ||
| 60 | GetOptions( $opt, | ||
| 61 | 'output=s', 'name=s', 'deploy', | ||
| 62 | ); | ||
| 63 | |||
| 64 | if ( !( $opt->{'deploy'} || $opt->{'output'} ) ) | ||
| 65 | { | ||
| 66 | die qq{Either --deploy or --output <folder> should be specified\n}; | ||
| 67 | } | ||
| 68 | if ( $opt->{'deploy'} && $opt->{'output'} ) | ||
| 69 | { | ||
| 70 | die qq{Either --deploy or --output <folder> should be specified but not both\n}; | ||
| 71 | } | ||
| 72 | |||
| 73 | if ( $opt->{'deploy'} && !$self->user_is_root ) | ||
| 74 | { | ||
| 75 | die qq{You must be root to deploy init script\n}; | ||
| 76 | } | ||
| 77 | |||
| 78 | if ( !$opt->{'name'} ) | ||
| 79 | { | ||
| 80 | my ( $vol, $folder, $filename ) = File::Spec->splitpath( $0 ); | ||
| 81 | ($opt->{'name'}) = $filename =~ m/^(.*?)(?:\.pl)?$/; | ||
| 82 | } | ||
| 83 | |||
| 84 | $opt->{'app_script'} = File::Spec->rel2abs( $0 ); | ||
| 85 | |||
| 86 | $self->SUPER::run( $target, $opt, @_ ); | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | sub user_is_root | ||
| 91 | { | ||
| 92 | my $self = shift; | ||
| 93 | return $> == 0 || $< == 0 | ||
| 94 | } | ||
| 95 | |||
| 96 | sub help | ||
| 97 | { | ||
| 98 | my $self = shift; | ||
| 99 | my $name = pop @ARGV; | ||
| 100 | if ( $name eq 'init_script' ) | ||
| 101 | { | ||
| 102 | print $self->usage; | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | my $module; | ||
| 106 | for my $namespace (@{$self->namespaces}) | ||
| 107 | { | ||
| 108 | |||
| 109 | # Generate module | ||
| 110 | my $try = $namespace . '::' . b($name)->camelize; | ||
| 111 | |||
| 112 | # Load | ||
| 113 | if (my $e = Mojo::Loader->load($try)) { | ||
| 114 | |||
| 115 | # Module missing | ||
| 116 | next unless ref $e; | ||
| 117 | |||
| 118 | # Real error | ||
| 119 | die $e; | ||
| 120 | } | ||
| 121 | |||
| 122 | # Module is a command? | ||
| 123 | next unless $try->can('new') && $try->can('run'); | ||
| 124 | |||
| 125 | # Found | ||
| 126 | $module = $try; | ||
| 127 | last; | ||
| 128 | } | ||
| 129 | |||
| 130 | die qq/Command "$name" missing, maybe you need to install it?\n/ | ||
| 131 | unless $module; | ||
| 132 | |||
| 133 | my $command = $module->new; | ||
| 134 | print $self->usage, "\n", $command->usage; | ||
| 135 | } | ||
| 136 | |||
| 137 | =head1 AUTHOR | ||
| 138 | |||
| 139 | Anatoliy Lapitskiy, C<< <nuclon at cpan.org> >> | ||
| 140 | |||
| 141 | |||
| 142 | =head1 SUPPORT | ||
| 143 | |||
| 144 | You can find documentation for this module with the perldoc command. | ||
| 145 | |||
| 146 | perldoc Mojo::Command::Generate::InitScript | ||
| 147 | |||
| 148 | |||
| 149 | You can also look for information at: | ||
| 150 | |||
| 151 | =over 4 | ||
| 152 | |||
| 153 | =item * bitbucket repository | ||
| 154 | |||
| 155 | L<http://bitbucket.org/nuclon/mojo-command-generate-initscript/> | ||
| 156 | |||
| 157 | =item * Search CPAN | ||
| 158 | |||
| 159 | L<http://search.cpan.org/dist/Mojo-Command-Generate-InitScript/> | ||
| 160 | |||
| 161 | =back | ||
| 162 | |||
| 163 | |||
| 164 | =head1 ACKNOWLEDGEMENTS | ||
| 165 | |||
| 166 | |||
| 167 | =head1 LICENSE AND COPYRIGHT | ||
| 168 | |||
| 169 | Copyright 2010 Anatoliy Lapitskiy. | ||
| 170 | |||
| 171 | This program is free software; you can redistribute it and/or modify it | ||
| 172 | under the terms of either: the GNU General Public License as published | ||
| 173 | by the Free Software Foundation; or the Artistic License. | ||
| 174 | |||
| 175 | See http://dev.perl.org/licenses/ for more information. | ||
| 176 | |||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | 1; # End of Mojo::Command::Generate::InitScript | ||