From 3a0b2c40d2dcb7f59b79ee56c3f9f16b52a48fb0 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Tue, 11 May 2021 14:28:46 +0200 Subject: [PATCH] report: avoid adding to report by side-effects, be more explicit And move the helper methods up and scope them to module-local only Uses the fact that perl methods return the last statement, so the dir2text sub closures in the command list do not need to be changed Signed-off-by: Thomas Lamprecht --- PVE/Report.pm | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/PVE/Report.pm b/PVE/Report.pm index 6037ba9d..7c16d5e8 100644 --- a/PVE/Report.pm +++ b/PVE/Report.pm @@ -5,6 +5,21 @@ use warnings; use PVE::Tools; +# output the content of all the files of a directory +my sub dir2text { + my ($target_dir, $regexp) = @_; + + my $text = ''; + PVE::Tools::dir_glob_foreach($target_dir, $regexp, sub { + my ($file) = @_; + $text .= "\n# cat $target_dir$file\n"; + $text .= PVE::Tools::file_get_contents($target_dir.$file)."\n"; + }); + return $text; +} + +# command -v is the posix equivalent of 'which' +my sub cmd_exists { system("command -v '$_[0]' > /dev/null 2>&1") == 0 } my $init_report_cmds = sub { # NOTE: always add new sections to the report_order array! @@ -106,29 +121,13 @@ my $init_report_cmds = sub { return $report_def; }; -my $report; -# output the content of all the files of a directory -sub dir2text { - my ($target_dir, $regexp) = @_; - - PVE::Tools::dir_glob_foreach($target_dir, $regexp, sub { - my ($file) = @_; - $report .= "\n# cat $target_dir$file\n"; - $report .= PVE::Tools::file_get_contents($target_dir.$file)."\n"; - }); -} - -# command -v is the posix equivalent of 'which' -sub cmd_exists { system("command -v '$_[0]' > /dev/null 2>&1") == 0 } - sub generate { - my $report_def = $init_report_cmds->(); my @report_order = ('general', 'storage', 'virtual guests', 'network', 'firewall', 'cluster', 'bios', 'pci', 'disks', 'volumes'); - $report = ''; + my $report = ''; my $record_output = sub { $report .= shift . "\n"; }; @@ -160,7 +159,7 @@ sub generate { foreach my $command (@$commands) { eval { if (ref $command eq 'CODE') { - PVE::Tools::run_with_timeout($cmd_timeout, $command); + $report .= PVE::Tools::run_with_timeout($cmd_timeout, $command); } else { print STDERR "Process ".$command."..."; $report .= "\n# $command\n";