pf-tools/pf-tools: Coding style for filter_vlan2if and correctly...

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Fri Sep 17 15:09:37 UTC 2010


details:   http://hg.debian.org/hg/pf-tools/pf-tools/rev/93fb61c9763a
changeset: 782:93fb61c9763a
user:      "Christophe Caillet <quadchris at free.fr>"
date:      Fri Sep 17 17:09:20 2010 +0200
description:
Coding style for filter_vlan2if and correctly handle error on reference detection in Init_SUBST

diffstat:

2 files changed, 2 deletions(-)
filters/filter_vlan2if |    1 -
lib/PFTools/Conf.pm    |    1 -

diffs (184 lines):

diff -r d008affda437 -r 93fb61c9763a filters/filter_vlan2if
--- a/filters/filter_vlan2if	Thu Sep 16 14:22:21 2010 +0200
+++ b/filters/filter_vlan2if	Fri Sep 17 17:09:20 2010 +0200
@@ -1,7 +1,6 @@
 #!/usr/bin/perl
 ##
-##  $Id$
-##
+##  Copyright (C) 2010 Christophe Caillet <quadchris at free.fr>
 ##  Copyright (C) 2003-2005 Damien Clermonte <damien at sitadelle.com>
 ##  Copyright (C) 2001-2003 Olivier Molteni <olivier at molteni.net>
 ##
@@ -29,6 +28,7 @@
 
 use English qw( -no_match_vars );    # Avoids regex performance penalty
 use Getopt::Long qw( :config ignore_case_always bundling );
+use IO::File;
 use Sys::Hostname;
 
 use PFTools::Logger;
@@ -37,27 +37,33 @@
 
 #################################
 # VARS
-my $HELP              = 0;
-my $HOSTNAME          = hostname;
-my $SITE              = '';
-my $GLOBAL_STORE_FILE = '';
-my $PF_CONFIG_FILE    = '';
+
+my @options_specs = (
+    'help',
+    'host|h=s',
+    'site|s=s',
+    'config|c=s',
+    'store=s',
+    'input|i=s',
+    'output|o=s',
+);
+
+my $options = {
+    'help'  => 0,
+    'host'  => hostname,
+};
+
 my $PF_CONFIG         = {};
-my $INPUT_FILE        = '';
-my $OUTPUT_FILE       = '';
 my $GLOBAL_STRUCT     = {};
 
 my $program = $0;
 $program =~ s%.*/%%;    # cheap basename
