2015-09-08 13:31:24 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# CONFIG HELP MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Config::ConfigHelp;
|
2015-09-08 13:31:24 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
|
|
|
use Exporter qw(import);
|
|
|
|
our @EXPORT = qw();
|
|
|
|
use File::Basename qw(dirname);
|
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Ini;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Common::String;
|
|
|
|
use pgBackRest::Config::Config;
|
|
|
|
use pgBackRest::Version;
|
2015-09-08 13:31:24 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Help types
|
|
|
|
####################################################################################################################################
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_COMMAND => 'command';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_COMMAND);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_CURRENT => 'current';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_CURRENT);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_DEFAULT => 'default';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_DEFAULT);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_DESCRIPTION => 'description';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_DESCRIPTION);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_OPTION => 'option';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_OPTION);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_SECTION => 'section';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_SECTION);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_SUMMARY => 'summary';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_SUMMARY);
|
|
|
|
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_SOURCE => 'source';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_SOURCE);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_SOURCE_DEFAULT => 'default';
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_SOURCE_DEFAULT);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_SOURCE_SECTION => CONFIG_HELP_SECTION;
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_SOURCE_SECTION);
|
2015-10-28 11:10:36 +02:00
|
|
|
use constant CONFIG_HELP_SOURCE_COMMAND => CONFIG_HELP_COMMAND;
|
2015-09-08 13:31:24 +02:00
|
|
|
push @EXPORT, qw(CONFIG_HELP_SOURCE_COMMAND);
|
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# Config Section Types
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant CONFIG_SECTION_COMMAND => 'command';
|
|
|
|
push @EXPORT, qw(CONFIG_SECTION_COMMAND);
|
|
|
|
use constant CONFIG_SECTION_GENERAL => 'general';
|
|
|
|
push @EXPORT, qw(CONFIG_SECTION_GENERAL);
|
|
|
|
use constant CONFIG_SECTION_LOG => 'log';
|
|
|
|
push @EXPORT, qw(CONFIG_SECTION_LOG);
|
|
|
|
use constant CONFIG_SECTION_EXPIRE => 'expire';
|
|
|
|
push @EXPORT, qw(CONFIG_SECTION_EXPIRE);
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# configHelp
|
|
|
|
#
|
|
|
|
# Display command-line help.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub configHelp
|
|
|
|
{
|
|
|
|
my $strCommand = shift;
|
|
|
|
my $strOption = shift;
|
2015-09-09 21:40:54 +02:00
|
|
|
my $bVersion = shift;
|
|
|
|
my $bConfigResult = shift;
|
2015-09-08 13:31:24 +02:00
|
|
|
|
|
|
|
# Load module dynamically
|
2016-04-14 15:30:54 +02:00
|
|
|
require pgBackRest::Config::ConfigHelpData;
|
|
|
|
pgBackRest::Config::ConfigHelpData->import();
|
2015-09-08 13:31:24 +02:00
|
|
|
|
|
|
|
# Get config data
|
|
|
|
my $oCommandHash = commandHashGet();
|
|
|
|
my $oOptionRule = optionRuleGet();
|
|
|
|
my $oConfigHelpData = configHelpDataGet();
|
|
|
|
|
2015-09-09 21:40:54 +02:00
|
|
|
# Build version
|
|
|
|
my $strVersion = (!$bConfigResult ? "\n" : '') . BACKREST_NAME . ' ' . BACKREST_VERSION;
|
|
|
|
|
|
|
|
# Display version
|
|
|
|
if ($bVersion)
|
|
|
|
{
|
|
|
|
syswrite(*STDOUT, "${strVersion}\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
# Build the title
|
|
|
|
my $strTitle;
|
|
|
|
my $strHelp;
|
|
|
|
|
|
|
|
# Since there's no command this will be general help
|
|
|
|
if (!defined($strCommand))
|
|
|
|
{
|
|
|
|
$strTitle = "General"
|
|
|
|
}
|
|
|
|
# Else check the command
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strCommand = lc($strCommand);
|
|
|
|
|
|
|
|
if (defined($$oCommandHash{$strCommand}))
|
|
|
|
{
|
|
|
|
$strTitle = "'${strCommand}' command";
|
|
|
|
|
|
|
|
# And check the option
|
|
|
|
if (defined($strOption))
|
|
|
|
{
|
|
|
|
$strOption = lc($strOption);
|
|
|
|
|
|
|
|
if (defined($$oConfigHelpData{&CONFIG_HELP_COMMAND}{$strCommand}{&CONFIG_HELP_OPTION}{$strOption}))
|
|
|
|
{
|
|
|
|
$strTitle .= " - '${strOption}' option";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strHelp = "Invalid option '${strOption}' for command '${strCommand}'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strHelp = "Invalid command '${strCommand}'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Build the help
|
|
|
|
my $strMore;
|
|
|
|
|
|
|
|
if (!defined($strHelp))
|
|
|
|
{
|
|
|
|
my $iScreenWidth = 80;
|
|
|
|
|
|
|
|
# General help
|
|
|
|
if (!defined($strCommand))
|
|
|
|
{
|
|
|
|
$strHelp =
|
|
|
|
"Usage:\n" .
|
|
|
|
" " . BACKREST_EXE . " [options] [command]\n\n" .
|
|
|
|
"Commands:\n";
|
|
|
|
|
|
|
|
# Find longest command length
|
|
|
|
my $iCommandLength = 0;
|
|
|
|
|
2015-09-08 18:58:13 +02:00
|
|
|
foreach my $strCommand (sort(keys(%{$$oConfigHelpData{&CONFIG_HELP_COMMAND}})))
|
2015-09-08 13:31:24 +02:00
|
|
|
{
|
|
|
|
if (length($strCommand) > $iCommandLength)
|
|
|
|
{
|
|
|
|
$iCommandLength = length($strCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output commands
|
2015-09-08 18:58:13 +02:00
|
|
|
foreach my $strCommand (sort(keys(%{$$oConfigHelpData{&CONFIG_HELP_COMMAND}})))
|
2015-09-08 13:31:24 +02:00
|
|
|
{
|
|
|
|
my $oCommand = $$oConfigHelpData{&CONFIG_HELP_COMMAND}{$strCommand};
|
|
|
|
|
|
|
|
$strHelp .= " ${strCommand}" . (' ' x ($iCommandLength - length($strCommand)));
|
|
|
|
$strHelp .=
|
2016-02-23 16:25:22 +02:00
|
|
|
' ' .
|
|
|
|
configHelpFormatText($$oCommand{&CONFIG_HELP_SUMMARY}, 4 + $iCommandLength + 2, false, $iScreenWidth + 1) .
|
2015-09-08 13:31:24 +02:00
|
|
|
"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$strMore = '[command]';
|
|
|
|
}
|
|
|
|
# Else command help
|
|
|
|
elsif (!defined($strOption))
|
|
|
|
{
|
|
|
|
my $oCommand = $$oConfigHelpData{&CONFIG_HELP_COMMAND}{$strCommand};
|
|
|
|
|
|
|
|
$strHelp =
|
2016-02-23 16:25:22 +02:00
|
|
|
configHelpFormatText($$oCommand{&CONFIG_HELP_SUMMARY} . "\n\n" .
|
2015-09-08 13:31:24 +02:00
|
|
|
$$oCommand{&CONFIG_HELP_DESCRIPTION}, 0, true, $iScreenWidth + 1);
|
|
|
|
|
|
|
|
# Find longest option length and unique list of sections
|
|
|
|
my $iOptionLength = 0;
|
|
|
|
my $oSection = {};
|
|
|
|
|
|
|
|
if (defined($$oCommand{&CONFIG_HELP_OPTION}))
|
|
|
|
{
|
2015-09-08 18:58:13 +02:00
|
|
|
foreach my $strOption (sort(keys(%{$$oCommand{&CONFIG_HELP_OPTION}})))
|
2015-09-08 13:31:24 +02:00
|
|
|
{
|
|
|
|
if (length($strOption) > $iOptionLength)
|
|
|
|
{
|
|
|
|
$iOptionLength = length($strOption);
|
|
|
|
}
|
|
|
|
|
2016-02-23 16:25:22 +02:00
|
|
|
my ($oOption, $strSection) = configHelpOptionFind($oConfigHelpData, $oOptionRule, $strCommand, $strOption);
|
2015-09-08 13:31:24 +02:00
|
|
|
|
|
|
|
$$oSection{$strSection}{$strOption} = $oOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Iterate sections
|
2015-09-08 18:58:13 +02:00
|
|
|
foreach my $strSection (sort(keys(%{$oSection})))
|
2015-09-08 13:31:24 +02:00
|
|
|
{
|
|
|
|
$strHelp .=
|
|
|
|
"\n\n" . ucfirst($strSection) . " Options:\n";
|
|
|
|
|
|
|
|
# Iterate options
|
2015-09-08 18:58:13 +02:00
|
|
|
foreach my $strOption (sort(keys(%{$$oSection{$strSection}})))
|
2015-09-08 13:31:24 +02:00
|
|
|
{
|
|
|
|
$strHelp .= "\n";
|
|
|
|
|
|
|
|
my $iIndent = 4 + $iOptionLength + 2;
|
|
|
|
my $oOption = $$oSection{$strSection}{$strOption};
|
|
|
|
|
|
|
|
# Set current and default values
|
|
|
|
my $strDefault = '';
|
|
|
|
|
|
|
|
if ($$oOption{&CONFIG_HELP_CURRENT} || $$oOption{&CONFIG_HELP_DEFAULT})
|
|
|
|
{
|
|
|
|
$strDefault = undef;
|
|
|
|
|
|
|
|
if ($$oOption{&CONFIG_HELP_CURRENT})
|
|
|
|
{
|
|
|
|
$strDefault .= 'current=' . $$oOption{&CONFIG_HELP_CURRENT};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($$oOption{&CONFIG_HELP_DEFAULT})
|
|
|
|
{
|
|
|
|
if (defined($strDefault))
|
|
|
|
{
|
|
|
|
$strDefault .= ', ';
|
|
|
|
}
|
|
|
|
|
|
|
|
$strDefault .= 'default=' . $$oOption{&CONFIG_HELP_DEFAULT};
|
|
|
|
}
|
|
|
|
|
|
|
|
$strDefault = " [${strDefault}]";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output help
|
|
|
|
$strHelp .= " --${strOption}" . (' ' x ($iOptionLength - length($strOption)));
|
2016-02-23 16:25:22 +02:00
|
|
|
$strHelp .= ' ' . configHelpFormatText(lcfirst(substr($$oOption{&CONFIG_HELP_SUMMARY}, 0,
|
|
|
|
length($$oOption{&CONFIG_HELP_SUMMARY}) - 1)) .
|
|
|
|
$strDefault, $iIndent, false, $iScreenWidth + 1);
|
2015-09-08 13:31:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$strMore = "${strCommand} [option]";
|
|
|
|
$strHelp .= "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Else option help
|
|
|
|
else
|
|
|
|
{
|
2016-02-23 16:25:22 +02:00
|
|
|
my ($oOption) = configHelpOptionFind($oConfigHelpData, $oOptionRule, $strCommand, $strOption);
|
2015-09-08 13:31:24 +02:00
|
|
|
|
|
|
|
# Set current and default values
|
|
|
|
my $strDefault = '';
|
|
|
|
|
|
|
|
if ($$oOption{&CONFIG_HELP_CURRENT} || $$oOption{&CONFIG_HELP_DEFAULT})
|
|
|
|
{
|
|
|
|
$strDefault = undef;
|
|
|
|
|
|
|
|
if ($$oOption{&CONFIG_HELP_CURRENT})
|
|
|
|
{
|
|
|
|
$strDefault = 'current: ' . $$oOption{&CONFIG_HELP_CURRENT};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($$oOption{&CONFIG_HELP_DEFAULT})
|
|
|
|
{
|
|
|
|
if (defined($strDefault))
|
|
|
|
{
|
|
|
|
$strDefault .= "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$strDefault .= 'default: ' . $$oOption{&CONFIG_HELP_DEFAULT};
|
|
|
|
}
|
|
|
|
|
|
|
|
$strDefault = "\n\n${strDefault}";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output help
|
|
|
|
$strHelp =
|
2016-02-23 16:25:22 +02:00
|
|
|
configHelpFormatText($$oOption{&CONFIG_HELP_SUMMARY} . "\n\n" . $$oOption{&CONFIG_HELP_DESCRIPTION} .
|
|
|
|
$strDefault, 0, true, $iScreenWidth + 1);
|
2015-09-08 13:31:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Output help
|
2015-09-09 21:40:54 +02:00
|
|
|
syswrite(*STDOUT, "${strVersion} -" . (defined($strTitle) ? " ${strTitle}" : '') . " help\n\n${strHelp}\n" .
|
2015-09-08 13:31:24 +02:00
|
|
|
(defined($strMore) ? 'Use \'' . BACKREST_EXE . " help ${strMore}' for more information.\n" : ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(configHelp);
|
|
|
|
|
2016-02-23 16:25:22 +02:00
|
|
|
# Helper function for configHelp() to make output look good on a console
|
|
|
|
sub configHelpFormatText
|
|
|
|
{
|
|
|
|
my $strTextIn = shift;
|
|
|
|
my $iIndent = shift;
|
|
|
|
my $bIndentFirst = shift;
|
|
|
|
my $iLength = shift;
|
|
|
|
|
|
|
|
my @stryText = split("\n", trim($strTextIn));
|
|
|
|
my $strText;
|
|
|
|
my $iIndex = 0;
|
|
|
|
|
|
|
|
foreach my $strLine (@stryText)
|
|
|
|
{
|
|
|
|
if (defined($strText))
|
|
|
|
{
|
|
|
|
$strText .= "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
my $strPart;
|
|
|
|
my $bFirst = true;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
($strPart, $strLine) = stringSplit($strLine, ' ', $iLength - $iIndent);
|
|
|
|
|
|
|
|
if (!$bFirst || $bIndentFirst)
|
|
|
|
{
|
|
|
|
if (!$bFirst)
|
|
|
|
{
|
|
|
|
$strText .= "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$strText .= ' ' x $iIndent;
|
|
|
|
}
|
|
|
|
|
|
|
|
$strText .= trim($strPart);
|
|
|
|
|
|
|
|
$bFirst = false;
|
|
|
|
}
|
|
|
|
while (defined($strLine));
|
|
|
|
|
|
|
|
$iIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $strText;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Helper function for configHelp() to find options. The option may be stored with the command or in the option list depending on
|
|
|
|
# whether it's generic or command-specific
|
|
|
|
sub configHelpOptionFind
|
|
|
|
{
|
|
|
|
my $oConfigHelpData = shift;
|
|
|
|
my $oOptionRule = shift;
|
|
|
|
my $strCommand = shift;
|
|
|
|
my $strOption = shift;
|
|
|
|
|
|
|
|
my $strSection = CONFIG_HELP_COMMAND;
|
|
|
|
my $oOption = $$oConfigHelpData{&CONFIG_HELP_COMMAND}{$strCommand}{&CONFIG_HELP_OPTION}{$strOption};
|
|
|
|
|
|
|
|
if (ref(\$oOption) eq 'SCALAR')
|
|
|
|
{
|
|
|
|
$oOption = $$oConfigHelpData{&CONFIG_HELP_OPTION}{$strOption};
|
|
|
|
|
|
|
|
if (defined($$oOption{&CONFIG_HELP_SECTION}))
|
|
|
|
{
|
|
|
|
$strSection = $$oOption{&CONFIG_HELP_SECTION};
|
|
|
|
|
|
|
|
if ($strSection eq CONFIG_SECTION_COMMAND)
|
|
|
|
{
|
|
|
|
$strSection = CONFIG_SECTION_GENERAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$strSection = CONFIG_SECTION_GENERAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($strSection ne CONFIG_SECTION_GENERAL && $strSection ne CONFIG_SECTION_LOG &&
|
|
|
|
$strSection ne CONFIG_SECTION_STANZA && $strSection ne CONFIG_SECTION_EXPIRE) ||
|
|
|
|
$strSection eq $strCommand)
|
|
|
|
{
|
|
|
|
$strSection = CONFIG_HELP_COMMAND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined(optionDefault($strOption, $strCommand)))
|
|
|
|
{
|
|
|
|
if ($$oOptionRule{$strOption}{&OPTION_RULE_TYPE} eq &OPTION_TYPE_BOOLEAN)
|
|
|
|
{
|
|
|
|
$$oOption{&CONFIG_HELP_DEFAULT} = optionDefault($strOption, $strCommand) ? 'y' : 'n';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$$oOption{&CONFIG_HELP_DEFAULT} = optionDefault($strOption, $strCommand);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optionTest($strOption) && optionSource($strOption) ne CONFIG_HELP_SOURCE_DEFAULT)
|
|
|
|
{
|
|
|
|
if ($$oOptionRule{$strOption}{&OPTION_RULE_TYPE} eq &OPTION_TYPE_BOOLEAN)
|
|
|
|
{
|
|
|
|
$$oOption{&CONFIG_HELP_CURRENT} = optionGet($strOption) ? 'y' : 'n';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$$oOption{&CONFIG_HELP_CURRENT} = optionGet($strOption);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $oOption, $strSection;
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:31:24 +02:00
|
|
|
1;
|