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 environ@GLIBC_2.2.5
main main
stderr@GLIBC_2.2.5 stderr@GLIBC_2.2.5
stdout@GLIBC_2.2.5
xmlFree@LIBXML2_2.4.30 xmlFree@LIBXML2_2.4.30

View File

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

View File

@ -98,6 +98,28 @@
<text> <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> <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> </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>
<!-- Noop command holds options that must be defined but we don't need --> <!-- 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"); THROW_ON_SYS_ERROR(getcwd(currentWorkDir, sizeof(currentWorkDir)) == NULL, FormatError, "unable to get cwd");
// If repo-path is relative then make it absolute // If repo-path is relative then make it absolute
if (cfgOptionValid(cfgOptRepoPath))
{
const String *const repoPath = cfgOptionStr(cfgOptRepoPath); const String *const repoPath = cfgOptionStr(cfgOptRepoPath);
if (!strBeginsWithZ(repoPath, "/")) 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(); FUNCTION_LOG_RETURN_VOID();
} }
@ -89,9 +95,6 @@ cfgLoad(unsigned int argListSize, const char *argList[])
if (cfgCommand() == cfgCmdNoop) if (cfgCommand() == cfgCmdNoop)
THROW(CommandInvalidError, "invalid command '" CFGCMD_NOOP "'"); THROW(CommandInvalidError, "invalid command '" CFGCMD_NOOP "'");
// If a command is set
if (cfgCommand() != cfgCmdNone && cfgCommand() != cfgCmdHelp && cfgCommand() != cfgCmdVersion)
{
// Load the log settings // Load the log settings
if (!cfgCommandHelp()) if (!cfgCommandHelp())
cfgLoadLogSetting(); cfgLoadLogSetting();
@ -110,7 +113,6 @@ cfgLoad(unsigned int argListSize, const char *argList[])
// Begin the command // Begin the command
cmdBegin(); cmdBegin();
} }
}
MEM_CONTEXT_TEMP_END(); MEM_CONTEXT_TEMP_END();
FUNCTION_LOG_RETURN_VOID(); FUNCTION_LOG_RETURN_VOID();

View File

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

View File

@ -300,6 +300,14 @@ option:
command-role: command-role:
main: {} main: {}
help:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
online: online:
type: boolean type: boolean
default: true default: true
@ -501,6 +509,14 @@ option:
command-role: command-role:
main: {} main: {}
version:
type: boolean
default: false
command:
help: {}
command-role:
main: {}
# Command-line only local/remote options # Command-line only local/remote options
#--------------------------------------------------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------------------------------------
exec-id: 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(cmd->name)));
} }
strCatFmt(config, " %s,\n", strZ(bldEnumCmd(STRDEF("none"))));
strCatZ( strCatZ(
config, config,
"} ConfigCommand;\n"); "} ConfigCommand;\n");

View File

@ -2572,6 +2572,28 @@
<text> <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> <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> </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>
<command id="server" name="Server"> <command id="server" name="Server">

View File

@ -163,7 +163,7 @@ cmdBegin(void)
{ {
FUNCTION_LOG_VOID(logLevelTrace); 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 // This is fairly expensive log message to generate so skip it if it won't be output
if (logAny(cfgLogLevelDefault())) if (logAny(cfgLogLevelDefault()))
@ -198,7 +198,7 @@ cmdEnd(const int code, const String *const errorMessage)
FUNCTION_LOG_PARAM(STRING, errorMessage); FUNCTION_LOG_PARAM(STRING, errorMessage);
FUNCTION_LOG_END(); 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. // 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())) if (logAny(cfgLogLevelDefault()))

View File

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

View File

@ -254,6 +254,16 @@ helpRender(const Buffer *const helpData)
String *const result = strCatZ(strNew(), PROJECT_NAME " " PROJECT_VERSION); 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() MEM_CONTEXT_TEMP_BEGIN()
{ {
// Set a small buffer size to minimize memory usage // Set a small buffer size to minimize memory usage
@ -287,7 +297,7 @@ helpRender(const Buffer *const helpData)
const String *more = NULL; const String *more = NULL;
// Display general help // Display general help
if (cfgCommand() == cfgCmdNone) if (!cfgCommandHelp())
{ {
strCatZ( strCatZ(
result, result,

View File

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

View File

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

View File

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

View File

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

View File

@ -2432,6 +2432,30 @@ static const ParseRuleOption parseRuleOption[CFG_OPTION_TOTAL] =
), // opt/force ), // opt/force
), // 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 PARSE_RULE_OPTION // opt/ignore-missing
( // opt/ignore-missing ( // opt/ignore-missing
PARSE_RULE_OPTION_NAME("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 ), // 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 cfgOptExecId, // opt-resolve-order
cfgOptExpireAuto, // opt-resolve-order cfgOptExpireAuto, // opt-resolve-order
cfgOptFilter, // opt-resolve-order cfgOptFilter, // opt-resolve-order
cfgOptHelp, // opt-resolve-order
cfgOptIgnoreMissing, // opt-resolve-order cfgOptIgnoreMissing, // opt-resolve-order
cfgOptIoTimeout, // opt-resolve-order cfgOptIoTimeout, // opt-resolve-order
cfgOptJobRetry, // opt-resolve-order cfgOptJobRetry, // opt-resolve-order
@ -11013,6 +11062,7 @@ static const uint8_t optionResolveOrder[] =
cfgOptTlsServerPort, // opt-resolve-order cfgOptTlsServerPort, // opt-resolve-order
cfgOptType, // opt-resolve-order cfgOptType, // opt-resolve-order
cfgOptVerbose, // opt-resolve-order cfgOptVerbose, // opt-resolve-order
cfgOptVersion, // opt-resolve-order
cfgOptArchiveCheck, // opt-resolve-order cfgOptArchiveCheck, // opt-resolve-order
cfgOptArchiveCopy, // opt-resolve-order cfgOptArchiveCopy, // opt-resolve-order
cfgOptArchiveModeCheck, // opt-resolve-order cfgOptArchiveModeCheck, // opt-resolve-order

View File

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

View File

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

View File

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

View File

@ -230,6 +230,28 @@
<text> <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> <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> </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>
<!-- Noop command holds options that must be defined but we don't need --> <!-- 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 // 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)); strLstInsert(argList, 0, cfgParseCommandRoleName(commandId, param.role));
// Insert the project exe // Insert the project exe

View File

@ -126,7 +126,7 @@ protocolLocalExec(
// Load configuration // Load configuration
StringList *const paramList = protocolLocalParam(protocolStorageType, hostIdx, processId); 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 // Change log process id to aid in debugging
hrnLogProcessIdSet(processId); hrnLogProcessIdSet(processId);
@ -224,7 +224,7 @@ protocolRemoteExec(
// Load configuration // Load configuration
StringList *const paramList = protocolRemoteParam(protocolStorageType, hostIdx); 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 // Change log process id to aid in debugging
hrnLogProcessIdSet(processId); hrnLogProcessIdSet(processId);

View File

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

View File

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

View File

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

View File

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

View File

@ -136,6 +136,23 @@ testRun(void)
{ {
StringList *argList = NULL; 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"); TEST_TITLE("general invocation");
@ -150,6 +167,13 @@ testRun(void)
TEST_RESULT_VOID(testCfgLoad(argList), "help from help command"); TEST_RESULT_VOID(testCfgLoad(argList), "help from help command");
TEST_RESULT_STR_Z(helpRender(helpData), generalHelp, "check text"); 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"); TEST_TITLE("version command");

View File

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