]> git.refcnt.org Git - distdns.git/blobdiff - client.pl
List remote entries
[distdns.git] / client.pl
index 1d03898ac44b17cbe201696c8364aa04cf23f8a2..e573f9dadfe271826fb5d91844cd090ac4034cce 100755 (executable)
--- a/client.pl
+++ b/client.pl
@@ -36,6 +36,8 @@ my $VERSION = '0.05';
 
 my $conf_file = catfile($Bin, 'client.conf');
 
+sub _die { die "$0: [client] $_[0]" }
+
 sub usage
 {
     print <<"USAGE";
@@ -43,12 +45,13 @@ Usage: $0
     -d, --debug    server debugging
     -h, --help     this help screen
     -i, --init     initialize session data
+    -l, --list     list remote entries
 USAGE
     exit;
 }
 
 my %opts;
-GetOptions(\%opts, qw(d|debug h|help i|init)) or usage();
+GetOptions(\%opts, qw(d|debug h|help i|init l|list)) or usage();
 usage() if $opts{h};
 
 my $config = Config::Tiny->new;
@@ -58,13 +61,13 @@ my $get_config_opts = sub
 {
     my ($section, $options) = @_;
 
-    die "$0: Section $section missing in $conf_file\n" unless exists $config->{$section};
+    _die "Section $section missing in $conf_file\n" unless exists $config->{$section};
 
     my %options;
     @options{@$options} = @{$config->{$section}}{@$options};
 
     foreach my $option (@$options) {
-        die "$0: Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
+        _die "Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option};
     }
 
     return @options{@$options};
@@ -79,14 +82,14 @@ my $save_session = sub
 {
     my ($session) = @_;
 
-    open(my $fh, '>', $session_file) or die "Cannot open client-side $session_file for writing: $!\n";
+    open(my $fh, '>', $session_file) or _die "Cannot open $session_file for writing: $!\n";
     print {$fh} "$session\n";
     close($fh);
 };
 
 my $get_session = sub
 {
-    open(my $fh, '<', $session_file) or die "Cannot open client-side $session_file for reading: $!\nPerhaps try running --init\n";
+    open(my $fh, '<', $session_file) or _die "Cannot open $session_file for reading: $!\nPerhaps try running --init\n";
     my $session = do { local $/; <$fh> };
     chomp $session;
     close($fh);
@@ -102,6 +105,7 @@ my %params = (
     name    => $name,
     debug   => $opts{d} || false,
     init    => $opts{i} || false,
+    list    => $opts{l} || false,
     session => $session,
 );
 
@@ -116,17 +120,32 @@ if ($response->is_success) {
         $data = decode_json($response->decoded_content);
     } or exit;
 
-    die "$0: $data->{error}" if defined $data->{error};
+    die "$0: [server] $data->{error}" if defined $data->{error};
 
     $save_session->($session) if $opts{i};
 
+    if ($opts{l}) {
+        format STDOUT_TOP =
+IP                 Name               PC                      Netz
+=============================================================================
+.
+        foreach my $entry (sort { $a->{netz} cmp $b->{netz} } @{$data->{entries}}) {
+            format STDOUT =
+@<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<<<<<<    @<<<<<<<<<<<<<<
+@$entry{qw(ip name pc netz)}
+.
+            write;
+        }
+        exit;
+    }
+
     my %list;
     foreach my $entry (@{$data->{entries}}) {
         my $host = "$entry->{ip}\t" . join '.', @$entry{qw(name pc netz)};
         push @{$list{$entry->{netz}}}, $host;
     }
 
-    my $o = tie my @hosts, 'Tie::File', $hosts_file or die "$0: Cannot tie $hosts_file: $!\n";
+    my $o = tie my @hosts, 'Tie::File', $hosts_file or _die "Cannot tie $hosts_file: $!\n";
     $o->flock(LOCK_EX);
 
     foreach my $network (keys %list) {