PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Mojo/Command/Generate/InitScript.pm

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