OSDN Git Service

Command: add Newslash::Command and implement danger/experimental flag
authorhylom <hylom@users.sourceforge.jp>
Fri, 17 Aug 2018 09:54:11 +0000 (18:54 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 17 Aug 2018 09:54:11 +0000 (18:54 +0900)
src/newslash_web/lib/Newslash/Command.pm [new file with mode: 0644]
src/newslash_web/lib/Newslash/Command/useradd.pm
src/newslash_web/lib/Newslash/Command/usermod.pm

diff --git a/src/newslash_web/lib/Newslash/Command.pm b/src/newslash_web/lib/Newslash/Command.pm
new file mode 100644 (file)
index 0000000..8ffe76e
--- /dev/null
@@ -0,0 +1,57 @@
+package Newslash::Command;
+use Mojo::Base 'Mojolicious::Command';
+use List::Util qw(all);
+
+has experimental => 0;
+has danger => 0;
+
+
+sub check_force_option {
+    my ($self, $args) = @_;
+
+    if ($self->experimental) {
+        my $mode = $self->app->mode;
+        if (!$mode || $mode ne "test") {
+            if (all { $_ !~ /^\s*-f\s*$/ } @$args) {
+                $self->app->log->error("This command is experimental, so needs '-f' option under '$mode' mode.");
+                return;
+            }
+            @$args = grep { $_ !~ /^\s*-f\s*$/ } @$args;
+        }
+    }
+
+    if ($self->danger) {
+        if (any { $_ !~ /^\s*-f\s*$/ } @$args) {
+            $self->app->log->error("This command is dangerous, so needs '-f' option.");
+            return;
+        }
+        @$args = grep { $_ !~ /^\s*-f\s*$/ } @$args;
+    }
+
+    return 1;
+}
+
+1;
+
+=encoding utf8
+
+=head1 NAME
+
+Newslash::Command - newslash administer command base class
+
+=head1 METHODS
+
+L<Newslash::Command> inherits all methods from
+L<Mojolicious::Command> and implements the following new ones.
+
+=head2 check_options
+
+  $daemon->run(@ARGV);
+
+Run this command.
+
+=head1 SEE ALSO
+
+L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
+
+=cut
index d013ac0..a584a68 100644 (file)
@@ -1,16 +1,20 @@
 package Newslash::Command::useradd;
-use Mojo::Base 'Mojolicious::Command';
+use Mojo::Base 'Newslash::Command';
 
 use Mojo::Util 'getopt';
 use Data::Dumper;
 
 has description => 'Add new user';
-#has usage => sub { shift->extract_usage };
+has usage => sub { shift->extract_usage };
+
+has experimental => 1;
+has danger => 0;
 
 sub run {
     my ($self, @args) = @_;
-    my $app = $self->app;
+    return if !$self->check_force_option(\@args);
 
+    my $app = $self->app;
     my $seclev = 1;
     my $author = 0;
     my $password = "";
index 27223cc..a978e82 100644 (file)
@@ -1,16 +1,20 @@
 package Newslash::Command::usermod;
-use Mojo::Base 'Mojolicious::Command';
+use Mojo::Base 'Newslash::Command';
 
 use Mojo::Util 'getopt';
 use Data::Dumper;
 
 has description => 'Change user properties';
-#has usage => sub { shift->extract_usage };
+has usage => sub { shift->extract_usage };
+
+has experimental => 1;
+has danger => 0;
 
 sub run {
     my ($self, @args) = @_;
-    my $app = $self->app;
+    return if !$self->check_force_option(\@args);
 
+    my $app = $self->app;
     my $seclev;
     my $author;
     my $password;