1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::Console::BaseCommand;
use strict;
use warnings;
use Getopt::Long();
use Term::ANSIColor();
use IO::Interactive();
use Encode::Locale();
use Kernel::System::VariableCheck qw(:all);
our @ObjectDependencies = (
'Kernel::Config',
'Kernel::System::Cache',
'Kernel::System::Encode',
'Kernel::System::Main',
);
our $SuppressANSI = 0;
=head1 NAME
Kernel::System::Console::BaseCommand - command base class
=head1 DESCRIPTION
Base class for console commands.
=head1 PUBLIC INTERFACE
=head2 new()
constructor for new objects. You should not need to reimplement this in your command,
override L</Configure()> instead if you need to.
=cut
sub new {
my ( $Type, %Param ) = @_;
my $Self = {};
bless( $Self, $Type );
# for usage help
$Self->{Name} = $Type;
$Self->{Name} =~ s{Kernel::System::Console::Command::}{}smx;
$Self->{ANSI} = 1;
# Check if we are in an interactive terminal, disable colors otherwise.
if ( !IO::Interactive::is_interactive() ) {
$Self->{ANSI} = 0;
}
# Force creation of the EncodeObject as it initialzes STDOUT/STDERR for unicode output.
$Kernel::OM->Get('Kernel::System::Encode');
# Call object specific configure method. We use an eval to trap any exceptions
# that might occur here. Only if everything was ok we set the _ConfigureSuccessful
# flag.
eval {
$Self->Configure();
$Self->{_ConfigureSuccessful} = 1;
};
$Self->{_GlobalOptions} = [
{
Name => 'help',
Description => 'Display help for this command.',
},
{
Name => 'no-ansi',
Description => 'Do not perform ANSI terminal output coloring.',
},
{
Name => 'quiet',
Description => 'Suppress informative output, only retain error messages.',
},
{
Name => 'allow-root',
Description =>
'Allow root user to execute the command. This might damage your system; use at your own risk.',
Invisible => 1, # hide from usage screen
},
];
return $Self;
}
=head2 Configure()
initializes this object. Override this method in your commands.
This method might throw exceptions.
=cut
sub Configure {
return;
}
=head2 Name()
get the Name of the current Command, e. g. 'Admin::User::SetPassword'.
=cut
sub Name {
my ($Self) = @_;
return $Self->{Name};
}
=head2 Description()
get/set description for the current command. Call this in your Configure() method.
=cut
sub Description {
my ( $Self, $Description ) = @_;
$Self->{Description} = $Description if defined $Description;
return $Self->{Description};
}
=head2 AdditionalHelp()
get/set additional help text for the current command. Call this in your Configure() method.
You can use color tags (see L</Print()>) in your help tags.
=cut
sub AdditionalHelp {
my ( $Self, $AdditionalHelp ) = @_;
$Self->{AdditionalHelp} = $AdditionalHelp if defined $AdditionalHelp;
return $Self->{AdditionalHelp};
}
=head2 AddArgument()
adds an argument that can/must be specified on the command line.
This function must be called during Configure() by the command to
indicate which arguments it can process.
$CommandObject->AddArgument(
Name => 'filename',
Description => 'name of the file to be loaded',
Required => 1,
ValueRegex => qr{a-zA-Z0-9]\.txt},
);
Please note that required arguments have to be specified before any optional ones.
The information about known arguments and options (see below) will be used to generate
usage help and also to automatically verify the data provided by the user on the command line.
=cut
sub AddArgument {
my ( $Self, %Param ) = @_;
for my $Key (qw(Name Description ValueRegex)) {
if ( !$Param{$Key} ) {
$Self->PrintError("Need $Key.");
die;
}
}
for my $Key (qw(Required)) {
if ( !defined $Param{$Key} ) {
$Self->PrintError("Need $Key.");
die;
}
}
if ( $Self->{_ArgumentSeen}->{ $Param{Name} }++ ) {
$Self->PrintError("Cannot register argument '$Param{Name}' twice.");
die;
}
if ( $Self->{_OptionSeen}->{ $Param{Name} } ) {
$Self->PrintError("Cannot add argument '$Param{Name}', because it is already registered as an option.");
die;
}
$Self->{_Arguments} //= [];
push @{ $Self->{_Arguments} }, \%Param;
return;
}
=head2 GetArgument()
fetch an argument value as provided by the user on the command line.
my $Filename = $CommandObject->GetArgument('filename');
=cut
sub GetArgument {
my ( $Self, $Argument ) = @_;
if ( !$Self->{_ArgumentSeen}->{$Argument} ) {
$Self->PrintError("Argument '$Argument' was not configured and cannot be accessed.");
return;
}
return $Self->{_ParsedARGV}->{Arguments}->{$Argument};
}
=head2 AddOption()
adds an option that can/must be specified on the command line.
This function must be called during L</Configure()> by the command to
indicate which arguments it can process.
$CommandObject->AddOption(
Name => 'iterations',
Description => 'number of script iterations to perform',
Required => 1,
HasValue => 0,
ValueRegex => qr{\d+},
Multiple => 0, # optional, allow more than one occurrence (only possible if HasValue is true)
);
B<Option Naming Conventions>
If there is a source and a target involved in the command, the related options should start
with C<--source> and C<--target>, for example C<--source-path>.
For specifying filesystem locations, C<--*-path> should be used for directory/filename
combinations (e.g. C<mydirectory/myfile.pl>), C<--*-filename> for filenames without directories,
and C<--*-directory> for directories.
Example: C<--target-path /tmp/test.out --source-filename test.txt --source-directory /tmp>
=cut
sub AddOption {
my ( $Self, %Param ) = @_;
for my $Key (qw(Name Description)) {
if ( !$Param{$Key} ) {
$Self->PrintError("Need $Key.");
die;
}
}
for my $Key (qw(Required HasValue)) {
if ( !defined $Param{$Key} ) {
$Self->PrintError("Need $Key.");
die;
}
}
if ( $Param{HasValue} ) {
if ( !$Param{ValueRegex} ) {
$Self->PrintError("Need ValueRegex.");
die;
}
}
if ( $Param{Multiple} && !$Param{HasValue} ) {
$Self->PrintError("Multiple can only be specified if HasValue is true.");
die;
}
if ( $Self->{_OptionSeen}->{ $Param{Name} }++ ) {
$Self->PrintError("Cannot register option '$Param{Name}' twice.");
die;
}
if ( $Self->{_ArgumentSeen}->{ $Param{Name} } ) {
$Self->PrintError("Cannot add option '$Param{Name}', because it is already registered as an argument.");
die;
}
$Self->{_Options} //= [];
push @{ $Self->{_Options} }, \%Param;
return;
}
=head2 GetOption()
fetch an option as provided by the user on the command line.
my $Iterations = $CommandObject->GetOption('iterations');
If the option was specified with HasValue => 1, the user provided value will be
returned. Otherwise 1 will be returned if the option was present.
In case of an option with C<Multiple => 1>, an array reference will be returned
if the option was specified, and undef otherwise.
=cut
sub GetOption {
my ( $Self, $Option ) = @_;
if ( !$Self->{_OptionSeen}->{$Option} ) {
$Self->PrintError("Option '--$Option' was not configured and cannot be accessed.");
return;
}
return $Self->{_ParsedARGV}->{Options}->{$Option};
}
=head2 PreRun()
perform additional validations/preparations before Run(). Override this method in your commands.
If this method returns, execution will be continued. If it throws an exception with die(), the program aborts at this point, and Run() will not be called.
=cut
sub PreRun {
return 1;
}
=head2 Run()
runs the command. Override this method in your commands.
This method needs to return the exit code to be used for the whole program.
For this, the convenience methods ExitCodeOk() and ExitCodeError() can be used.
In case of an exception, the error code will be set to 1 (error).
=cut
sub Run {
my $Self = shift;
return $Self->ExitCodeOk();
}
=head2 PostRun()
perform additional cleanups after Run(). Override this method in your commands.
The return value of this method will be ignored. It will be called after Run(), even
if Run() caused an exception or returned an error exit code.
In case of an exception in this method, the exit code will be set to 1 (error) if
Run() returned 0 (ok).
=cut
sub PostRun {
return;
}
=head2 Execute()
this method will parse/validate the command line arguments supplied by the user.
If that was ok, the Run() method of the command will be called.
=cut
sub Execute {
my ( $Self, @CommandlineArguments ) = @_;
# Normally, nothing was logged until this point, so the LogObject does not exist yet.
# Change the LogPrefix so that it indicates which command causes the log entry.
# In future we might need to check if it was created and update it on the fly.
$Kernel::OM->ObjectParamAdd(
'Kernel::System::Log' => {
LogPrefix => 'OTRS-otrs.Console.pl-' . $Self->Name(),
},
);
my $ParsedGlobalOptions = $Self->_ParseGlobalOptions( \@CommandlineArguments );
# Don't allow to run these scripts as root.
if ( !$ParsedGlobalOptions->{'allow-root'} && $> == 0 ) { # $EFFECTIVE_USER_ID
$Self->PrintError(
"You cannot run otrs.Console.pl as root. Please run it as the 'otrs' user or with the help of su:"
);
$Self->Print(" <yellow>su -c \"bin/otrs.Console.pl MyCommand\" -s /bin/bash otrs</yellow>\n");
return $Self->ExitCodeError();
}
# Disable in-memory cache to avoid problems with long running scripts.
$Kernel::OM->Get('Kernel::System::Cache')->Configure(
CacheInMemory => 0,
);
# Only run if the command was setup ok.
if ( !$Self->{_ConfigureSuccessful} ) {
$Self->PrintError("Aborting because the command was not successfully configured.");
return $Self->ExitCodeError();
}
# First handle the optional global options.
if ( $ParsedGlobalOptions->{'no-ansi'} ) {
$Self->ANSI(0);
}
if ( $ParsedGlobalOptions->{help} ) {
print "\n" . $Self->GetUsageHelp();
return $Self->ExitCodeOk();
}
if ( $ParsedGlobalOptions->{quiet} ) {
$Self->{Quiet} = 1;
}
# Parse command line arguments and bail out in case of error,
# of course with a helpful usage screen.
$Self->{_ParsedARGV} = $Self->_ParseCommandlineArguments( \@CommandlineArguments );
if ( !%{ $Self->{_ParsedARGV} // {} } ) {
print STDERR "\n" . $Self->GetUsageHelp();
return $Self->ExitCodeError();
}
# If we have an interactive console, make sure that the output can handle UTF-8.
if (
IO::Interactive::is_interactive()
&& !$Kernel::OM->Get('Kernel::Config')->Get('SuppressConsoleEncodingCheck')
)
{
my $ConsoleEncoding = lc $Encode::Locale::ENCODING_CONSOLE_OUT; ## no critic
if ( $ConsoleEncoding ne 'utf-8' ) {
$Self->PrintError(
"The terminal encoding should be set to 'utf-8', but is '$ConsoleEncoding'. Some characters might not be displayed correctly."
);
}
}
eval { $Self->PreRun(); };
if ($@) {
$Self->PrintError($@);
return $Self->ExitCodeError();
}
# Make sure we get a proper exit code to return to the shell.
my $ExitCode;
eval {
# Make sure that PostRun() works even if a user presses ^C.
local $SIG{INT} = sub {
$Self->PostRun();
exit $Self->ExitCodeError();
};
$ExitCode = $Self->Run();
};
if ($@) {
$Self->PrintError($@);
$ExitCode = $Self->ExitCodeError();
}
eval { $Self->PostRun(); };
if ($@) {
$Self->PrintError($@);
$ExitCode ||= $Self->ExitCodeError(); # switch from 0 (ok) to error
}
if ( !defined $ExitCode ) {
$Self->PrintError("Command $Self->{Name} did not return a proper exit code.");
$ExitCode = $Self->ExitCodeError();
}
return $ExitCode;
}
=head2 ExitCodeError()
returns an exit code to signal something went wrong (mostly for better
code readability).
return $CommandObject->ExitCodeError();
You can also provide a custom error code for special use cases:
return $CommandObject->ExitCodeError(255);
=cut
sub ExitCodeError {
my ( $Self, $CustomExitCode ) = @_;
return $CustomExitCode // 1;
}
=head2 ExitCodeOk()
returns 0 as exit code to indicate everything went fine in the command
(mostly for better code readability).
=cut
sub ExitCodeOk {
return 0;
}
=head2 GetUsageHelp()
generates usage / help screen for this command.
my $UsageHelp = $CommandObject->GetUsageHelp();
=cut
sub GetUsageHelp {
my $Self = shift;
my $UsageText = "<green>$Self->{Description}</green>\n";
$UsageText .= "\n<yellow>Usage:</yellow>\n";
$UsageText .= " otrs.Console.pl $Self->{Name}";
my $OptionsText = "<yellow>Options:</yellow>\n";
my $ArgumentsText = "<yellow>Arguments:</yellow>\n";
OPTION:
for my $Option ( @{ $Self->{_Options} // [] } ) {
my $OptionShort = "--$Option->{Name}";
if ( $Option->{HasValue} ) {
$OptionShort .= " ...";
if ( $Option->{Multiple} ) {
$OptionShort .= " ($OptionShort)";
}
}
if ( !$Option->{Required} ) {
$OptionShort = "[$OptionShort]";
}
$UsageText .= " $OptionShort";
$OptionsText .= sprintf " <green>%-30s</green> - %s", $OptionShort, $Option->{Description} . "\n";
}
# Global options only show up at the end of the options section, but not in the command line string as
# they don't actually belong to the current command (only).
GLOBALOPTION:
for my $Option ( @{ $Self->{_GlobalOptions} // [] } ) {
next GLOBALOPTION if $Option->{Invisible};
my $OptionShort = "[--$Option->{Name}]";
$OptionsText .= sprintf " <green>%-30s</green> - %s", $OptionShort, $Option->{Description} . "\n";
}
ARGUMENT:
for my $Argument ( @{ $Self->{_Arguments} // [] } ) {
my $ArgumentShort = $Argument->{Name};
if ( !$Argument->{Required} ) {
$ArgumentShort = "[$ArgumentShort]";
}
$UsageText .= " $ArgumentShort";
$ArgumentsText .= sprintf " <green>%-30s</green> - %s", $ArgumentShort,
$Argument->{Description} . "\n";
}
$UsageText .= "\n";
$UsageText .= "\n$OptionsText"; # Always present because of global options
if ( @{ $Self->{_Arguments} // [] } ) {
$UsageText .= "\n$ArgumentsText";
}
if ( $Self->AdditionalHelp() ) {
$UsageText .= "\n<yellow>Help:</yellow>\n";
$UsageText .= $Self->AdditionalHelp();
}
$UsageText .= "\n";
return $Self->_ReplaceColorTags($UsageText);
}
=head2 ANSI()
get/set support for colored text.
=cut
sub ANSI {
my ( $Self, $ANSI ) = @_;
$Self->{ANSI} = $ANSI if defined $ANSI;
return $Self->{ANSI};
}
=head2 PrintError()
shorthand method to print an error message to STDERR.
It will be prefixed with 'Error: ' and colored in red,
if the terminal supports it (see L</ANSI()>).
=cut
sub PrintError {
my ( $Self, $Text ) = @_;
chomp $Text;
print STDERR $Self->_Color( 'red', "Error: $Text\n" );
return;
}
=head2 Print()
this method will print the given text to STDOUT.
You can add color tags (C<< <green>...</green>, <yellow>...</yellow>, <red>...</red> >>)
to your text, and they will be replaced with the corresponding terminal escape sequences
if the terminal supports it (see L</ANSI()>).
=cut
sub Print {
my ( $Self, $Text ) = @_;
if ( !$Self->{Quiet} ) {
print $Self->_ReplaceColorTags($Text);
}
return;
}
=head2 TableOutput()
this method generates an ascii table of headers and column content
my $FormattedOutput = $Command->TableOutput(
TableData => {
Header => [
'First Header',
'Second Header',
'Third Header'
],
Body => [
[ 'FirstItem 1', 'SecondItem 1', 'ThirdItem 1' ],
[ 'FirstItem 2', 'SecondItem 2', 'ThirdItem 2' ],
[ 'FirstItem 3', 'SecondItem 3', 'ThirdItem 3' ],
[ 'FirstItem 4', 'SecondItem 4', 'ThirdItem 4' ],
],
},
Indention => 2, # Spaces to indent (ltr), default 0;
EvenOdd => 'yellow', # add even and odd line coloring (green|yellow|red)
# (overwrites existing coloring), # default 0
);
Returns:
+--------------+---------------+--------------+
| First Header | Second Header | Third Header |
+--------------+---------------+--------------+
| FirstItem 1 | SecondItem 1 | ThirdItem 1 |
| FirstItem 2 | SecondItem 2 | ThirdItem 1 |
| FirstItem 3 | SecondItem 3 | ThirdItem 1 |
| FirstItem 4 | SecondItem 4 | ThirdItem 1 |
+--------------+---------------+--------------+
=cut
sub TableOutput {
my ( $Self, %Param ) = @_;
return if $Param{TableData}->{Header} && !IsArrayRefWithData( $Param{TableData}->{Header} );
return if $Param{TableData}->{Body} && !IsArrayRefWithData( $Param{TableData}->{Body} );
my @MaxColumnLength;
# check for available header row and determine lengths
my $ShowHeader = IsArrayRefWithData( $Param{TableData}->{Header} ) ? 1 : 0;
if ($ShowHeader) {
my $HeaderCount = 0;
for my $Header ( @{ $Param{TableData}->{Header} } ) {
# detect coloring
my $PreparedHeader = $Header;
if ( $PreparedHeader =~ m/<.+?>.+?<\/.+?>/smx ) {
$PreparedHeader =~ s{ (<.+?>)(.+?)(<\/.+?>) }{$2}xmsg;
}
# detect header value length
if ( !$MaxColumnLength[$HeaderCount] || $MaxColumnLength[$HeaderCount] < length $PreparedHeader ) {
$MaxColumnLength[$HeaderCount] = length $PreparedHeader;
}
$HeaderCount++;
}
}
Row:
for my $Row ( @{ $Param{TableData}->{Body} } ) {
next ROW if !$Row;
next ROW if !IsArrayRefWithData($Row);
# determine maximum length of every column
my $ColumnCount = 0;
for my $Column ( @{$Row} ) {
# detect coloring
my $PreparedColumn = $Column || ' ';
if ( $PreparedColumn =~ m/<.+?>.+?<\/.+?>/smx ) {
$PreparedColumn =~ s{ (<.+?>)(.+?)(<\/.+?>) }{$2}xmsg;
}
# detect column value length
if ( !$MaxColumnLength[$ColumnCount] || $MaxColumnLength[$ColumnCount] < length $PreparedColumn ) {
$MaxColumnLength[$ColumnCount] = length $PreparedColumn;
}
$ColumnCount++;
}
}
# generate horizontal border
my $HorizontalBorder = '';
my $ColumnCount = 0;
for my $ColumnLength (@MaxColumnLength) {
# add space character before and after column content
$ColumnLength += 2;
# save new column length in maximum column length array
$MaxColumnLength[$ColumnCount] = $ColumnLength;
# save border part
$HorizontalBorder .= '+' . ( '-' x $ColumnLength );
$ColumnCount++;
}
$HorizontalBorder .= '+';
if ( $Param{Indention} ) {
my $Indention = ' ' x $Param{Indention};
$HorizontalBorder = $Indention . $HorizontalBorder;
}
# add first border to output
my $Output = $HorizontalBorder . "\n";
# add header row if available
if ($ShowHeader) {
my $HeaderContent = '';
my $HeaderCount = 0;
if ( $Param{Indention} ) {
my $Indention = ' ' x $Param{Indention};
$HeaderContent = $Indention . $HeaderContent;
}
for my $Header ( @{ $Param{TableData}->{Header} } ) {
# prepare header content
$HeaderContent .= '| ' . $Header;
# detect coloring
if ( $Header =~ m/<.+?>.+?<\/.+?>/smx ) {
$Header =~ s{ (<.+?>)(.+?)(<\/.+?>) }{$2}xmsg;
}
# determine difference between current header content and maximum content length
my $HeaderContentDiff = ( $MaxColumnLength[$HeaderCount] ) - ( length $Header );
# fill up with spaces
if ($HeaderContentDiff) {
$HeaderContent .= ' ' x ( $HeaderContentDiff - 1 );
}
$HeaderCount++;
}
# save the result as output
$Output .= $HeaderContent . "|\n";
# add horizontal border
$Output .= $HorizontalBorder . "\n";
}
my $EvenOddIndicator = 0;
# add body rows
Row:
for my $Row ( @{ $Param{TableData}->{Body} } ) {
next ROW if !$Row;
next ROW if !IsArrayRefWithData($Row);
my $RowContent = '';
my $ColumnCount = 0;
if ( $Param{Indention} ) {
my $Indention = ' ' x $Param{Indention};
$RowContent = $Indention . $RowContent;
}
for my $Column ( @{$Row} ) {
$Column = IsStringWithData($Column) ? $Column : ' ';
# even and odd coloring
if ( $Param{EvenOdd} ) {
if ( $Column =~ m/<.+?>.+?<\/.+?>/smx ) {
$Column =~ s{ (<.+?>)(.+?)(<\/.+?>) }{$2}xmsg;
}
if ($EvenOddIndicator) {
$Column = "<$Param{EvenOdd}>" . $Column . "</$Param{EvenOdd}>";
}
}
# prepare header content
$RowContent .= '| ' . $Column;
# detect coloring
if ( $Column =~ m/<.+?>.+?<\/.+?>/smx ) {
$Column =~ s{ (<.+?>)(.+?)(<\/.+?>) }{$2}xmsg;
}
# determine difference between current column content and maximum content length
my $RowContentDiff = ( $MaxColumnLength[$ColumnCount] ) - ( length $Column );
# fill up with spaces
if ($RowContentDiff) {
$RowContent .= ' ' x ( $RowContentDiff - 1 );
}
$ColumnCount++;
}
# toggle even odd indicator
$EvenOddIndicator = $EvenOddIndicator ? 0 : 1;
# save the result as output
$Output .= $RowContent . "|\n";
}
# add trailing horizontal border
$Output .= $HorizontalBorder . "\n";
return $Output // '';
}
=begin Internal:
=head2 _ParseGlobalOptions()
parses any global options possibly provided by the user.
Returns a hash with the option values.
=cut
sub _ParseGlobalOptions {
my ( $Self, $Arguments ) = @_;
Getopt::Long::Configure('pass_through');
Getopt::Long::Configure('no_auto_abbrev');
my %OptionValues;
OPTION:
for my $Option ( @{ $Self->{_GlobalOptions} } ) {
my $Value;
my $Lookup = $Option->{Name};
Getopt::Long::GetOptionsFromArray(
$Arguments,
$Lookup => \$Value,
);
$OptionValues{ $Option->{Name} } = $Value;
}
return \%OptionValues;
}
=head2 _ParseCommandlineArguments()
parses and validates the command line arguments provided by the user according to
the configured arguments and options of the command.
Returns a hash with argument and option values if all needed values were supplied
and correct, or undef otherwise.
=cut
sub _ParseCommandlineArguments {
my ( $Self, $Arguments ) = @_;
Getopt::Long::Configure('pass_through');
Getopt::Long::Configure('no_auto_abbrev');
my %OptionValues;
OPTION:
for my $Option ( @{ $Self->{_Options} // [] }, @{ $Self->{_GlobalOptions} } ) {
my $Lookup = $Option->{Name};
if ( $Option->{HasValue} ) {
$Lookup .= '=s';
if ( $Option->{Multiple} ) {
$Lookup .= '@';
}
}
# Option with multiple values
if ( $Option->{HasValue} && $Option->{Multiple} ) {
my @Values;
Getopt::Long::GetOptionsFromArray(
$Arguments,
$Lookup => \@Values,
);
if ( !@Values ) {
if ( !$Option->{Required} ) {
next OPTION;
}
$Self->PrintError("please provide option '--$Option->{Name}'.");
return;
}
for my $Value (@Values) {
if ( $Option->{HasValue} && $Value !~ $Option->{ValueRegex} ) {
$Self->PrintError("please provide a valid value for option '--$Option->{Name}'.");
return;
}
}
$OptionValues{ $Option->{Name} } = \@Values;
}
# Option with no or a single value
else {
my $Value;
Getopt::Long::GetOptionsFromArray(
$Arguments,
$Lookup => \$Value,
);
if ( !defined $Value ) {
if ( !$Option->{Required} ) {
next OPTION;
}
$Self->PrintError("please provide option '--$Option->{Name}'.");
return;
}
if ( $Option->{HasValue} && $Value !~ $Option->{ValueRegex} ) {
$Self->PrintError("please provide a valid value for option '--$Option->{Name}'.");
return;
}
$OptionValues{ $Option->{Name} } = $Value;
}
}
my %ArgumentValues;
ARGUMENT:
for my $Argument ( @{ $Self->{_Arguments} // [] } ) {
if ( !@{$Arguments} ) {
if ( !$Argument->{Required} ) {
next ARGUMENT;
}
$Self->PrintError("please provide a value for argument '$Argument->{Name}'.");
return;
}
my $Value = shift @{$Arguments};
if ( $Value !~ $Argument->{ValueRegex} ) {
$Self->PrintError("please provide a valid value for argument '$Argument->{Name}'.");
return;
}
$ArgumentValues{ $Argument->{Name} } = $Value;
}
# check for superfluous arguments
if ( @{$Arguments} ) {
my $Error = "found unknown arguments on the command line ('";
$Error .= join "', '", @{$Arguments};
$Error .= "').\n";
$Self->PrintError($Error);
return;
}
return {
Options => \%OptionValues,
Arguments => \%ArgumentValues,
};
}
=head2 _Color()
this will color the given text (see Term::ANSIColor::color()) if
ANSI output is available and active, otherwise the text stays unchanged.
my $PossiblyColoredText = $CommandObject->_Color('green', $Text);
=cut
sub _Color {
my ( $Self, $Color, $Text ) = @_;
return $Text if !$Self->{ANSI};
return $Text if $SuppressANSI;
return Term::ANSIColor::color($Color) . $Text . Term::ANSIColor::color('reset');
}
sub _ReplaceColorTags {
my ( $Self, $Text ) = @_;
$Text =~ s{<(green|yellow|red)>(.*?)</\1>}{$Self->_Color($1, $2)}gsmxe;
return $Text;
}
1;
=end Internal:
=head1 TERMS AND CONDITIONS
This software is part of the OTRS project (L<https://otrs.org/>).
This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.
=cut
|