diff --git a/xCAT-probe/lib/perl/probe_format.pm b/xCAT-probe/lib/perl/probe_format.pm new file mode 100644 index 0000000000..585bd52991 --- /dev/null +++ b/xCAT-probe/lib/perl/probe_format.pm @@ -0,0 +1,120 @@ +# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html +package probe_format; + +use strict; +use warnings; + +my $DESIRED_WIDTH = 120; +my $DEFAULT_WIDTH = 80; +my $FLAG_WIDTH = 6; + +#----------------------------------- + +=head3 screen_width + + Descriptions: + Return the number of columns to format a message in. + Arguments: + raw: the output of 'tput cols', or undef when there is no terminal + Returns: + the column count + +=cut + +#----------------------------------- +sub screen_width { + my ($raw) = @_; + + return $DEFAULT_WIDTH unless (defined($raw)); + return $raw =~ /(\d+)/ ? $1 + 0 : 0; +} + +#----------------------------------- + +=head3 render + + Descriptions: + Lay out one message line of a probe subcommand. + Arguments: + line: the line the subcommand printed, "[]:" + fullmsg: true to wrap a long message, false to truncate it + screenwidth: the column count from screen_width() + Returns: + (lines, mark). lines is an array reference. Print every line but the + last with a newline. mark is what follows the last line: undef when + the line carries no flag, and otherwise the mark to print, which + already holds its newline. + +=cut + +#----------------------------------- +sub render { + my ($line, $fullmsg, $screenwidth) = @_; + + my $msg = $line; + my $flag; + if ($line =~ /\[(\w+)\]\s*:(.+)/) { + $flag = $1; + $msg = $2; + } + + $msg =~ s/\t/ /g; + my $maxlen = ($screenwidth > $DESIRED_WIDTH ? $DESIRED_WIDTH : $screenwidth); + + my @finalmsg = (); + my $msglen = length($msg); + if ($msglen <= $maxlen) { + if (!$fullmsg && $flag) { + my $leftspace = $maxlen - length($msg); + if ($leftspace < $FLAG_WIDTH) { + $msg = substr($msg, 0, $maxlen - $FLAG_WIDTH); + $msg =~ s/(.*).../$1\.\.\./g; + } + } + push @finalmsg, $msg; + } else { + my $head = 0; + my $tail = $maxlen; + while ($head < $msglen) { + push @finalmsg, substr($msg, $head, ($tail - $head)); + if (!$fullmsg) { + if ($flag) { + $finalmsg[0] =~ s/(.*).........$/$1\.\.\./g; + } else { + $finalmsg[0] =~ s/(.*)...$/$1\.\.\./g; + } + last; + } + $head = $tail; + $tail = $head + ($maxlen <= ($msglen - $tail) ? $maxlen : ($msglen - $tail)); + } + } + + return (\@finalmsg, undef) unless ($flag); + + my $last = $finalmsg[-1]; + my $leftspace = $maxlen - length($last); + my $debug = ($flag =~ /debug/i) ? 1 : 0; + my $mark = ''; + if ($flag =~ /failed/i) { + $mark = "[FAIL]\n"; + } elsif ($flag =~ /warning/i) { + $mark = "[WARN]\n"; + } elsif ($flag =~ /ok/i) { + $mark = "[ OK ]\n"; + } elsif ($debug) { + $mark = "\n"; + } elsif ($flag =~ /info/i) { + $mark = "[INFO]\n"; + } + + my $spacenum = 0; + if (!$debug) { + $spacenum = (($leftspace >= $FLAG_WIDTH) ? ($leftspace - $FLAG_WIDTH) : ($screenwidth - length($last) + $maxlen - $FLAG_WIDTH)); + } + $finalmsg[-1] = $last . (" " x $spacenum); + + return (\@finalmsg, $mark); +} + +1; diff --git a/xCAT-probe/xcatprobe b/xCAT-probe/xcatprobe index 5a732c7831..6b1371c9ee 100755 --- a/xCAT-probe/xcatprobe +++ b/xCAT-probe/xcatprobe @@ -1,6 +1,10 @@ #!/usr/bin/env perl # IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html +BEGIN { $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat'; } + +use lib "$::XCATROOT/probe/lib/perl"; +use probe_format; use File::Basename; use Data::Dumper; use File::Path; @@ -8,8 +12,6 @@ use POSIX qw(WNOHANG setsid :errno_h); use Term::ANSIColor qw(:constants); $Term::ANSIColor::AUTORESET = 1; -$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat'; - my $program_name = basename($0); my $pro_dir = "$::XCATROOT/probe/"; @@ -74,6 +76,20 @@ sub loadsubcmds { } +#----------------------------------- + +=head3 + Description: + Ask the terminal how wide it is. 'tput cols' writes nothing when TERM + names a terminal terminfo does not carry, so probe_format decides what an + unusable answer means. +=cut + +#----------------------------------- +sub terminal_width { + return probe_format::screen_width($ENV{'TERM'} ? `tput cols 2>/dev/null` : undef); +} + #----------------------------------- =head3 @@ -87,96 +103,29 @@ sub format_cmd_output { my $color = shift; my $fullmsg = shift; - my $msg; - my $flag; + my ($lines, $mark) = probe_format::render($line, $fullmsg, terminal_width()); - if ($line =~ /\[(\w+)\]\s*:(.+)/) { - $flag = $1; - $msg = $2; + my $last = pop @$lines; + print "$_\n" foreach (@$lines); + + unless (defined($mark)) { + print "$last\n"; + return 0; + } + + print $last; + return 0 unless (length($mark)); + + if (!$color) { + print $mark; + } elsif ($mark =~ /FAIL/) { + print BOLD RED $mark; + } elsif ($mark =~ /WARN/) { + print BOLD BLUE $mark; + } elsif ($mark =~ /OK/) { + print BOLD GREEN $mark; } else { - $msg = $line; - } - - $msg =~ s/\t/ /g; - my $flaglen = 6; - my $desiredwidth = 120; - my $screenwidth = 80; - if ($ENV{'TERM'}) { - $screenwidth = (`tput cols` + 0); - } - my $maxlen = ($screenwidth > $desiredwidth ? $desiredwidth : $screenwidth); - - my @finalmsg = (); - my $msglen = length($msg); - if ($msglen <= $maxlen) { - if (!$fullmsg && $flag) { - my $leftspace = $maxlen - length($msg); - if ($leftspace < $flaglen) { - $msg = substr($msg, 0, $maxlen - $flaglen); - $msg =~ s/(.*).../$1\.\.\./g; - } - } - push @finalmsg, $msg; - } else { - my @tmpmsg = split("", $msg); - my $head = 0; - my $tail = $maxlen; - while ($head < $msglen) { - push @finalmsg, substr($msg, $head, ($tail - $head)); - if (!$fullmsg) { - if ($flag) { - $finalmsg[0] =~ s/(.*).........$/$1\.\.\./g; - } else { - $finalmsg[0] =~ s/(.*)...$/$1\.\.\./g; - } - last; - } - $head = $tail; - $tail = $head + ($maxlen <= ($msglen - $tail) ? $maxlen : ($msglen - $tail)); - } - } - - for (my $i = 0 ; $i < $#finalmsg + 1 ; ++$i) { - if ($i ne $#finalmsg) { - print "$finalmsg[$i]\n"; - next; - } - - if ($flag) { - my $leftspace = $maxlen - length($finalmsg[$i]); - my $spacenum = 0; - if($flag !~ /debug/i) { - $spacenum = (($leftspace >= $flaglen) ? ($leftspace - $flaglen) : ($screenwidth - length($finalmsg[$i]) + $maxlen - $flaglen)); - } - my $spacestr = " " x $spacenum; - print "$finalmsg[$i]$spacestr"; - - if ($flag =~ /failed/i) { - if ($color) { - print BOLD RED "[FAIL]\n"; - } else { - print "[FAIL]\n"; - } - } elsif ($flag =~ /warning/i) { - if ($color) { - print BOLD BLUE "[WARN]\n"; - } else { - print "[WARN]\n"; - } - } elsif ($flag =~ /ok/i) { - if ($color) { - print BOLD GREEN "[ OK ]\n"; - } else { - print "[ OK ]\n"; - } - } elsif ($flag =~ /debug/i) { - print "\n"; - } elsif ($flag =~ /info/i) { - print "[INFO]\n"; - } - } else { - print "$finalmsg[$i]\n"; - } + print $mark; } return 0; } @@ -197,10 +146,7 @@ sub listvalidsubcmd { $maxlen += 4; my $desiredwidth = 120; - my $screenwidth = 80; - if ($ENV{'TERM'}) { - $screenwidth = (`tput cols` + 0); - } + my $screenwidth = terminal_width(); my $finallen = ($screenwidth > $desiredwidth ? $desiredwidth : $screenwidth); print "Supported sub commands are:\n"; diff --git a/xCAT-test/unit/probe_xcatmn_selinux_case.t b/xCAT-test/unit/probe_xcatmn_selinux_case.t index 768e29e5bb..921897b70e 100644 --- a/xCAT-test/unit/probe_xcatmn_selinux_case.t +++ b/xCAT-test/unit/probe_xcatmn_selinux_case.t @@ -13,6 +13,7 @@ use lib "$FindBin::Bin/../../xCAT-probe/lib/perl"; use Test::More; require probe_utils; +require probe_format; like($INC{'probe_utils.pm'}, qr/\Q$FindBin::Bin\E/, 'probe_utils comes from this checkout'); # The case ran the probe through grep. ausearch is not involved here: grep kept @@ -60,10 +61,18 @@ isnt($mark{o}, $mark{f}, "send_msg marks an ok checkpoint '$mark{o}' apart from a failed one '$mark{f}'"); like($mark{o}, qr/\QSELinux\E/, 'the ok mark carries the checkpoint text'); -my %rendered = ( - ok => $checkpoint . (' ' x 39) . '[ OK ]', - failed => $checkpoint . (' ' x 39) . '[FAIL]', -); +# Lay the marks out the way xcatprobe does, at the width the GitHub Actions +# container gives: it sets TERM to a name terminfo does not carry, so +# 'tput cols' writes nothing. The case reads what this renders. +my $width = probe_format::screen_width(''); +my %rendered; +foreach my $tag (qw(o f)) { + my ($lines, $flag) = probe_format::render($mark{$tag}, 0, $width); + chomp($flag); + $rendered{ $tag eq 'o' ? 'ok' : 'failed' } = join("\n", @$lines) . $flag; +} +like($rendered{ok}, qr/\Q$checkpoint\E/, + "the rendered checkpoint keeps its text without a terminal: '$rendered{ok}'"); foreach my $pattern (@positive) { like($rendered{ok}, qr/$pattern/, "a passing SELinux checkpoint matches '$pattern'"); unlike($rendered{failed}, qr/$pattern/,