]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - make/console.pm
Install the provider configs.
[user/henk/code/inspircd.git] / make / console.pm
index 84fbaae4a3aff44f3ec2bf8df8eadc3fed558d14..132544caafe8b00d39b1ec3062eca25b11e72c41 100644 (file)
@@ -1,7 +1,7 @@
 #
 # InspIRCd -- Internet Relay Chat Daemon
 #
-#   Copyright (C) 2014 Peter Powell <petpow@saberuk.com>
+#   Copyright (C) 2014-2017 Peter Powell <petpow@saberuk.com>
 #
 # 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
@@ -27,11 +27,14 @@ 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
@@ -39,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",
@@ -48,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;
@@ -81,8 +92,12 @@ sub print_warning {
 
 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($$$;$) {
@@ -111,4 +126,38 @@ sub prompt_string($$$) {
        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;