bin/dnsvi
changeset 72 e0c8ae0169e4
parent 71 ce0fa0e7c898
child 78 606f6bae6f43
equal deleted inserted replaced
71:ce0fa0e7c898 72:e0c8ae0169e4
     1 #! /usr/bin/perl
     1 #! /usr/bin/perl
     2 #line 3
     2 #line 3
     3 # Copyright: (C) 2014-2014 Heiko Schlittermann <hs@schlittermann>
     3 # Copyright: (C) 2014-2015 Heiko Schlittermann <hs@schlittermann>
     4 # This program is released unter the Terms of the GPL.
     4 # This program is released unter the Terms of the GPL.
     5 use 5.010;
     5 use 5.10.1;
     6 use strict;
     6 use strict;
     7 use warnings;
     7 use warnings;
     8 use if $ENV{DEBUG} // '' eq 'dnsvi' => 'Smart::Comments';
     8 use if $ENV{DEBUG} => 'Smart::Comments';
     9 use Getopt::Long;
     9 use Getopt::Long;
    10 use Pod::Usage;
    10 use Pod::Usage;
    11 use File::Copy;
    11 use File::Copy;
    12 
    12 
    13 #use blib;
    13 use experimental 'smartmatch';
    14 use DNS::Vi;
    14 use DNS::Vi;
    15 
    15 
    16 sub slurp {
    16 sub slurp {
    17     local $/    = undef;
    17     local $/    = undef;
    18     local @ARGV = @_;
    18     local @ARGV = @_;
    53         $zone
    53         $zone
    54     );
    54     );
    55 
    55 
    56     my @zone1 = parse($_ = `@dig`, { -skip => $o{skip} })
    56     my @zone1 = parse($_ = `@dig`, { -skip => $o{skip} })
    57       or die "Empty zone\n";
    57       or die "Empty zone\n";
    58     my $fh2;
    58 
    59     my @zone2 = do {
    59   UNDO:
       
    60     my @zone2 = @zone1;
       
    61     my $backup;    # it's a tmp file containing the original
       
    62 
       
    63   EDIT:
       
    64     @zone2 = do {
    60         if (my $file = shift @ARGV) {
    65         if (my $file = shift @ARGV) {
    61             parse(slurp($file), { -skip => $o{skip} });
    66             parse(slurp($file), { -skip => $o{skip} });
    62         }
    67         }
    63         else {
    68         else {
    64             edit(@zone1,
    69             edit(
    65                 { -skip => $o{skip}, -editor => $o{editor}, -backup => \$fh2 });
    70                 @zone2,
       
    71                 {
       
    72                     -skip   => $o{skip},
       
    73                     -editor => $o{editor},
       
    74                     -backup => \$backup
       
    75                 }
       
    76             );
    66         }
    77         }
    67     };
    78     };
    68     ### @zone2
    79     ### @zone2
    69 
    80 
    70     my %delta = delta(\@zone1, \@zone2);
    81     my %delta = delta(\@zone1, \@zone2);
    71     if (!%delta) {
    82     if (!%delta) {
    72         say 'nothing changed';
    83         say 'nothing changed';
    73         return 0;
    84         return 0;
    74     }
    85     }
    75 
    86 
    76     say 'The following changes need your confirmation.';
    87   VIEW:
    77     say join "\n", show(@delta{qw/add del/});
    88     if ($_ eq 'v' or (map { @{$_} } values %delta) < 10) {
    78     print 'confirm (yes|NO): ';
    89         say 'The following changes need your confirmation.';
    79     return 1 if <STDIN> !~ /^y/i;
    90         say join "\n", show(@delta{qw/add del/});
    80 
    91     }
    81     update(
    92     else {
       
    93         say 'added: ', 0 + @{ $delta{add} }, ', removed: ',
       
    94           0 + @{ $delta{del} };
       
    95     }
       
    96   CONFIRM:
       
    97     print 'action [yqQvVeu?] ?';
       
    98     $_ = get_key;
       
    99 
       
   100     given ($_) {
       
   101         when ('y') { }
       
   102 	when ('q') { }
       
   103 	when ('V') { }
       
   104         when ('Q') { return 1 }
       
   105         when ('e') { goto EDIT }
       
   106         when ('v') { goto VIEW }
       
   107         when ('u') { goto UNDO }
       
   108         when ('?') {
       
   109             print <<_;
       
   110   y -- yes: submit changes and exit
       
   111   q -- quit: save changes as ",dnsvi-$$"
       
   112   Q -- quit: discard changes and exit
       
   113   v -- view changes
       
   114   V -- view changes as nsupdate commands
       
   115   e -- edit again
       
   116   u -- undo and edit again
       
   117   ? -- what?
       
   118 _
       
   119             goto CONFIRM;
       
   120         }
       
   121         default { goto CONFIRM }
       
   122     }
       
   123 
       
   124     /^[yV]$/ and update(
    82         \@zone1,
   125         \@zone1,
    83         @delta{qw/add del/},
   126         @delta{qw/add del/},
    84         {
   127         {
       
   128 	    $_ eq 'V' ? (-dry => 1) : (),
    85             -server => $o{server},
   129             -server => $o{server},
    86             -local  => $o{local},
   130             -local  => $o{local},
    87             -debug  => $o{debug},
   131             -debug  => $o{debug},
    88             -key    => $o{key}
   132             -key    => $o{key}
    89         }
   133         }
    90       )
   134       )
    91       or do {
   135       or do {
    92         copy($fh2->filename, ",dnsvi-$$")
   136 	  copy($backup->filename, ",dnsvi-$$")
    93           and say "Saved as ',dnsvi-$$'"
   137           and say "Saved as ',dnsvi-$$'";
    94           if $fh2;
       
    95       };
   138       };
       
   139 
       
   140     goto CONFIRM if $_ eq 'V';
    96 
   141 
    97     return 0;
   142     return 0;
    98 }
   143 }
    99 
   144 
   100 exit main(@ARGV) if not caller;
   145 exit main(@ARGV) if not caller;