1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-18 04:58:51 +02:00

Add error parameter to cfgCommandId().

This allows commands to be checked for validity without generating an error.
This commit is contained in:
David Steele 2019-12-28 13:37:03 -07:00
parent d28837a300
commit e72a9dd0d2
8 changed files with 23 additions and 23 deletions

View File

@ -18,7 +18,7 @@ CODE:
MEM_CONTEXT_XS_TEMP_BEGIN()
{
// Set the command so we can get the correct lock type to use
cfgCommandSet(cfgCommandId(command));
cfgCommandSet(cfgCommandId(command, true));
// Attempt to acquire the lock
if (cfgLockRequired())

View File

@ -12,7 +12,7 @@ CODE:
ERROR_XS_BEGIN()
{
RETVAL = cfgCommandId(commandName);
RETVAL = cfgCommandId(commandName, true);
}
ERROR_XS_END();
OUTPUT:

View File

@ -50,7 +50,7 @@ cmdRemote(int handleRead, int handleWrite)
// Only try the lock if this is process 0, i.e. the remote started from the main process
if (cfgOptionUInt(cfgOptProcess) == 0)
{
ConfigCommand commandId = cfgCommandId(strPtr(cfgOptionStr(cfgOptCommand)));
ConfigCommand commandId = cfgCommandId(strPtr(cfgOptionStr(cfgOptCommand)), true);
// Acquire a lock if this command requires a lock
if (cfgLockRemoteRequired(commandId))

View File

@ -226,14 +226,13 @@ cfgCommandDefIdFromId(ConfigCommand commandId)
FUNCTION_TEST_RETURN((ConfigDefineCommand)commandId);
}
/***********************************************************************************************************************************
Get command id by name
***********************************************************************************************************************************/
/**********************************************************************************************************************************/
ConfigCommand
cfgCommandId(const char *commandName)
cfgCommandId(const char *commandName, bool error)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRINGZ, commandName);
FUNCTION_TEST_PARAM(BOOL, error);
FUNCTION_TEST_END();
ASSERT(commandName != NULL);
@ -244,7 +243,7 @@ cfgCommandId(const char *commandName)
if (strcmp(commandName, configCommandData[commandId].name) == 0)
break;
if (commandId == cfgCmdNone)
if (commandId == cfgCmdNone && error)
THROW_FMT(AssertError, "invalid command '%s'", commandName);
FUNCTION_TEST_RETURN(commandId);

View File

@ -93,7 +93,10 @@ void cfgInit(void);
ConfigDefineCommand cfgCommandDefIdFromId(ConfigCommand commandId);
bool cfgCommandHelp(void);
void cfgCommandHelpSet(bool helpParam);
ConfigCommand cfgCommandId(const char *commandName);
// Get command id by name. If error is true then assert when the command does not exist.
ConfigCommand cfgCommandId(const char *commandName, bool error);
void cfgCommandParamSet(const StringList *param);
void cfgCommandSet(ConfigCommand commandParam);

View File

@ -462,17 +462,14 @@ configParse(unsigned int argListSize, const char *argList[], bool resetLogLevel)
if (!commandSet)
{
// Try getting the command from the valid command list
TRY_BEGIN()
{
cfgCommandSet(cfgCommandId(argList[optind - 1]));
}
// Assert error means the command does not exist, which is correct for all usages but this one (since we
// don't have any control over what the user passes), so modify the error code and message.
CATCH(AssertError)
{
ConfigCommand commandId = cfgCommandId(argList[optind - 1], false);
// Error when command does not exist
if (commandId == cfgCmdNone)
THROW_FMT(CommandInvalidError, "invalid command '%s'", argList[optind - 1]);
}
TRY_END();
// Set the command
cfgCommandSet(commandId);
if (cfgCommand() == cfgCmdHelp)
cfgCommandHelpSet(true);

View File

@ -16,8 +16,9 @@ testRun(void)
// *****************************************************************************************************************************
if (testBegin("check known values"))
{
TEST_ERROR(cfgCommandId(BOGUS_STR), AssertError, "invalid command 'BOGUS'");
TEST_RESULT_INT(cfgCommandId("archive-push"), cfgCmdArchivePush, "command id from name");
TEST_ERROR(cfgCommandId(BOGUS_STR, true), AssertError, "invalid command 'BOGUS'");
TEST_RESULT_INT(cfgCommandId(BOGUS_STR, false), cfgCmdNone, "command none id from bogus");
TEST_RESULT_INT(cfgCommandId("archive-push", true), cfgCmdArchivePush, "command id from name");
TEST_ERROR(
cfgCommandDefIdFromId(CFG_COMMAND_TOTAL), AssertError, "assertion 'commandId < cfgCmdNone' failed");

View File

@ -138,9 +138,9 @@ testRun(void)
// Set up defaults
String *backupCmdDefConfigValue = strNew(cfgDefOptionDefault(
cfgCommandDefIdFromId(cfgCommandId(TEST_COMMAND_BACKUP)), cfgOptionDefIdFromId(cfgOptConfig)));
cfgCommandDefIdFromId(cfgCommandId(TEST_COMMAND_BACKUP, true)), cfgOptionDefIdFromId(cfgOptConfig)));
String *backupCmdDefConfigInclPathValue = strNew(cfgDefOptionDefault(
cfgCommandDefIdFromId(cfgCommandId(TEST_COMMAND_BACKUP)), cfgOptionDefIdFromId(cfgOptConfigIncludePath)));
cfgCommandDefIdFromId(cfgCommandId(TEST_COMMAND_BACKUP, true)), cfgOptionDefIdFromId(cfgOptConfigIncludePath)));
String *oldConfigDefault = strNewFmt("%s%s", testPath(), PGBACKREST_CONFIG_ORIG_PATH_FILE);
// Create the option structure and initialize with 0