X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=make%2Fconsole.pm;h=98442ff771975e4eeda02288b7cfa4d5086faf2b;hb=e73d011e68c577df72c2f0eae235c32cd4261870;hp=9be5ef47c3ad40331e6c87776dce16bd55eeb845;hpb=35d80008d6cb55160d06dda51aebc716c4d6511b;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/make/console.pm b/make/console.pm index 9be5ef47c..98442ff77 100644 --- a/make/console.pm +++ b/make/console.pm @@ -1,7 +1,7 @@ # # InspIRCd -- Internet Relay Chat Daemon # -# Copyright (C) 2014 Peter Powell +# Copyright (C) 2014-2017, 2019 Sadie Powell # # This file is part of InspIRCd. InspIRCd is free software: you can # redistribute it and/or modify it under the terms of the GNU General Public @@ -20,17 +20,21 @@ package make::console; BEGIN { - require 5.8.0; + require 5.10.0; } +use feature ':5.10'; use strict; use warnings FATAL => qw(all); +use Class::Struct qw(struct); +use Exporter qw(import); use File::Path qw(mkpath); use File::Spec::Functions qw(rel2abs); -use Exporter qw(import); -our @EXPORT = qw(print_format +our @EXPORT = qw(command + execute_command + print_format print_error print_warning prompt_bool @@ -38,8 +42,9 @@ our @EXPORT = qw(print_format prompt_string); my %FORMAT_CODES = ( - DEFAULT => "\e[0m", - BOLD => "\e[1m", + DEFAULT => "\e[0m", + BOLD => "\e[1m", + UNDERLINE => "\e[4m", RED => "\e[1;31m", GREEN => "\e[1;32m", @@ -47,6 +52,13 @@ my %FORMAT_CODES = ( BLUE => "\e[1;34m" ); +my %commands; + +struct 'command' => { + 'callback' => '$', + 'description' => '$', +}; + sub __console_format($$) { my ($name, $data) = @_; return $data unless -t STDOUT; @@ -55,44 +67,48 @@ sub __console_format($$) { sub print_format($;$) { my $message = shift; - my $stream = shift || *STDOUT; - while ($message =~ /(<\|(\S+)\s(.+?)\|>)/) { + my $stream = shift // *STDOUT; + while ($message =~ /(<\|(\S+)\s(.*?)\|>)/) { my $formatted = __console_format $2, $3; $message =~ s/\Q$1\E/$formatted/; } print { $stream } $message; } -sub print_error($) { - my $message = shift; - print_format "<|RED Error:|> $message\n", *STDERR; +sub print_error { + print_format "<|RED Error:|> ", *STDERR; + for my $line (@_) { + print_format "$line\n", *STDERR; + } exit 1; } -sub print_warning($) { - my $message = shift; - print_format "<|YELLOW Warning:|> $message\n", *STDERR; +sub print_warning { + print_format "<|YELLOW Warning:|> ", *STDERR; + for my $line (@_) { + print_format "$line\n", *STDERR; + } } sub prompt_bool($$$) { my ($interactive, $question, $default) = @_; - my $answer = prompt_string($interactive, $question, $default ? 'y' : 'n'); - return $answer =~ /y/i; + while (1) { + my $answer = prompt_string($interactive, $question, $default ? 'yes' : 'no'); + return 1 if $answer =~ /^y(?:es)?$/i; + return 0 if $answer =~ /^no?$/i; + print_warning "\"$answer\" is not \"yes\" or \"no\". Please try again.\n"; + } } -sub prompt_dir($$$) { - my ($interactive, $question, $default) = @_; +sub prompt_dir($$$;$) { + my ($interactive, $question, $default, $create_now) = @_; my ($answer, $create); do { $answer = rel2abs(prompt_string($interactive, $question, $default)); $create = prompt_bool($interactive && !-d $answer, "$answer does not exist. Create it?", 'y'); - if ($create) { - my $mkpath = eval { - mkpath($answer, 0, 0750); - return 1; - }; - unless (defined $mkpath) { - print_warning "unable to create $answer!\n"; + if ($create && $create_now) { + unless (create_directory $answer, 0750) { + print_warning "unable to create $answer: $!\n"; $create = 0; } } @@ -106,8 +122,42 @@ sub prompt_string($$$) { print_format "$question\n"; print_format "[<|GREEN $default|>] => "; chomp(my $answer = ); - print "\n"; + say ''; return $answer ? $answer : $default; } +sub command($$$) { + my ($name, $description, $callback) = @_; + $commands{$name} = command->new; + $commands{$name}->callback($callback); + $commands{$name}->description($description); +} + +sub command_alias($$) { + my ($source, $target) = @_; + command $source, undef, sub(@) { + execute_command $target, @_; + }; +} + +sub execute_command(@) { + my $command = defined $_[0] ? lc shift : 'help'; + if ($command eq 'help') { + print_format "<|GREEN Usage:|> $0 <<|UNDERLINE COMMAND|>> [<|UNDERLINE OPTIONS...|>]\n\n"; + print_format "<|GREEN Commands:|>\n"; + for my $key (sort keys %commands) { + next unless defined $commands{$key}->description; + my $name = sprintf "%-15s", $key; + my $description = $commands{$key}->description; + print_format " <|BOLD $name|> # $description\n"; + } + exit 0; + } elsif (!$commands{$command}) { + print_error "no command called <|BOLD $command|> exists!", + "See <|BOLD $0 help|> for a list of commands."; + } else { + return $commands{$command}->callback->(@_); + } +} + 1;