pf-tools commit: r895 [parmelan-guest] - in /branches/next-gen: README.coding.style filters/filter_privateresolve

parmelan-guest at users.alioth.debian.org parmelan-guest at users.alioth.debian.org
Tue Sep 7 21:50:27 UTC 2010


Author: parmelan-guest
Date: Tue Sep  7 21:50:24 2010
New Revision: 895

URL: http://svn.debian.org/wsvn/pf-tools/?sc=1&rev=895
Log:
filter_privateresolve: more style

Modified:
    branches/next-gen/README.coding.style
    branches/next-gen/filters/filter_privateresolve   (contents, props changed)

Modified: branches/next-gen/README.coding.style
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/README.coding.style?rev=895&op=diff
==============================================================================
--- branches/next-gen/README.coding.style (original)
+++ branches/next-gen/README.coding.style Tue Sep  7 21:50:24 2010
@@ -19,6 +19,12 @@
 
 * Always use IO::File objects instead of a bare open().
 
+* Functions with too many parameters should be converted to named parameters.
+
+* No function prototypes.
+
+* Return early on known error cases (no "else" needed).
+
 
 The following line is only here to tell Vim my preferences when editing this
 file :

Modified: branches/next-gen/filters/filter_privateresolve
URL: http://svn.debian.org/wsvn/pf-tools/branches/next-gen/filters/filter_privateresolve?rev=895&op=diff
==============================================================================
--- branches/next-gen/filters/filter_privateresolve (original)
+++ branches/next-gen/filters/filter_privateresolve Tue Sep  7 21:50:24 2010
@@ -33,44 +33,51 @@
 use PFTools::Structqueries;
 use PFTools::Utils;
 
-#################################
-# VARS
-my $HELP              = 0;
-my $HOSTNAME          = hostname;
-my $SITE              = '';
-my $GLOBAL_STORE_FILE = '';
-my $PF_CONFIG_FILE    = '';
-my $PF_CONFIG         = {};
-my $TYPE_RESOLVE      = 'cnf';
-my $SEPARATOR         = " ";
-my $ZONE              = "";
-my $INPUT_FILE        = '';
-my $OUTPUT_FILE       = '';
-my $GLOBAL_STRUCT     = {};
+#<<< please, perltidy, don't mess with this (keep each entry on its own line)
+my @options_specifications = (
+    'config-file|config|c=s',
+    'help',
+    'hostname|host|h=s',
+    'input-file|input|i=s',
+    'output-file|output|o=s',
+    'separator|sep=s',
+    'site|s=s',
+    'store-file|store=s',
+    'type-resolve|type|t=s',
+    'zone|z=s',
+);
+#>>>
+# default values
+my $options = {
+    'help'         => 0,
+    'hostname'     => hostname,
+    'separator'    => ' ',
+    'type-resolve' => 'cnf',
+};
 
 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]
 
-Usage:	$program [options]
-	--help		: print help and exit
-	-h --host	: hostname for which we want to filter input
-	-s --site	: site on which hostname is defined (optional)
-	-c --config	: file where pf-tools configuration is stored e.g. /etc/pf-tools.conf (optional)
-	--store		: file where global structure datas are in storable format (optional)
-	-z --zone	: zone on which we want to filter input
-	-t --type	: type for resolution. Allowed values are cnf (from global configuration structure) and dns
-	--sep		: separator between resolved IPs
-	-i --input	: input file
-	-o --output	: output file
+    --help                 : print help and exit
+ -h --hostname             : hostname for which we want to filter input
+ -s --site                 : site on which hostname is defined (optional)
+ -c --config-file          : file where pf-tools configuration is stored
+                             e.g. /etc/pf-tools.conf (optional)
+    --store-file           : file where global structure datas are in
+                             storable format (optional)
+ -z --zone                 : zone on which we want to filter input
+ -t --type-resolve         : type for resolution. Allowed values are cnf
+                             (from global configuration structure) and dns
+    --separator            : separator between resolved IPs [default: space]
+ -i --input                : input file
+ -o --output               : output file
     
 # ENDHELP
 }
@@ -78,75 +85,84 @@
 ##################################
 ### MAIN
 
