Permalink
Browse files

options changes, add MOJO_SECRET_OPTIONS var

  • Loading branch information...
sshaw
sshaw committed Jun 7, 2012
1 parent 90d5b73 commit a8724435bcfd846a63e4ceb29474e236f0233bf7
Showing with 45 additions and 16 deletions.
  1. +1 −0 INSTALL.SKIP
  2. +13 −5 README.pod
  3. +31 −11 lib/Mojolicious/Command/secret.pm
View
@@ -0,0 +1 @@
+README.*
View
@@ -2,24 +2,32 @@
=head1 NAME
-Mojolicious::Command::secret - Generate a secret() using random bytes and add it to your application
+Mojolicious::Command::secret - Create an application secret() consisting of random bytes
=head1 DESCRIPTION
-Tired of manually adding secrets? Me too!
+Tired of manually creating and adding secrets? Me too! Use this command to create a secret
+and add it to your C<Mojolicous> or C<Mojolicious::Lite> application:
./script/your_app secret
- perl ./lite_app.pl secret
+ ./lite_app secret
-B<This will modify your file>. An existing secret will not be overridden unless you the C<-f> option is used.
+B<This will modify the appropriate application file>, though an existing secret will not be overridden unless the C<-f> option is used.
+If you do not want to automatically add the secret to your application use the C<mojo> command or
+the C<-p> option and the secret will be printed to C<STDOUT> instead:
+
+ mojo secret
+ ./script/your_app secret -p
=head1 OPTIONS
-f, --force Overwrite an existing secret. Defaults to 0.
-g, --generator MODULE=method Module & method used to generate the secret. Defaults to Crypt::URandom=urandom.
- Set the MOJO_GEN_SECRET environment variable to override this.
+ -p, --print Print the secret, do not add it to your application.
-s, --size SIZE Number of bytes to use. Defaults to 32.
+ Default options can be added to the MOJO_SECRET_OPTIONS environment variable.
+
=head1 AUTHOR
Skye Shaw
@@ -1,3 +1,4 @@
+#package Mojolicious::Command::generate:secret;
package Mojolicious::Command::secret;
use Mojo::Base 'Mojo::Command';
@@ -7,33 +8,44 @@ use Getopt::Long qw(GetOptionsFromArray :config no_ignore_case no_auto_abbrev);
our $VERSION = '0.01';
-has description => "Generate a secret() consisting of random bytes and add it to your application\n";
+has description => "Create an application secret() consisting of random bytes\n";
has usage => <<USAGE;
usage $0 secret [OPTIONS]
OPTIONS:
-f, --force Overwrite an existing secret. Defaults to 0.
-g, --generator MODULE=method Module & method used to generate the secret. Defaults to Crypt::URandom=urandom.
- Set the MOJO_GEN_SECRET environment variable to override this.
+ -p, --print Just print the secret, do not add it to your application.
-s, --size SIZE Number of bytes to use. Defaults to 32.
+
+Default options can be added to the MOJO_SECRET_OPTIONS environment variable.
USAGE
sub run
{
my ($self, @argv) = @_;
- my ($force, $size, $module);
+ unshift @argv, split /\s+/, $ENV{MOJO_SECRET_OPTIONS} if $ENV{MOJO_SECRET_OPTIONS};
+
+ my ($force, $size, $module, $print);
my $ok = GetOptionsFromArray(\@argv,
'f|force' => \$force,
's|size=i' => \$size,
+ 'p|print' => \$print,
'g|generator=s' => \$module);
-
return unless $ok;
my $secret = _create_secret($module, $size);
my $code = sprintf q|->secret('%s');|, $secret;
my $filename = $self->class_to_path(ref($self->app));
my $path = $filename eq 'Mojolicious/Lite.pm' ? $0 : File::Spec->catdir('lib', $filename);
+ # If we're called as `mojo` just print the secret
+ my $base = join '/', (File::Spec->splitdir($path))[-2,-1];
+ if($print || $base eq 'bin/mojo') {
+ say $secret;
+ return;
+ }
+
open my $io, '+<:encoding(utf8)', $path or die "Error opening $path: $!\n";
my $data = do { local $/; <$io> };
@@ -52,7 +64,7 @@ sub run
$data =~ s/^((\s*)\b(app)->\w+)/$2$3$code$1/m) {
$created = 1;
}
-
+
if(!$created) {
die "Can't figure out where to insert the call to secret()\n"
}
@@ -65,7 +77,7 @@ sub run
sub _create_secret
{
- my $module = shift || $ENV{'MOJO_GEN_SECRET'} || 'Crypt::URandom=urandom';
+ my $module = shift || 'Crypt::URandom=urandom';
my $size = shift || 32;
my ($class, $method) = split /=/, $module, 2;
@@ -91,24 +103,32 @@ sub _create_secret
=head1 NAME
-Mojolicious::Command::secret - Generate a secret() using random bytes and add it to your application
+Mojolicious::Command::secret - Create an application secret() consisting of random bytes
=head1 DESCRIPTION
-Tired of manually adding secrets? Me too!
+Tired of manually creating and adding secrets? Me too! Use this command to create a secret
+and add it to your C<Mojolicous> or C<Mojolicious::Lite> application:
./script/your_app secret
- perl ./lite_app.pl secret
+ ./lite_app secret
-B<This will modify your file>. An existing secret will not be overridden unless the C<-f> option is used.
+B<This will modify the appropriate application file>, though an existing secret will not be overridden unless the C<-f> option is used.
+If you do not want to automatically add the secret to your application use the C<mojo> command or
+the C<-p> option and the secret will be printed to C<STDOUT> instead:
+
+ mojo secret
+ ./script/your_app secret -p
=head1 OPTIONS
-f, --force Overwrite an existing secret. Defaults to 0.
-g, --generator MODULE=method Module & method used to generate the secret. Defaults to Crypt::URandom=urandom.
- Set the MOJO_GEN_SECRET environment variable to override this.
+ -p, --print Print the secret, do not add it to your application.
-s, --size SIZE Number of bytes to use. Defaults to 32.
+ Default options can be added to the MOJO_SECRET_OPTIONS environment variable.
+
=head1 AUTHOR
Skye Shaw

0 comments on commit a872443

Please sign in to comment.