]> git.refcnt.org Git - distdns.git/commitdiff
Make switches mutually exclusive
authorSteven Schubiger <stsc@refcnt.org>
Thu, 19 Sep 2013 19:00:29 +0000 (21:00 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Thu, 19 Sep 2013 19:00:29 +0000 (21:00 +0200)
client.pl
server.cgi

index 0563c5cf26602aba7054d500d40e83a45bffa973..aab5ea47f89691473f728b1ffd9b1c197e4ddd68 100755 (executable)
--- a/client.pl
+++ b/client.pl
@@ -40,8 +40,9 @@ sub _die { die "$0: [client] $_[0]" }
 
 sub usage
 {
 
 sub usage
 {
+    warn "$_[0]\n" if defined $_[0];
     print <<"USAGE";
     print <<"USAGE";
-Usage: $0
+Usage: $0 [options]
     -d, --debug    server debugging
     -h, --help     this help screen
     -i, --init     initialize session data
     -d, --debug    server debugging
     -h, --help     this help screen
     -i, --init     initialize session data
@@ -54,6 +55,8 @@ my %opts;
 GetOptions(\%opts, qw(d|debug h|help i|init l|list)) or usage();
 usage() if $opts{h};
 
 GetOptions(\%opts, qw(d|debug h|help i|init l|list)) or usage();
 usage() if $opts{h};
 
+usage('Cannot combine --init and --list') if $opts{i} && $opts{l};
+
 my $config = Config::Tiny->new;
    $config = Config::Tiny->read($conf_file);
 
 my $config = Config::Tiny->new;
    $config = Config::Tiny->read($conf_file);
 
@@ -122,9 +125,10 @@ if ($response->is_success) {
 
     die "$0: [server] $data->{error}" if defined $data->{error};
 
 
     die "$0: [server] $data->{error}" if defined $data->{error};
 
-    $save_session->($session) if $opts{i};
-
-    if ($opts{l}) {
+    if ($opts{i}) {
+        $save_session->($session);
+    }
+    elsif ($opts{l}) {
         format STDOUT_TOP =
 IP                 Name               PC                      Netz
 =============================================================================
         format STDOUT_TOP =
 IP                 Name               PC                      Netz
 =============================================================================
@@ -136,37 +140,37 @@ IP                 Name               PC                      Netz
 .
             write;
         }
 .
             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;
     }
     }
+    else {
+        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 "Cannot tie $hosts_file: $!\n";
-    $o->flock(LOCK_EX);
-
-    foreach my $network (keys %list) {
-        my %indexes;
-        for (my $i = 0; $i < @hosts; $i++) {
-            if ($hosts[$i] =~ /^\#$network\#$/i) {
-                $indexes{start} = $i;
-            }
-            elsif (exists $indexes{start} && $hosts[$i] =~ /^\#\/$network\#$/i) {
-                $indexes{end} = $i;
-                my $count = ($indexes{end} - $indexes{start} > 1)
-                  ? $indexes{end} - $indexes{start} - 1
-                  : 0;
-                splice @hosts, $indexes{start} + 1, $count, @{$list{$network}};
-                last;
+        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) {
+            my %indexes;
+            for (my $i = 0; $i < @hosts; $i++) {
+                if ($hosts[$i] =~ /^\#$network\#$/i) {
+                    $indexes{start} = $i;
+                }
+                elsif (exists $indexes{start} && $hosts[$i] =~ /^\#\/$network\#$/i) {
+                    $indexes{end} = $i;
+                    my $count = ($indexes{end} - $indexes{start} > 1)
+                      ? $indexes{end} - $indexes{start} - 1
+                      : 0;
+                    splice @hosts, $indexes{start} + 1, $count, @{$list{$network}};
+                    last;
+                }
             }
         }
             }
         }
-    }
 
 
-    undef $o;
-    untie @hosts;
+        undef $o;
+        untie @hosts;
+    }
 }
 else {
     warn $response->status_line, "\n";
 }
 else {
     warn $response->status_line, "\n";
index f9f8579009c71558664a7cf0c127d5a4bc028701..43bf0bd302489dc30ffac9de781f7f3bd7e53837 100755 (executable)
@@ -81,6 +81,10 @@ if ($params{init}) {
     open(my $fh, '>', $session_file) or die "Cannot open $session_file for writing: $!\n";
     print {$fh} "$params{session}\n";
     close($fh);
     open(my $fh, '>', $session_file) or die "Cannot open $session_file for writing: $!\n";
     print {$fh} "$params{session}\n";
     close($fh);
+
+    print $query->header('application/json');
+    print encode_json({ entries => [], error => undef });
+    exit;
 }
 else {
     open(my $fh, '<', $session_file) or die "Cannot open $session_file for reading: $!\nPerhaps try running --init\n";
 }
 else {
     open(my $fh, '<', $session_file) or die "Cannot open $session_file for reading: $!\nPerhaps try running --init\n";