-
-my $version = sprintf( "svn-r%s", q$Revision$ =~ /([\d.]+)/ );
 
 ###################################
 # Funtions
 
 sub Do_help {
     print STDERR << "# ENDHELP";
-    $program - version $version
 
 Usage:	$program [options]
 	--help		: print help and exit
@@ -74,65 +80,63 @@
 ##################################
 ### MAIN
 
-GetOptions(
-    'help'       => \$HELP,
-    'host|h=s'   => \$HOSTNAME,
-    'site|s=s'   => \$SITE,
-    'config|c=s' => \$PF_CONFIG_FILE,
-    'store=s'    => \$GLOBAL_STORE_FILE,
-    'input|i=s'  => \$INPUT_FILE,
-    'output|o=s' => \$OUTPUT_FILE
-) or die "Didn't grok options (see --help).\n";
+GetOptions( $options, @options_specs )
+    or die "Didn't grok options (see --help).\n";
 
-if ($HELP) {
+if ($options->{'help'}) {
     Do_help();
     exit 0;
 }
 
-( $PF_CONFIG, $GLOBAL_STRUCT )
-    = Init_TOOLS( $HOSTNAME, $PF_CONFIG_FILE, $GLOBAL_STORE_FILE );
+( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS(
+    $options->{'host'},
+    $options->{'config'},
+    $options->{'store'}
+);
 
-if ( $SITE eq '' ) {
-    if ( !defined $PF_CONFIG->{'location'}->{'site'} ) {
-        my $site_list = Get_site_from_hostname( $HOSTNAME, $GLOBAL_STRUCT );
-        if ( !defined $site_list ) {
+unless( $options->{'site'} ) {
+    unless( $PF_CONFIG->{'location'}->{'site'} ) {
+        my $site_list = Get_site_from_hostname(
+            $options->{'host'},
+            $GLOBAL_STRUCT
+        );
+        unless( $site_list ) {
             Abort( $CODE->{'UNDEF_KEY'},
-                      "Unable to retrieve site for hostname "
-                    . $HOSTNAME
-                    . " : hostname not defined" );
+                "Unknown hostname $options->{'host'}" );
         }
-        elsif ( scalar @{$site_list} > 1 ) {
+        if ( scalar @{$site_list} > 1 ) {
             Abort( $CODE->{'DUPLICATE_VALUE'},
-                      "Unable to retrieve site for hostname "
-                    . $HOSTNAME
-                    . " : hostname appeared in multiple sites : "
-                    . join( ",", @{$site_list} ) . ".\n"
-                    . "Please relaunch this command with the right site" );
+                "Multiple sites for hostname $options->{'host'}" );
         }
-        else {
-            ($SITE) = @{$site_list};
-        }
+        ($options->{'site'}) = @{$site_list};
     }
-    else {
-        $SITE = $PF_CONFIG->{'location'}->{'site'};
-    }
+    $options->{'site'} = $PF_CONFIG->{'location'}->{'site'};
 }
 
-if ( $INPUT_FILE eq '' || $OUTPUT_FILE eq '' ) {
+if ( $options->{'input'} eq '' || $options->{'output'} eq '' ) {
     Abort( $CODE->{'UNDEF_KEY'},
         "Source and/or destination file is(are) not defined on CLI" );
 }
 
-my $filtered_src
-    = Search_and_replace( $HOSTNAME, $SITE, $INPUT_FILE, 'iface', $PF_CONFIG,
-    "", $GLOBAL_STRUCT );
+my $filtered_src = Search_and_replace(
+    $options->{'host'},
+    $options->{'site'},
+    $options->{'input'},
+    'iface',
+    $PF_CONFIG,
+    "",
+    $GLOBAL_STRUCT
+);
 
-unless ( open( OUTPUT, ">" . $OUTPUT_FILE ) ) {
-    Abort( $CODE->{'OPEN'},
-        "Unable to open destination file " . $OUTPUT_FILE . " : $OS_ERROR" );
-}
-
-print OUTPUT join( "", @{$filtered_src} );
-close(OUTPUT);
+my $output_fh = IO::File->new( ">" . $options->{'output'} )
+    or Abort( $CODE->{'OPEN'},
+        "Unable to open destination $options->{'output'} : $OS_ERROR" );
+$output_fh->print( join( "", @{$filtered_src} ) )
+    or Abort( $CODE->{'OPEN'},
+        "Unable to write on destination $options->{'output'} : $OS_ERROR" );
+$output_fh->close()
+    or Abort( $CODE->{'OPEN'},
+        "Unable to close destination $options->{'output'} : $OS_ERROR" );
 
 exit 0;
+
diff -r d008affda437 -r 93fb61c9763a lib/PFTools/Conf.pm
--- a/lib/PFTools/Conf.pm	Thu Sep 16 14:22:21 2010 +0200
+++ b/lib/PFTools/Conf.pm	Fri Sep 17 17:09:20 2010 +0200
@@ -204,8 +204,8 @@
 
     $domainname ||= $pf_config->{'location'}->{'zone'} || hostdomain || q{};
 
-    if ( $hosttype and ref $hosttype ne 'SCALAR' ) {
-        croak q{ERROR: Hosttype parameter must be a string};
+    unless( ! ref $hosttype ) {
+        croak qq{ERROR: Hosttype parameter must be a string};
     }
 
     my $host_regex = $pf_config->{'regex'}->{'hostname'}



More information about the pf-tools-commits mailing list