This repository was archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathlogbot-util
More file actions
executable file
·55 lines (47 loc) · 1.38 KB
/
logbot-util
File metadata and controls
executable file
·55 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/perl
use local::lib;
use v5.10;
use strict;
use warnings;
use FindBin qw( $RealBin );
use lib "$RealBin/lib";
BEGIN {
$ENV{TZ} = 'UTC';
}
$| = 1;
use List::Util qw( first );
use LogBot::Config qw( find_config load_all_configs load_config );
use Module::Load qw( load );
use Mojo::Util qw( trim );
use Try::Tiny qw( catch try );
my @configs;
if (@ARGV && $ARGV[0] eq '--all') {
shift;
@configs = values %{ load_all_configs(all => !!$ENV{DEBUG}) };
} else {
push @configs, load_config(find_config(shift));
}
my @commands;
foreach my $file (glob("$RealBin/lib/LogBot/CLI/*.pm")) {
(my $class = substr($file, length("$RealBin/lib/"), -3)) =~ s{/}{::}g;
load($file);
push @commands, $class->manifest();
$commands[-1]->{class} = $class;
}
@commands = sort { $a->{command} cmp $b->{command} } @commands;
my ($command, @args) = (lc(shift // ''), @ARGV);
$command = first { $_->{command} eq $command } @commands;
unless (@configs && $command) {
my $usage = "syntax: logbot-util <config>|--all <command> [command args]\ncommands:\n";
foreach my $c (@commands) {
$usage .= sprintf(" %-16s %s\n", $c->{command}, $c->{help});
}
die $usage;
}
try {
$command->{class}->execute(\@configs, @args);
}
catch {
my $error = trim($_);
die $error eq 'syntax' ? 'syntax: logbot-util <config>|--all ' . $command->{usage} . "\n" : "$error\n";
};