Commit 71667811 authored by Luc Didry's avatar Luc Didry

Merge branch 'fix-42' into 'development'

Fix #42 - Allow to remove multiple URLs at once with the CLI

See merge request luc/lstu!23
parents 99b06bf0 a8327b8c
Pipeline #69637 passed with stages
in 5 minutes and 48 seconds
......@@ -4,6 +4,7 @@ Revision history for Perl application Lstu
- Add option to store IP address of URL creator
- Add command to search URL with the IP address of its creator
- Add option to blacklist IP address
- Allow to remove multiple URLs at once with the CLI
0.17-3 2018-08-20
- Fix missing default db_path
......
......@@ -2,6 +2,7 @@
package Lstu::Command::url;
use Mojo::Base 'Mojolicious::Command';
use Mojo::Util qw(getopt);
use Mojo::Collection 'c';
use Lstu::DB::URL;
use FindBin qw($Bin);
use File::Spec qw(catfile);
......@@ -62,55 +63,71 @@ sub run {
}
getopt \@args,
'i|info=s' => \my $info,
'r|remove=s' => \my $remove,
's|search=s' => \my $search,
'ip=s' => \my $ip,
'y|yes' => \my $yes;
if ($info) {
my $u = get_short($c, $info);
print_infos($u->to_hash) if $u;
}
if ($remove) {
my $u = get_short($c, $remove);
if ($u) {
print_infos($u->to_hash);
my $confirm = ($yes) ? 'yes' : undef;
unless (defined $confirm) {
print 'Are you sure you want to remove this URL? [N/y] ';
$confirm = <STDIN>;
chomp $confirm;
'i|info=s{1,}' => \my @info,
'r|remove=s{1,}' => \my @remove,
's|search=s' => \my $search,
'ip=s' => \my $ip,
'y|yes' => \my $yes;
if (scalar @info) {
c(@info)->each(
sub {
my ($e, $num) = @_;
my $u = get_short($c, $e);
print_infos($u->to_hash) if $u;
}
if ($confirm =~ m/^y(es)?$/i) {
if ($u->delete) {
if (scalar(@{$config->{memcached_servers}})) {
$c->app->chi('lstu_urls_cache')->remove($remove);
);
}
if (scalar @remove) {
c(@remove)->each(
sub {
my ($e, $num) = @_;
my $u = get_short($c, $e);
if ($u) {
print_infos($u->to_hash);
my $confirm = ($yes) ? 'yes' : undef;
unless (defined $confirm) {
printf('Are you sure you want to remove this URL (%s)? [N/y] ', $e);
$confirm = <STDIN>;
chomp $confirm;
}
if ($confirm =~ m/^y(es)?$/i) {
if ($u->delete) {
if (scalar(@{$config->{memcached_servers}})) {
$c->app->chi('lstu_urls_cache')->remove($e);
}
say sprintf('Success: %s URL has been removed', $e);
} else {
say sprintf('Failure: %s URL has not been removed', $e);
}
} else {
say 'Answer was not "y" or "yes". Aborting deletion.';
}
say sprintf('Success: %s URL has been removed', $remove);
} else {
say sprintf('Failure: %s URL has not been removed', $remove);
}
} else {
say 'Answer was not "y" or "yes". Aborting deletion.';
}
}
);
}
if ($search) {
my $u = Lstu::DB::URL->new(app => $c->app)->search_url($search);
my @shorts;
$u->each(sub {
my ($e, $num) = @_;
push @shorts, $e->{short};
print_infos($e);
});
say sprintf('%d matching URLs', $u->size);
say sprintf("If you want to delete those URLs, please do:\n carton exec script/lstu url --remove %s", join(' ', @shorts)) if @shorts;
}
if ($ip) {
my $u = Lstu::DB::URL->new(app => $c->app)->search_creator($ip);
my @shorts;
$u->each(sub {
my ($e, $num) = @_;
push @shorts, $e->{short};
print_infos($e);
});
say sprintf('%d matching URLs', $u->size);
say sprintf("If you want to delete those URLs, please do:\n carton exec script/lstu url --remove %s", join(' ', @shorts)) if @shorts;
}
}
......@@ -140,7 +157,12 @@ sub print_infos {
EOF
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($u->{timestamp});
my $timestamp = sprintf('%d-%d-%d %d:%d:%d GMT', $year + 1900, ++$mon, $mday, $hour, $min, $sec);
say sprintf($msg, $u->{short}, $u->{url}, $u->{counter}, $timestamp, $u->{timestamp});
if ($u->{created_by}) {
$msg .= ' created_by : %s';
say sprintf($msg, $u->{short}, $u->{url}, $u->{counter}, $timestamp, $u->{timestamp}, $u->{created_by});
} else {
say sprintf($msg, $u->{short}, $u->{url}, $u->{counter}, $timestamp, $u->{timestamp});
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment