1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-09 00:45:49 +02:00

Refactor config parse to remove none command, add version/help options.

The none command was a bit confusing since it was only valid when parsing failed but still needed to be added to various switches and logic. Replace with cfgInited() which should make it clearer what state configuration is in.

Make the default command help and convert --version and --help to real options.

Combine version and help output into a single function to simplify processing in main.

Additional reformatting and a bit of refactoring.
This commit is contained in:
David Steele
2024-07-23 16:39:02 +07:00
parent 6c757366c2
commit 8d6bceb541
29 changed files with 1124 additions and 978 deletions

View File

@ -33,5 +33,4 @@ data_start
environ@GLIBC_2.2.5
main
stderr@GLIBC_2.2.5
stdout@GLIBC_2.2.5
xmlFree@LIBXML2_2.4.30

View File

@ -45,6 +45,14 @@ option:
- 512KiB
- 1MiB
help:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
neutral-umask:
type: boolean
internal: true
@ -56,6 +64,14 @@ option:
command:
build: {}
version:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
# Logging options
#---------------------------------------------------------------------------------------------------------------------------------
log-level:

View File

@ -98,6 +98,28 @@
<text>
<p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>doc-pgbackrest help build</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>doc-pgbackrest help build repo-path</cmd>) then a full description of the option as it applies to the command will be displayed.</p>
</text>
<option-list>
<option id="help" name="Display Help">
<summary>Display help.</summary>
<text>
<p>Displays help even if the <cmd>help</cmd> command is not specified and overrides the <br-option>--version</br-option> option.</p>
</text>
<example>y</example>
</option>
<option id="version" name="Display Version">
<summary>Display version.</summary>
<text>
<p>Displays version even if the <cmd>version</cmd> or <cmd>help</cmd> command is not specified.</p>
</text>
<example>y</example>
</option>
</option-list>
</command>
<!-- Noop command holds options that must be defined but we don't need -->

View File

@ -48,10 +48,16 @@ cfgLoadUpdateOption(void)
THROW_ON_SYS_ERROR(getcwd(currentWorkDir, sizeof(currentWorkDir)) == NULL, FormatError, "unable to get cwd");
// If repo-path is relative then make it absolute
if (cfgOptionValid(cfgOptRepoPath))
{
const String *const repoPath = cfgOptionStr(cfgOptRepoPath);
if (!strBeginsWithZ(repoPath, "/"))
cfgOptionSet(cfgOptRepoPath, cfgOptionSource(cfgOptRepoPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(repoPath))));
{
cfgOptionSet(
cfgOptRepoPath, cfgOptionSource(cfgOptRepoPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(repoPath))));
}
}
FUNCTION_LOG_RETURN_VOID();
}
@ -89,9 +95,6 @@ cfgLoad(unsigned int argListSize, const char *argList[])
if (cfgCommand() == cfgCmdNoop)
THROW(CommandInvalidError, "invalid command '" CFGCMD_NOOP "'");
// If a command is set
if (cfgCommand() != cfgCmdNone && cfgCommand() != cfgCmdHelp && cfgCommand() != cfgCmdVersion)
{
// Load the log settings
if (!cfgCommandHelp())
cfgLoadLogSetting();
@ -110,7 +113,6 @@ cfgLoad(unsigned int argListSize, const char *argList[])
// Begin the command
cmdBegin();
}
}
MEM_CONTEXT_TEMP_END();
FUNCTION_LOG_RETURN_VOID();

View File

@ -65,22 +65,15 @@ main(int argListSize, const char *argList[])
cmdBuild(cfgOptionStr(cfgOptRepoPath));
break;
// Help
// Help/version
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp:
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Version
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdVersion:
printf(PROJECT_NAME " Documentation " PROJECT_VERSION "\n");
fflush(stdout);
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Error on commands that should have been handled
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdNone:
case cfgCmdNoop:
THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName());
break;

View File

@ -300,6 +300,14 @@ option:
command-role:
main: {}
help:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
online:
type: boolean
default: true
@ -501,6 +509,14 @@ option:
command-role:
main: {}
version:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
# Command-line only local/remote options
#---------------------------------------------------------------------------------------------------------------------------------
exec-id:

View File

@ -193,8 +193,6 @@ bldCfgRenderConfigAutoH(const Storage *const storageRepo, const BldCfg bldCfg)
strCatFmt(config, " %s,\n", strZ(bldEnumCmd(cmd->name)));
}
strCatFmt(config, " %s,\n", strZ(bldEnumCmd(STRDEF("none"))));
strCatZ(
config,
"} ConfigCommand;\n");

View File

@ -2572,6 +2572,28 @@
<text>
<p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>pgbackrest help backup</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>pgbackrest help backup type</cmd>) then a full description of the option as it applies to the command will be displayed.</p>
</text>
<option-list>
<option id="help" name="Display Help">
<summary>Display help.</summary>
<text>
<p>Displays help even if the <cmd>help</cmd> command is not specified and overrides the <br-option>--version</br-option> option.</p>
</text>
<example>y</example>
</option>
<option id="version" name="Display Version">
<summary>Display version.</summary>
<text>
<p>Displays version even if the <cmd>version</cmd> or <cmd>help</cmd> command is not specified.</p>
</text>
<example>y</example>
</option>
</option-list>
</command>
<command id="server" name="Server">

View File

@ -163,7 +163,7 @@ cmdBegin(void)
{
FUNCTION_LOG_VOID(logLevelTrace);
ASSERT(cfgCommand() != cfgCmdNone);
ASSERT(cfgInited());
// This is fairly expensive log message to generate so skip it if it won't be output
if (logAny(cfgLogLevelDefault()))
@ -198,7 +198,7 @@ cmdEnd(const int code, const String *const errorMessage)
FUNCTION_LOG_PARAM(STRING, errorMessage);
FUNCTION_LOG_END();
ASSERT(cfgCommand() != cfgCmdNone);
ASSERT(cfgInited());
// Skip this log message if it won't be output. It's not too expensive but since we skipped cmdBegin(), may as well.
if (logAny(cfgLogLevelDefault()))

View File

@ -11,7 +11,7 @@ Exit Routines
#include "command/lock.h"
#include "common/debug.h"
#include "common/log.h"
#include "config/config.h"
#include "config/config.intern.h"
#include "protocol/helper.h"
#include "version.h"
@ -137,12 +137,11 @@ exitSafe(int result, const bool error, const SignalType signalType)
result = errorCode();
}
// Log command end if a command is set
if (cfgCommand() != cfgCmdNone)
// On error generate an error message for command end when config is initialized
if (cfgInited())
{
String *errorMessage = NULL;
// On error generate an error message
if (result != 0)
{
// On process terminate

View File

@ -254,6 +254,16 @@ helpRender(const Buffer *const helpData)
String *const result = strCatZ(strNew(), PROJECT_NAME " " PROJECT_VERSION);
// Display version only
if (!cfgCommandHelp() &&
((cfgCommand() == cfgCmdHelp && cfgOptionBool(cfgOptVersion) && !cfgOptionBool(cfgOptHelp)) ||
cfgCommand() == cfgCmdVersion))
{
strCatChr(result, '\n');
FUNCTION_LOG_RETURN(STRING, result);
}
MEM_CONTEXT_TEMP_BEGIN()
{
// Set a small buffer size to minimize memory usage
@ -287,7 +297,7 @@ helpRender(const Buffer *const helpData)
const String *more = NULL;
// Display general help
if (cfgCommand() == cfgCmdNone)
if (!cfgCommandHelp())
{
strCatZ(
result,

View File

@ -78,6 +78,7 @@ Option constants
#define CFGOPT_EXPIRE_AUTO "expire-auto"
#define CFGOPT_FILTER "filter"
#define CFGOPT_FORCE "force"
#define CFGOPT_HELP "help"
#define CFGOPT_IGNORE_MISSING "ignore-missing"
#define CFGOPT_IO_TIMEOUT "io-timeout"
#define CFGOPT_JOB_RETRY "job-retry"
@ -135,8 +136,9 @@ Option constants
#define CFGOPT_TLS_SERVER_PORT "tls-server-port"
#define CFGOPT_TYPE "type"
#define CFGOPT_VERBOSE "verbose"
#define CFGOPT_VERSION "version"
#define CFG_OPTION_TOTAL 181
#define CFG_OPTION_TOTAL 183
/***********************************************************************************************************************************
Option value constants
@ -366,7 +368,6 @@ typedef enum
cfgCmdStop,
cfgCmdVerify,
cfgCmdVersion,
cfgCmdNone,
} ConfigCommand;
/***********************************************************************************************************************************
@ -418,6 +419,7 @@ typedef enum
cfgOptExpireAuto,
cfgOptFilter,
cfgOptForce,
cfgOptHelp,
cfgOptIgnoreMissing,
cfgOptIoTimeout,
cfgOptJobRetry,
@ -564,6 +566,7 @@ typedef enum
cfgOptTlsServerPort,
cfgOptType,
cfgOptVerbose,
cfgOptVersion,
} ConfigOption;
#endif

View File

@ -37,19 +37,27 @@ cfgInit(Config *const config)
FUNCTION_TEST_RETURN_VOID();
}
FN_EXTERN bool
cfgInited(void)
{
FUNCTION_TEST_VOID();
FUNCTION_TEST_RETURN(ENUM, configLocal != NULL);
}
/**********************************************************************************************************************************/
FN_EXTERN ConfigCommand
cfgCommand(void)
{
FUNCTION_TEST_VOID();
FUNCTION_TEST_RETURN(ENUM, configLocal == NULL ? cfgCmdNone : configLocal->command);
ASSERT(cfgInited());
FUNCTION_TEST_RETURN(ENUM, configLocal->command);
}
FN_EXTERN ConfigCommandRole
cfgCommandRole(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
FUNCTION_TEST_RETURN(ENUM, configLocal->commandRole);
}
@ -61,8 +69,8 @@ cfgCommandSet(ConfigCommand commandId, ConfigCommandRole commandRoleId)
FUNCTION_TEST_PARAM(ENUM, commandRoleId);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(commandId <= cfgCmdNone);
ASSERT(cfgInited());
ASSERT(commandId < CFG_COMMAND_TOTAL);
configLocal->command = commandId;
configLocal->commandRole = commandRoleId;
@ -85,7 +93,7 @@ cfgCommandJobRetry(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
// Return NULL if no retries
const unsigned int retryTotal = cfgOptionUInt(cfgOptJobRetry);
@ -112,8 +120,8 @@ cfgCommandName(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(configLocal->command < cfgCmdNone);
ASSERT(cfgInited());
ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN_CONST(STRINGZ, cfgParseCommandName(configLocal->command));
}
@ -131,7 +139,7 @@ cfgCommandParam(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
if (configLocal->paramList == NULL)
{
@ -150,7 +158,7 @@ FN_EXTERN const String *
cfgExe(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
FUNCTION_TEST_RETURN(STRING, configLocal->exe);
}
@ -160,8 +168,8 @@ cfgLockRequired(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(configLocal->command != cfgCmdNone);
ASSERT(cfgInited());
ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
// Local roles never take a lock and the remote role has special logic for locking
FUNCTION_TEST_RETURN(
@ -178,8 +186,8 @@ cfgLockRemoteRequired(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(configLocal->command != cfgCmdNone);
ASSERT(cfgInited());
ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN(BOOL, configLocal->lockRemoteRequired);
}
@ -190,8 +198,8 @@ cfgLockType(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(configLocal->command != cfgCmdNone);
ASSERT(cfgInited());
ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN(ENUM, configLocal->lockType);
}
@ -202,8 +210,8 @@ cfgLogFile(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(configLocal->command != cfgCmdNone);
ASSERT(cfgInited());
ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN(
BOOL,
@ -221,8 +229,8 @@ cfgLogLevelDefault(void)
{
FUNCTION_TEST_VOID();
ASSERT(configLocal != NULL);
ASSERT(configLocal->command != cfgCmdNone);
ASSERT(cfgInited());
ASSERT(configLocal->command < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN(ENUM, configLocal->logLevelDefault);
}
@ -235,7 +243,7 @@ cfgOptionGroup(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL);
FUNCTION_TEST_RETURN(BOOL, configLocal->option[optionId].group);
@ -250,7 +258,7 @@ cfgOptionGroupName(const ConfigOptionGroup groupId, const unsigned int groupIdx)
FUNCTION_TEST_PARAM(UINT, groupIdx);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(groupId < CFG_OPTION_GROUP_TOTAL);
ASSERT(groupIdx < configLocal->optionGroup[groupId].indexTotal);
@ -280,7 +288,7 @@ cfgOptionGroupId(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal->option[optionId].group);
@ -295,7 +303,7 @@ cfgOptionGroupIdxDefault(const ConfigOptionGroup groupId)
FUNCTION_TEST_PARAM(ENUM, groupId);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(groupId < CFG_OPTION_GROUP_TOTAL);
ASSERT(configLocal->optionGroup[groupId].indexDefaultExists);
@ -311,7 +319,7 @@ cfgOptionGroupIdxToKey(const ConfigOptionGroup groupId, const unsigned int group
FUNCTION_TEST_PARAM(UINT, groupIdx);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(groupId < CFG_OPTION_GROUP_TOTAL);
ASSERT(groupIdx < configLocal->optionGroup[groupId].indexTotal);
@ -327,7 +335,7 @@ cfgOptionKeyToIdx(const ConfigOption optionId, const unsigned int key)
FUNCTION_TEST_PARAM(UINT, key);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL);
unsigned int result = 0;
@ -360,7 +368,7 @@ cfgOptionGroupIdxTotal(const ConfigOptionGroup groupId)
FUNCTION_TEST_PARAM(ENUM, groupId);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(groupId < CFG_OPTION_GROUP_TOTAL);
FUNCTION_TEST_RETURN(UINT, configLocal->optionGroup[groupId].indexTotal);
@ -374,7 +382,7 @@ cfgOptionIdxDefault(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(
!configLocal->option[optionId].group || configLocal->optionGroup[configLocal->option[optionId].groupId].indexDefaultExists);
@ -392,7 +400,7 @@ cfgOptionIdxTotal(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL);
const ConfigOptionData *const option = &configLocal->option[optionId];
@ -408,7 +416,7 @@ cfgOptionDefault(const ConfigOption optionId)
FUNCTION_TEST_PARAM(ENUM, optionId);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(optionId < CFG_OPTION_TOTAL);
ConfigOptionData *const option = &configLocal->option[optionId];
@ -428,7 +436,7 @@ cfgOptionDefaultSet(const ConfigOption optionId, const Variant *defaultValue)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT(configLocal->option[optionId].valid);
ASSERT(cfgParseOptionDataType(optionId) == cfgOptDataTypeString);
@ -500,7 +508,7 @@ cfgOptionIdxDisplay(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -559,7 +567,7 @@ cfgOptionIdxName(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -604,7 +612,7 @@ cfgOptionIdxNegate(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -622,7 +630,7 @@ cfgOptionIdxReset(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -644,7 +652,7 @@ cfgOptionIdxInternal(
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -681,7 +689,7 @@ cfgOptionIdxVar(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_PARAM(UINT, optionIdx);
FUNCTION_TEST_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -813,7 +821,7 @@ cfgOptionIdxLst(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_LOG_PARAM(UINT, optionIdx);
FUNCTION_LOG_END();
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
const VariantList *optionValue = cfgOptionIdxInternal(optionId, optionIdx, cfgOptDataTypeList, true)->value.list;
@ -890,7 +898,7 @@ cfgOptionIdxSet(
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -985,7 +993,7 @@ cfgOptionIdxSource(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT((!group && optionIdx == 0) || (group && optionIdx < indexTotal));
@ -1013,7 +1021,7 @@ cfgOptionIdxTest(const ConfigOption optionId, const unsigned int optionIdx)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
ASSERT_DECLARE(const bool group = configLocal->option[optionId].group);
ASSERT_DECLARE(const unsigned int indexTotal = configLocal->optionGroup[configLocal->option[optionId].groupId].indexTotal);
ASSERT(!cfgOptionValid(optionId) || ((!group && optionIdx == 0) || (group && optionIdx < indexTotal)));
@ -1030,7 +1038,7 @@ cfgOptionValid(const ConfigOption optionId)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
FUNCTION_TEST_RETURN(BOOL, configLocal->option[optionId].valid);
}
@ -1043,7 +1051,7 @@ cfgOptionInvalidate(const ConfigOption optionId)
FUNCTION_TEST_END();
ASSERT(optionId < CFG_OPTION_TOTAL);
ASSERT(configLocal != NULL);
ASSERT(cfgInited());
configLocal->option[optionId].valid = false;

View File

@ -88,6 +88,9 @@ Init Function
// Init with new configuration
FN_EXTERN void cfgInit(Config *config);
// Has the configuration been initialized?
FN_EXTERN bool cfgInited(void);
/***********************************************************************************************************************************
Command Functions
***********************************************************************************************************************************/

View File

@ -526,9 +526,6 @@ cfgLoad(const unsigned int argListSize, const char *argList[])
if (cfgOptionValid(cfgOptNeutralUmask) && cfgOptionBool(cfgOptNeutralUmask))
umask(0000);
// If a command is set
if (cfgCommand() != cfgCmdNone)
{
// Initialize TCP settings
if (cfgOptionValid(cfgOptSckKeepAlive))
{
@ -540,8 +537,8 @@ cfgLoad(const unsigned int argListSize, const char *argList[])
cfgOptionTest(cfgOptTcpKeepAliveInterval) ? cfgOptionInt(cfgOptTcpKeepAliveInterval) : 0);
}
// Set IO buffer size
if (cfgOptionValid(cfgOptBufferSize))
// Set IO buffer size (use the default for help to lower memory usage)
if (cfgOptionValid(cfgOptBufferSize) && !cfgCommandHelp())
ioBufferSizeSet(cfgOptionUInt(cfgOptBufferSize));
// Set IO timeout
@ -577,7 +574,6 @@ cfgLoad(const unsigned int argListSize, const char *argList[])
// Update options that have complex rules
cfgLoadUpdateOption();
}
}
MEM_CONTEXT_TEMP_END();
FUNCTION_LOG_RETURN_VOID();

View File

@ -2432,6 +2432,30 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
), // opt/force
), // opt/force
// -----------------------------------------------------------------------------------------------------------------------------
PARSE_RULE_OPTION // opt/help
( // opt/help
PARSE_RULE_OPTION_NAME("help"), // opt/help
PARSE_RULE_OPTION_TYPE(Boolean), // opt/help
PARSE_RULE_OPTION_REQUIRED(true), // opt/help
PARSE_RULE_OPTION_SECTION(CommandLine), // opt/help
// opt/help
PARSE_RULE_OPTION_COMMAND_ROLE_MAIN_VALID_LIST // opt/help
( // opt/help
PARSE_RULE_OPTION_COMMAND(Help) // opt/help
), // opt/help
// opt/help
PARSE_RULE_OPTIONAL // opt/help
( // opt/help
PARSE_RULE_OPTIONAL_GROUP // opt/help
( // opt/help
PARSE_RULE_OPTIONAL_DEFAULT // opt/help
( // opt/help
PARSE_RULE_VAL_BOOL_FALSE, // opt/help
), // opt/help
), // opt/help
), // opt/help
), // opt/help
// -----------------------------------------------------------------------------------------------------------------------------
PARSE_RULE_OPTION // opt/ignore-missing
( // opt/ignore-missing
PARSE_RULE_OPTION_NAME("ignore-missing"), // opt/ignore-missing
@ -10650,6 +10674,30 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
), // opt/verbose
), // opt/verbose
), // opt/verbose
// -----------------------------------------------------------------------------------------------------------------------------
PARSE_RULE_OPTION // opt/version
( // opt/version
PARSE_RULE_OPTION_NAME("version"), // opt/version
PARSE_RULE_OPTION_TYPE(Boolean), // opt/version
PARSE_RULE_OPTION_REQUIRED(true), // opt/version
PARSE_RULE_OPTION_SECTION(CommandLine), // opt/version
// opt/version
PARSE_RULE_OPTION_COMMAND_ROLE_MAIN_VALID_LIST // opt/version
( // opt/version
PARSE_RULE_OPTION_COMMAND(Help) // opt/version
), // opt/version
// opt/version
PARSE_RULE_OPTIONAL // opt/version
( // opt/version
PARSE_RULE_OPTIONAL_GROUP // opt/version
( // opt/version
PARSE_RULE_OPTIONAL_DEFAULT // opt/version
( // opt/version
PARSE_RULE_VAL_BOOL_FALSE, // opt/version
), // opt/version
), // opt/version
), // opt/version
), // opt/version
};
/***********************************************************************************************************************************
@ -10943,6 +10991,7 @@ static const uint8_t optionResolveOrder[] =
cfgOptExecId, // opt-resolve-order
cfgOptExpireAuto, // opt-resolve-order
cfgOptFilter, // opt-resolve-order
cfgOptHelp, // opt-resolve-order
cfgOptIgnoreMissing, // opt-resolve-order
cfgOptIoTimeout, // opt-resolve-order
cfgOptJobRetry, // opt-resolve-order
@ -11013,6 +11062,7 @@ static const uint8_t optionResolveOrder[] =
cfgOptTlsServerPort, // opt-resolve-order
cfgOptType, // opt-resolve-order
cfgOptVerbose, // opt-resolve-order
cfgOptVersion, // opt-resolve-order
cfgOptArchiveCheck, // opt-resolve-order
cfgOptArchiveCopy, // opt-resolve-order
cfgOptArchiveModeCheck, // opt-resolve-order

View File

@ -407,7 +407,7 @@ cfgParseCommandName(const ConfigCommand commandId)
FUNCTION_TEST_PARAM(ENUM, commandId);
FUNCTION_TEST_END();
ASSERT(commandId < cfgCmdNone);
ASSERT(commandId < CFG_COMMAND_TOTAL);
FUNCTION_TEST_RETURN_CONST(STRINGZ, parseRuleCommand[commandId].name);
}
@ -1433,8 +1433,8 @@ cfgFileLoad(
bool loadConfig = true;
bool loadConfigInclude = true;
// If the option is specified on the command line, then found will be true meaning the file is required to exist,
// else it is optional
// If the option is specified on the command line, then found will be true meaning the file is required to exist, else it is
// optional
const bool configFound = optionList[cfgOptConfig].indexList != NULL && optionList[cfgOptConfig].indexList[0].found;
bool configRequired = configFound;
const bool configPathRequired =
@ -1621,7 +1621,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
*config = (Config)
{
.memContext = MEM_CONTEXT_NEW(),
.command = cfgCmdNone,
.command = cfgCmdHelp,
.exe = strNewZ(argList[0]),
};
}
@ -1634,9 +1634,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
// Only the first non-option parameter should be treated as a command so track if the command has been set
bool commandSet = false;
unsigned int argIgnore = 0;
bool help = false;
bool version = false;
bool commandHelpSet = false;
for (unsigned int argListIdx = 1; argListIdx < argListSize; argListIdx++)
{
@ -1670,25 +1668,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
CfgParseOptionResult option = cfgParseOptionP(optionName, .prefixMatch = true);
if (!option.found)
{
// Check for --help shortcut to the help command
if (strcmp(arg, CFGCMD_HELP) == 0)
{
help = true;
argIgnore++;
continue;
}
// Else check for --version shortcut to the version command
else if (strcmp(arg, CFGCMD_VERSION) == 0)
{
version = true;
argIgnore++;
continue;
}
// Else invalid option
else
THROW_FMT(OptionInvalidError, "invalid option '--%s'", arg);
}
// If the option may have an argument (arguments are optional for boolean options)
if (!option.negate && !option.reset)
@ -1820,10 +1800,9 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
config->command = cfgParseCommandId(arg);
// Set help if help command
if (config->command == cfgCmdHelp && !config->help)
if (config->command == cfgCmdHelp && !commandHelpSet)
{
config->command = cfgCmdNone;
config->help = true;
commandHelpSet = true;
}
// Else parse command role if appended
else
@ -1840,9 +1819,10 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
THROW_FMT(CommandInvalidError, "invalid command/role combination '%s'", arg);
// Error if help requested and role is not main
if (config->help && config->commandRole != cfgCmdRoleMain)
if (commandHelpSet && config->commandRole != cfgCmdRoleMain)
THROW_FMT(CommandInvalidError, "help is not available for the '%s' role", colonPtr + 1);
config->help = commandHelpSet;
commandSet = true;
}
}
@ -1864,33 +1844,25 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
}
// Handle command not found
if (!commandSet && !config->help)
if (!commandSet && !commandHelpSet)
{
// If there are args then error
if (argListSize - argIgnore > 1)
THROW_FMT(CommandRequiredError, "no command found");
ASSERT(config->command == cfgCmdHelp);
// Output help if --help specified or --version not specified
if (help || !version)
for (unsigned int optionIdx = 0; optionIdx < CFG_OPTION_TOTAL; optionIdx++)
{
config->help = true;
if (parseOptionList[optionIdx].indexListTotal != 0 && !cfgParseOptionValid(cfgCmdHelp, cfgCmdRoleMain, optionIdx))
THROW_FMT(CommandRequiredError, "no command found");
}
// Else output version
else
config->command = cfgCmdVersion;
}
// Set command options
const ParseRuleCommand *const ruleCommand = &parseRuleCommand[config->command];
if (config->command != cfgCmdNone)
{
config->lockRequired = ruleCommand->lockRequired;
config->lockRemoteRequired = ruleCommand->lockRemoteRequired;
config->lockType = (LockType)ruleCommand->lockType;
config->logFile = ruleCommand->logFile;
config->logLevelDefault = (LogLevel)ruleCommand->logLevelDefault;
}
// Error when parameters found but the command does not allow parameters
if (config->paramList != NULL && !config->help && !ruleCommand->parameterAllowed)
@ -1900,16 +1872,8 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
if (!param.noResetLogLevel && config->commandRole == cfgCmdRoleMain)
logInit(logLevelWarn, logLevelOff, logLevelOff, false, 0, 1, false);
// Only continue if command options need to be validated, i.e. a real command is running or we are getting help for a
// specific command and would like to display actual option values in the help.
if (config->command != cfgCmdNone && config->command != cfgCmdVersion && config->command != cfgCmdHelp)
{
// Error if --help or --version passed to command
if (help || version)
THROW_FMT(OptionInvalidError, "invalid option '--%s'", help ? CFGCMD_HELP : CFGCMD_VERSION);
// Phase 2: parse environment variables
// ---------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------
unsigned int environIdx = 0;
// Loop through all environment variables and look for our env vars by matching the prefix
@ -1991,15 +1955,14 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
}
// Phase 3: parse config file unless --no-config passed
// ---------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------
// Load the configuration file(s)
if (!param.noConfigLoad)
{
const String *const configString = cfgFileLoad(
storage, parseOptionList,
(const String *)&parseRuleValueStr[parseRuleValStrCFGOPTDEF_CONFIG_PATH_SP_QT_FS_QT_SP_PROJECT_CONFIG_FILE],
(const String *)&parseRuleValueStr[
parseRuleValStrCFGOPTDEF_CONFIG_PATH_SP_QT_FS_QT_SP_PROJECT_CONFIG_INCLUDE_PATH],
(const String *)&parseRuleValueStr[parseRuleValStrCFGOPTDEF_CONFIG_PATH_SP_QT_FS_QT_SP_PROJECT_CONFIG_INCLUDE_PATH],
PGBACKREST_CONFIG_ORIG_PATH_FILE_STR);
iniFree(configParseLocal.ini);
@ -2097,9 +2060,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
if (sectionIdx % 2 == 0)
{
LOG_WARN_FMT(
"configuration file contains option '%s' invalid for section '%s'", strZ(key),
strZ(section));
continue;
"configuration file contains option '%s' invalid for section '%s'", strZ(key), strZ(section));
}
continue;
@ -2110,8 +2071,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
(strEqZ(section, CFGDEF_SECTION_GLOBAL) || strBeginsWithZ(section, CFGDEF_SECTION_GLOBAL ":")))
{
LOG_WARN_FMT(
"configuration file contains stanza-only option '%s' in global section '%s'", strZ(key),
strZ(section));
"configuration file contains stanza-only option '%s' in global section '%s'", strZ(key), strZ(section));
continue;
}
@ -2145,8 +2105,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
if (strSize(value) == 0)
{
THROW_FMT(
OptionInvalidValueError, "section '%s', key '%s' must have a value", strZ(section),
strZ(key));
OptionInvalidValueError, "section '%s', key '%s' must have a value", strZ(section), strZ(key));
}
if (cfgParseOptionType(option.id) == cfgOptTypeBoolean)
@ -2168,7 +2127,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
}
// Phase 4: create the config and resolve indexed options for each group
// ---------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------
// Determine how many indexes are used in each group
bool groupIdxMap[CFG_OPTION_GROUP_TOTAL][CFG_OPTION_KEY_MAX] = {{0}};
@ -2207,8 +2166,8 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
config->optionGroup[groupId].valid = true;
// Scan the option values to determine which indexes are in use. Store them in a map that will later be scanned
// to create a list of just the used indexes.
// Scan the option values to determine which indexes are in use. Store them in a map that will later be scanned to
// create a list of just the used indexes.
for (unsigned int optionKeyIdx = 0; optionKeyIdx < parseOptionList[optionId].indexListTotal; optionKeyIdx++)
{
if (parseOptionList[optionId].indexList[optionKeyIdx].found &&
@ -2235,17 +2194,17 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
continue;
// Allocate memory for the index to key index map
const unsigned int optionIdxTotal = config->optionGroup[groupId].indexTotal;
MEM_CONTEXT_BEGIN(config->memContext)
{
config->optionGroup[groupId].indexMap = memNew(
sizeof(unsigned int) *
(config->optionGroup[groupId].indexTotal == 0 ? 1 : config->optionGroup[groupId].indexTotal));
config->optionGroup[groupId].indexMap = memNew(sizeof(unsigned int) * (optionIdxTotal == 0 ? 1 : optionIdxTotal));
}
MEM_CONTEXT_END();
// If no values were found in any index then use index 0 since all valid groups must have at least one index. This
// may lead to an error unless all options in the group have defaults but that will be resolved later.
if (config->optionGroup[groupId].indexTotal == 0)
// If no values were found in any index then use index 0 since all valid groups must have at least one index. This may
// lead to an error unless all options in the group have defaults but that will be resolved later.
if (optionIdxTotal == 0)
{
config->optionGroup[groupId].indexTotal = 1;
config->optionGroup[groupId].indexMap[0] = 0;
@ -2279,7 +2238,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
}
// Phase 5: validate option definitions and load into configuration
// ---------------------------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------------------------------
// Determine whether a group index will be kept based on non-default values
bool optionGroupIndexKeep[CFG_OPTION_GROUP_TOTAL][CFG_OPTION_KEY_MAX] = {{false}};
@ -2313,12 +2272,12 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
const unsigned optionKeyIdx = optionGroup ? config->optionGroup[optionGroupId].indexMap[optionListIdx] : 0;
// Get the parsed value using the key index. Provide a default structure when the value was not found.
ParseOptionValue *parseOptionValue =
const ParseOptionValue *const parseOptionValue =
optionKeyIdx < parseOptionList[optionId].indexListTotal ?
&parseOptionList[optionId].indexList[optionKeyIdx] : &(ParseOptionValue){0};
// Get the location where the value will be stored in the configuration
ConfigOptionValue *configOptionValue = &config->option[optionId].index[optionListIdx];
ConfigOptionValue *const configOptionValue = &config->option[optionId].index[optionListIdx];
// Is the value set for this option?
const bool optionSet =
@ -2337,10 +2296,10 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
PackRead *const filter = pckReadNewC(optionalRules.valid, optionalRules.validSize);
dependResult = cfgParseOptionalFilterDepend(filter, config, optionListIdx);
// If depend not resolved and option value is set on the command-line then error. It is OK to have
// unresolved options in the config file because they may be there for another command. For instance,
// spool-path is only loaded for the archive-push command when archive-async=y, and the presence of
// spool-path in the config file should not cause an error here, it will just end up null.
// If depend not resolved and option value is set on the command-line then error. It is OK to have unresolved
// options in the config file because they may be there for another command. For instance, spool-path is only
// loaded for the archive-push command when archive-async=y, and the presence of spool-path in the config file
// should not cause an error here, it will just end up null.
if (!dependResult.valid && optionSet && parseOptionValue->source == cfgSourceParam)
{
PackRead *const filter = pckReadNewC(optionalRules.valid, optionalRules.validSize);
@ -2474,8 +2433,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
MEM_CONTEXT_END();
// If a numeric type check that the value is valid
if (optionType == cfgOptTypeInteger || optionType == cfgOptTypeSize ||
optionType == cfgOptTypeTime)
if (optionType == cfgOptTypeInteger || optionType == cfgOptTypeSize || optionType == cfgOptTypeTime)
{
// Check that the value can be converted
TRY_BEGIN()
@ -2577,8 +2535,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
}
// If the option has an allow list then check it
if (cfgParseOptionalRule(
&optionalRules, parseRuleOptionalTypeAllowList, config->command, optionId))
if (cfgParseOptionalRule(&optionalRules, parseRuleOptionalTypeAllowList, config->command, optionId))
{
PackRead *const allowList = pckReadNewC(optionalRules.allowList, optionalRules.allowListSize);
bool allowListFound = false;
@ -2617,8 +2574,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
if (!allowListFound)
{
PackRead *const allowList = pckReadNewC(
optionalRules.allowList, optionalRules.allowListSize);
PackRead *const allowList = pckReadNewC(optionalRules.allowList, optionalRules.allowListSize);
String *const hintList = strNew();
while (!pckReadNullP(allowList))
@ -2640,8 +2596,11 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
}
}
}
// Else set source when negated (value is already false)
else if (parseOptionValue->negate)
{
configOptionValue->source = parseOptionValue->source;
}
// Else try to set a default
else
{
@ -2649,8 +2608,7 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
MEM_CONTEXT_BEGIN(config->memContext)
{
found = cfgParseOptionalRule(
&optionalRules, parseRuleOptionalTypeDefault, config->command, optionId);
found = cfgParseOptionalRule(&optionalRules, parseRuleOptionalTypeDefault, config->command, optionId);
}
MEM_CONTEXT_END();
@ -2664,16 +2622,14 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
else
{
const bool required =
cfgParseOptionalRule(
&optionalRules, parseRuleOptionalTypeRequired, config->command, optionId) ?
cfgParseOptionalRule(&optionalRules, parseRuleOptionalTypeRequired, config->command, optionId) ?
optionalRules.required : ruleOption->required;
if (required && !config->help)
{
THROW_FMT(
OptionRequiredError, "%s command requires option: %s%s",
cfgParseCommandName(config->command),
cfgParseOptionKeyIdxName(optionId, optionKeyIdx),
cfgParseCommandName(config->command), cfgParseOptionKeyIdxName(optionId, optionKeyIdx),
ruleOption->section == cfgSectionStanza ? "\nHINT: does this stanza exist?" : "");
}
}
@ -2696,10 +2652,10 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
// Phase 6: Remove any group indexes that have all default values (unless there is only one)
//
// It is possible that a group index was created for an option that was later found to not meet dependencies. In this
// case all values will be default leading to a phantom group, which can be quite confusing. Remove all group indexes
// that are all default (except the final one) and make sure the key for the final all default group index is 1.
// ---------------------------------------------------------------------------------------------------------------------
// It is possible that a group index was created for an option that was later found to not meet dependencies. In this case
// all values will be default leading to a phantom group, which can be quite confusing. Remove all group indexes that are
// all default (except the final one) and make sure the key for the final all default group index is 1.
// -------------------------------------------------------------------------------------------------------------------------
for (unsigned int optionGroupIdx = 0; optionGroupIdx < CFG_OPTION_GROUP_TOTAL; optionGroupIdx++)
{
ConfigOptionGroupData *const optionGroup = &config->optionGroup[optionGroupIdx];
@ -2742,12 +2698,11 @@ cfgParse(const Storage *const storage, const unsigned int argListSize, const cha
}
}
// If the remaining index contains all default values and is not key 1 then make it key 1. This prevents the key
// from being determined by the key of an unused option.
// If the remaining index contains all default values and is not key 1 then make it key 1. This prevents the key from
// being determined by the key of an unused option.
if (optionGroup->indexTotal == 1 && !optionGroupIndexKeep[optionGroupIdx][0] && optionGroup->indexMap[0] != 0)
optionGroup->indexMap[0] = 0;
}
}
// Initialize config
cfgInit(config);

View File

@ -175,12 +175,6 @@ main(int argListSize, const char *argList[])
cmdExpire();
break;
// Help command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp:
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Info command
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdInfo:
@ -271,17 +265,12 @@ main(int argListSize, const char *argList[])
cmdVerify();
break;
// Display version
// Help/version commands
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp:
case cfgCmdVersion:
printf(PROJECT_NAME " " PROJECT_VERSION "\n");
fflush(stdout);
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Error on commands that should have been handled
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdNone:
THROW(AssertError, "'none' command should have been handled");
}
}
// Local/remote commands

View File

@ -65,6 +65,14 @@ option:
command:
test: {}
help:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
neutral-umask:
type: boolean
internal: true
@ -123,6 +131,14 @@ option:
command:
test: {}
version:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
vm:
type: string
internal: true

View File

@ -230,6 +230,28 @@
<text>
<p>Three levels of help are provided. If no command is specified then general help will be displayed. If a command is specified (e.g. <cmd>test-pgbackrest help test</cmd>) then a full description of the command will be displayed along with a list of valid options. If an option is specified in addition to a command (e.g. <cmd>pgbackrest help test vm</cmd>) then a full description of the option as it applies to the command will be displayed.</p>
</text>
<option-list>
<option id="help" name="Display Help">
<summary>Display help.</summary>
<text>
<p>Displays help even if the <cmd>help</cmd> command is not specified and overrides the <br-option>--version</br-option> option.</p>
</text>
<example>y</example>
</option>
<option id="version" name="Display Version">
<summary>Display version.</summary>
<text>
<p>Displays version even if the <cmd>version</cmd> or <cmd>help</cmd> command is not specified.</p>
</text>
<example>y</example>
</option>
</option-list>
</command>
<!-- Noop command holds options that must be defined but we don't need -->

View File

@ -65,7 +65,7 @@ hrnCfgLoad(ConfigCommand commandId, const StringList *argListParam, const HrnCfg
}
// Insert the command so it does not interfere with parameters
if (commandId != cfgCmdNone)
if (commandId < CFG_COMMAND_TOTAL)
strLstInsert(argList, 0, cfgParseCommandRoleName(commandId, param.role));
// Insert the project exe

View File

@ -126,7 +126,7 @@ protocolLocalExec(
// Load configuration
StringList *const paramList = protocolLocalParam(protocolStorageType, hostIdx, processId);
hrnCfgLoadP(cfgCmdNone, paramList, .noStd = true);
hrnCfgLoadP(CFG_COMMAND_TOTAL, paramList, .noStd = true);
// Change log process id to aid in debugging
hrnLogProcessIdSet(processId);
@ -224,7 +224,7 @@ protocolRemoteExec(
// Load configuration
StringList *const paramList = protocolRemoteParam(protocolStorageType, hostIdx);
hrnCfgLoadP(cfgCmdNone, paramList, .noStd = true);
hrnCfgLoadP(CFG_COMMAND_TOTAL, paramList, .noStd = true);
// Change log process id to aid in debugging
hrnLogProcessIdSet(processId);

View File

@ -48,16 +48,28 @@ cfgLoadUpdateOption(void)
THROW_ON_SYS_ERROR(getcwd(currentWorkDir, sizeof(currentWorkDir)) == NULL, FormatError, "unable to get cwd");
// If repo-path is relative then make it absolute
if (cfgOptionValid(cfgOptRepoPath))
{
const String *const repoPath = cfgOptionStr(cfgOptRepoPath);
if (!strBeginsWithZ(repoPath, "/"))
cfgOptionSet(cfgOptRepoPath, cfgOptionSource(cfgOptRepoPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(repoPath))));
{
cfgOptionSet(
cfgOptRepoPath, cfgOptionSource(cfgOptRepoPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(repoPath))));
}
}
// If test-path is relative then make it absolute
if (cfgOptionValid(cfgOptTestPath))
{
const String *const testPath = cfgOptionStr(cfgOptTestPath);
if (!strBeginsWithZ(testPath, "/"))
cfgOptionSet(cfgOptTestPath, cfgOptionSource(cfgOptTestPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(testPath))));
{
cfgOptionSet(
cfgOptTestPath, cfgOptionSource(cfgOptTestPath), VARSTR(strNewFmt("%s/%s", currentWorkDir, strZ(testPath))));
}
}
FUNCTION_LOG_RETURN_VOID();
}
@ -95,11 +107,7 @@ cfgLoad(unsigned int argListSize, const char *argList[])
if (cfgCommand() == cfgCmdNoop)
THROW(CommandInvalidError, "invalid command '" CFGCMD_NOOP "'");
// If a command is set
if (cfgCommand() != cfgCmdNone && cfgCommand() != cfgCmdHelp && cfgCommand() != cfgCmdVersion)
{
// Load the log settings
if (!cfgCommandHelp())
cfgLoadLogSetting();
// Neutralize the umask to make the repository file/path modes more consistent
@ -116,7 +124,6 @@ cfgLoad(unsigned int argListSize, const char *argList[])
// Begin the command
cmdBegin();
}
}
MEM_CONTEXT_TEMP_END();
FUNCTION_LOG_RETURN_VOID();

View File

@ -86,22 +86,15 @@ main(int argListSize, const char *argList[])
break;
}
// Help
// Help/version
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdHelp:
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Version
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdVersion:
printf(PROJECT_NAME " Test " PROJECT_VERSION "\n");
fflush(stdout);
cmdHelp(BUF(helpData, sizeof(helpData)));
break;
// Error on commands that should have been handled
// -----------------------------------------------------------------------------------------------------------------
case cfgCmdNone:
case cfgCmdNoop:
THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName());
break;

View File

@ -525,7 +525,6 @@ testRun(void)
" cfgCmdBackup,\n"
" cfgCmdHelp,\n"
" cfgCmdVersion,\n"
" cfgCmdNone,\n"
"} ConfigCommand;\n"
"\n"
COMMENT_BLOCK_BEGIN "\n"

View File

@ -29,6 +29,7 @@ testRun(void)
// *****************************************************************************************************************************
if (testBegin("exitInit() and exitOnSignal()"))
{
TEST_RESULT_INT(exitSafe(0, false, signalTypeNone), 0, "exit with no command");
HRN_CFG_LOAD(cfgCmdHelp, strLstNew());
HRN_FORK_BEGIN()
@ -46,11 +47,6 @@ testRun(void)
// *****************************************************************************************************************************
if (testBegin("exitSafe()"))
{
HRN_CFG_LOAD(cfgCmdHelp, strLstNew());
cfgCommandSet(cfgCmdNone, cfgCmdRoleMain);
TEST_RESULT_INT(exitSafe(0, false, signalTypeNone), 0, "exit with no command");
// -------------------------------------------------------------------------------------------------------------------------
StringList *argList = strLstNew();
hrnCfgArgRawZ(argList, cfgOptStanza, "test");

View File

@ -136,6 +136,23 @@ testRun(void)
{
StringList *argList = NULL;
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("version");
const char *versionOnly = zNewFmt("%s\n", helpVersion);
argList = strLstNew();
strLstAddZ(argList, "/path/to/pgbackrest");
strLstAddZ(argList, "version");
TEST_RESULT_VOID(testCfgLoad(argList), "version from version command");
TEST_RESULT_STR_Z(helpRender(helpData), versionOnly, "check text");
argList = strLstNew();
strLstAddZ(argList, "/path/to/pgbackrest");
strLstAddZ(argList, "--version");
TEST_RESULT_VOID(testCfgLoad(argList), "version from version option");
TEST_RESULT_STR_Z(helpRender(helpData), versionOnly, "check text");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("general invocation");
@ -150,6 +167,13 @@ testRun(void)
TEST_RESULT_VOID(testCfgLoad(argList), "help from help command");
TEST_RESULT_STR_Z(helpRender(helpData), generalHelp, "check text");
argList = strLstNew();
strLstAddZ(argList, "/path/to/pgbackrest");
strLstAddZ(argList, "--version");
strLstAddZ(argList, "--help");
TEST_RESULT_VOID(testCfgLoad(argList), "help from help option");
TEST_RESULT_STR_Z(helpRender(helpData), generalHelp, "check text");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("version command");

View File

@ -883,8 +883,10 @@ testRun(void)
hrnCfgArgRawZ(argList, cfgOptLogLevelFile, "off");
hrnCfgArgRawZ(argList, cfgOptRepoRetentionFull, "2");
ioBufferSizeSet(333);
TEST_RESULT_VOID(cfgLoad(strLstSize(argList), strLstPtr(argList)), "help command for backup");
TEST_RESULT_UINT(ioBufferSize(), 1048576, "buffer size set to option default");
TEST_RESULT_UINT(ioBufferSize(), 333, "buffer size not updated by help command");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("help command for help");
@ -894,8 +896,10 @@ testRun(void)
strLstAddZ(argList, "help");
strLstAddZ(argList, "help");
ioBufferSizeSet(333);
TEST_RESULT_VOID(cfgLoad(strLstSize(argList), strLstPtr(argList)), "load config");
TEST_RESULT_UINT(ioBufferSize(), 1048576, "buffer size set to option default");
TEST_RESULT_UINT(ioBufferSize(), 333, "buffer size not updated by help command");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("command takes lock and opens log file and uses custom tcp settings");

View File

@ -82,12 +82,12 @@ testRun(void)
// Config functions that are not tested with parse
// *****************************************************************************************************************************
if (testBegin("cfg*()"))
if (testBegin("cfgInited()"))
{
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("config command defaults to none before cfgInit()");
TEST_RESULT_UINT(cfgCommand(), cfgCmdNone, "command is none");
TEST_RESULT_BOOL(cfgInited(), false, "config is not inited");
}
// config and config-include-path options
@ -1547,8 +1547,8 @@ testRun(void)
hrnLogLevelStdOutSet(logLevelOff);
hrnLogLevelStdErrSet(logLevelOff);
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList)), "no command");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, "command is none");
TEST_RESULT_BOOL(cfgCommandHelp(), false, "help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
TEST_RESULT_INT(hrnLogLevelStdOut(), logLevelWarn, "console logging is warn");
TEST_RESULT_INT(hrnLogLevelStdErr(), logLevelOff, "stderr logging is off");
harnessLogLevelReset();
@ -1561,8 +1561,8 @@ testRun(void)
strLstAddZ(argList, "help");
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "help command");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, "command is help");
TEST_RESULT_BOOL(cfgCommandHelp(), false, "command help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
argList = strLstNew();
strLstAddZ(argList, TEST_BACKREST_EXE);
@ -1571,7 +1571,7 @@ testRun(void)
TEST_RESULT_VOID(
cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "help for version command");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "command help is set");
TEST_RESULT_INT(cfgCommand(), cfgCmdVersion, "command is version");
TEST_RESULT_Z(cfgCommandName(), "version", "command name is version");
@ -1582,7 +1582,7 @@ testRun(void)
TEST_RESULT_VOID(
cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "command help is set");
TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
// -------------------------------------------------------------------------------------------------------------------------
@ -1593,8 +1593,9 @@ testRun(void)
strLstAddZ(argList, "--help");
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is set");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, "command is none");
TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
TEST_RESULT_BOOL(cfgCommandHelp(), false, "command help is not set");
TEST_RESULT_BOOL(cfgOptionBool(cfgOptHelp), true, "help option is set");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("version option");
@ -1604,8 +1605,9 @@ testRun(void)
strLstAddZ(argList, "--version");
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config");
TEST_RESULT_BOOL(cfgCommandHelp(), false, "help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdVersion, "command is version");
TEST_RESULT_BOOL(cfgCommandHelp(), false, "command help is not set");
TEST_RESULT_UINT(cfgCommand(), cfgCmdHelp, "command is help");
TEST_RESULT_BOOL(cfgOptionBool(cfgOptVersion), true, "version option is set");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("help and version options");
@ -1616,8 +1618,10 @@ testRun(void)
strLstAddZ(argList, "--version");
TEST_RESULT_VOID(cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), "load config");
TEST_RESULT_BOOL(cfgCommandHelp(), true, "help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdNone, "command is none");
TEST_RESULT_BOOL(cfgCommandHelp(), false, "help is not set");
TEST_RESULT_INT(cfgCommand(), cfgCmdHelp, "command is help");
TEST_RESULT_BOOL(cfgOptionBool(cfgOptHelp), true, "version option is set");
TEST_RESULT_BOOL(cfgOptionBool(cfgOptVersion), true, "version option is set");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("error on command with --help option");
@ -1629,7 +1633,7 @@ testRun(void)
TEST_ERROR(
cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), OptionInvalidError,
"invalid option '--help'");
"option 'help' not valid for command 'backup'");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("error on command with --version option");
@ -1641,7 +1645,7 @@ testRun(void)
TEST_ERROR(
cfgParseP(storageTest, strLstSize(argList), strLstPtr(argList), .noResetLogLevel = true), OptionInvalidError,
"invalid option '--version'");
"option 'version' not valid for command 'backup'");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("help command - should not fail on missing options");