App-dnsq
view release on metacpan or search on metacpan
lib/DNSQuery/Interactive.pm view on Meta::CPAN
package DNSQuery::Interactive;
use strict;
use warnings;
use Term::ReadLine;
use DNSQuery::Resolver;
use DNSQuery::Validator qw(:all);
use DNSQuery::Constants qw(:all);
our $VERSION = '1.1.0';
sub new {
my ($class, $config, $resolver, $output) = @_;
return bless {
config => $config,
resolver => $resolver,
output => $output,
term => Term::ReadLine->new('dnsq'),
}, $class;
}
sub run {
my ($self) = @_;
print "Interactive Mode - Type 'help' for commands, 'quit' to exit\n\n";
my $prompt = "dnsq> ";
while (defined(my $input = $self->{term}->readline($prompt))) {
chomp $input;
$input =~ s/^\s+|\s+$//g;
next if $input eq '';
$self->{term}->addhistory($input) if $input =~ /\S/;
if ($input eq 'quit' || $input eq 'exit') {
last;
} elsif ($input eq 'help') {
$self->print_help();
} elsif ($input =~ /^set\s+(\w+)\s+(.+)$/) {
$self->set_config($1, $2);
} elsif ($input eq 'show' || $input eq 'stats') {
$self->show_config();
} elsif ($input eq 'clear cache') {
$self->{resolver}->clear_cache();
print "Cache cleared\n";
} else {
$self->process_query($input);
}
}
print "\nGoodbye!\n";
}
sub set_config {
my ($self, $key, $value) = @_;
unless (exists $self->{config}{$key}) {
print "Unknown setting: $key\n";
return;
}
# Validate specific settings using Validator module
my ($valid, $error);
if ($key eq 'port') {
($valid, $error) = validate_port($value);
unless ($valid) {
print "Error: $error\n";
return;
}
} elsif ($key eq 'timeout') {
($valid, $error) = validate_timeout($value);
unless ($valid) {
print "Error: $error\n";
return;
}
} elsif ($key eq 'retries') {
($valid, $error) = validate_retries($value);
unless ($valid) {
print "Error: $error\n";
return;
}
} elsif ($key eq 'protocol') {
unless ($value =~ /^(tcp|udp)$/i) {
print "Error: Protocol must be 'tcp' or 'udp'\n";
return;
}
$value = lc($value);
( run in 1.559 second using v1.01-cache-2.11-cpan-0b5f733616e )