diff mbox series

[1/1] asn: fix missing quiet checks in xt_asn_build

Message ID 20240902194324.2948111-1-philipp@redfish-solutions.com
State Handled Elsewhere, archived
Headers show
Series [1/1] asn: fix missing quiet checks in xt_asn_build | expand

Commit Message

Philip Prindeville Sept. 2, 2024, 7:43 p.m. UTC
From: Philip Prindeville <philipp@redfish-solutions.com>

Conceivably someone might want to run a refresh of the ASN database
from within a script, particularly an unattended script such as a cron
job. Do not generate output in that case.

Also, do not send normal (progress) output to STDERR.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
---
 asn/xt_asn_build | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/asn/xt_asn_build b/asn/xt_asn_build
index 4c406799480538c03efb8a5ccb9479aef3ef3060..e5edfbf169eaeb2e0049c38db4934070ea3d5b38 100755
--- a/asn/xt_asn_build
+++ b/asn/xt_asn_build
@@ -12,12 +12,14 @@  use Socket qw(AF_INET AF_INET6 inet_pton);
 use warnings;
 use Text::CSV_XS; # or trade for Text::CSV
 use strict;
+$| = 1;
 
 my $csv = Text::CSV_XS->new({
 	allow_whitespace => 1,
 	binary => 1,
 	eol => $/,
 }); # or Text::CSV
+my $quiet = 0;
 my $source_dir = ".";
 my $target_dir = ".";
 my $output_txt;
@@ -27,6 +29,7 @@  my $output_txt;
 	"D=s" => \$target_dir,
 	"S=s" => \$source_dir,
 	"O=s" => \$output_txt,
+	"q" => \$quiet,
 );
 
 if (!-d $source_dir) {
@@ -85,8 +88,8 @@  sub collect
 
 		$asns{$asn}->{pool_v4}->add($cidr);
 
-		if ($. % 4096 == 0) {
-			print STDERR "\r\e[2K$. entries";
+		if (!$quiet && $. % 4096 == 0) {
+			print STDOUT "\r\e[2K$. entries";
 		}
 
 		if ($outfile) {
@@ -95,7 +98,7 @@  sub collect
 		}
 	}
 
-	print STDERR "\r\e[2K$. entries total\n";
+	print STDOUT "\r\e[2K$. entries total\n" unless ($quiet);
 
 	close($fh);
 
@@ -132,8 +135,8 @@  sub collect
 
 		$asns{$asn}->{pool_v6}->add($cidr);
 
-		if ($. % 4096 == 0) {
-			print STDERR "\r\e[2K$. entries";
+		if (!$quiet && $. % 4096 == 0) {
+			print STDOUT "\r\e[2K$. entries";
 		}
 
 		if ($outfile) {
@@ -142,7 +145,7 @@  sub collect
 		}
 	}
 
-	print STDERR "\r\e[2K$. entries total\n";
+	print STDOUT "\r\e[2K$. entries total\n" unless ($quiet);
 
 	close($fh);
 
@@ -187,7 +190,7 @@  sub writeASN
 	printf "%5u IPv%s ranges for %s\n",
 		scalar(@ranges),
 		($family == AF_INET ? '4' : '6'),
-		$asn_number;
+		$asn_number unless ($quiet);
 
 	my $file = "$target_dir/".$asn_number.".iv".($family == AF_INET ? '4' : '6');
 	if (!open($fh, '>', $file)) {