-GetOptions(
-    'help'       => \$HELP,
-    'host|h=s'   => \$HOSTNAME,
-    'site|s=s'   => \$SITE,
-    'config|c=s' => \$PF_CONFIG_FILE,
-    'store=s'    => \$GLOBAL_STORE_FILE,
-    'type|t=s'   => \$TYPE_RESOLVE,
-    'sep=s'      => \$SEPARATOR,
-    'zone|z=s'   => \$ZONE,
-    'input|i=s'  => \$INPUT_FILE,
-    'output|o=s' => \$OUTPUT_FILE
-) or die "Didn't grok options (see --help).\n";
+GetOptions( $options, @options_specifications )
+    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 );
-
-if ( $SITE eq '' ) {
-    if ( !defined $PF_CONFIG->{'location'}->{'site'} ) {
-        my $site_list = Get_site_from_hostname( $HOSTNAME, $GLOBAL_STRUCT );
-        if ( !defined $site_list ) {
-            Abort( $CODE->{'UNDEF_KEY'},
-                      "Unable to retrieve site for hostname "
-                    . $HOSTNAME
-                    . " : hostname not defined" );
-        }
-        elsif ( 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" );
-        }
-        else {
-            ($SITE) = @{$site_list};
-        }
-    }
-    else {
-        $SITE = $PF_CONFIG->{'location'}->{'site'};
-    }
-}
-
-$ZONE = Get_zone_from_site_GLOBAL( $SITE, $GLOBAL_STRUCT )
-    if $ZONE eq '';
-
-if ( $INPUT_FILE eq '' || $OUTPUT_FILE eq '' ) {
+unless ( $options->{'input-file'} and $options->{'output-file'} ) {
     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, 'resolver',
-    $PF_CONFIG, $SEPARATOR, $GLOBAL_STRUCT, $TYPE_RESOLVE );
+my ( $PF_CONFIG, $GLOBAL_STRUCT ) = Init_TOOLS(
+    $options->{'hostname'},
+    $options->{'config-file'},
+    $options->{'store-file'}
+);
 
-my $output_fh = IO::File->new("> $OUTPUT_FILE")
+# FIXME: make a (private) function to handle this?
+unless ( $options->{'site'} ) {
+    my $site = $PF_CONFIG->{'location'}->{'site'};
+    unless ($site) {
+        my $site_list = Get_site_from_hostname( $options->{'hostname'},
+            $GLOBAL_STRUCT );
+        unless ($site_list) {
+            Abort( $CODE->{'UNDEF_KEY'},
+                "Unable to retrieve site for hostname $options->{'hostname'}: hostname not defined"
+            );
+        }
+        if ( scalar @{$site_list} > 1 ) {
+            Abort( $CODE->{'DUPLICATE_VALUE'},
+                "Unable to retrieve site for hostname $options->{'hostname'}: hostname appeared in multiple sites: "
+                    . join( ',', @{$site_list} )
+                    . ".\nPlease relaunch this command with the right site" );
+        }
+        ($site) = @{$site_list};
+    }
+
+    $options->{'site'} = $site;
+}
+
+$options->{'zone'}
+    = Get_zone_from_site_GLOBAL( $options->{'site'}, $GLOBAL_STRUCT )
+    unless $options->{'zone'};
+
+my $filtered_src = Search_and_replace(
+    $options->{'hostname'},   $options->{'site'},
+    $options->{'input-file'}, 'resolver',
+    $PF_CONFIG,               $options->{'separator'},
+    $GLOBAL_STRUCT,           $options->{'type-resolve'}
+);
+
+# FIXME: functions with too many parameters should be converted
+# to named parameters, like this :
+#my $args_ref = {
+#    hostname       => $options->{'hostname'},
+#    site           => $options->{'site'},
+#    'input-file'   => $options->{'input-file'},
+#    resolver       => 'resolver',
+#    pf_config      => $PF_CONFIG,
+#    separator      => $options->{'separator'},
+#    global_struct  => $GLOBAL_STRUCT,
+#    'resolve-type' => $options->{'type-resolve'},
+#};
+#my $filtered_src = search_and_replace($args_ref);
+
+my $output_fh = IO::File->new("> $options->{'output-file'}")
     or Abort( $CODE->{'OPEN'},
-        "Unable to open destination file $OUTPUT_FILE: $OS_ERROR" );
+    "Unable to open destination file $options->{'output-file'}: $OS_ERROR" );
 
 $output_fh->print( join '', @{$filtered_src} )
     or Abort( $CODE->{'OPEN'},
-        "Unable to write to destination file $OUTPUT_FILE: $OS_ERROR" );
+    "Unable to write to destination file $options->{'output-file'}: $OS_ERROR"
+    );
 
 $output_fh->close()
     or Abort( $CODE->{'OPEN'},
-        "Unable to close destination file $OUTPUT_FILE: $OS_ERROR" );
+    "Unable to close destination file $options->{'output-file'}: $OS_ERROR" );
 
 exit 0;




More information about the pf-tools-commits mailing list