mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-03-05 15:05:48 +02:00
Simplify main() functions in core, doc, and test.
This refactor simplifies the main() functions and puts the more commonly run commands first. For core main() also remove code duplication in local/remote role handling.
This commit is contained in:
parent
faee892067
commit
6c757366c2
@ -55,37 +55,35 @@ main(int argListSize, const char *argList[])
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
cfgLoad((unsigned int)argListSize, argList);
|
||||
|
||||
// Display help
|
||||
// Handle command
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
if (cfgCommandHelp())
|
||||
switch (cfgCommandHelp() ? cfgCmdHelp : cfgCommand())
|
||||
{
|
||||
cmdHelp(BUF(helpData, sizeof(helpData)));
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (cfgCommand())
|
||||
{
|
||||
// Build
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdBuild:
|
||||
cmdBuild(cfgOptionStr(cfgOptRepoPath));
|
||||
break;
|
||||
// Build
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdBuild:
|
||||
cmdBuild(cfgOptionStr(cfgOptRepoPath));
|
||||
break;
|
||||
|
||||
// Display version
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdVersion:
|
||||
printf(PROJECT_NAME " Documentation " PROJECT_VERSION "\n");
|
||||
fflush(stdout);
|
||||
break;
|
||||
// Help
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdHelp:
|
||||
cmdHelp(BUF(helpData, sizeof(helpData)));
|
||||
break;
|
||||
|
||||
// Error on commands that should have already been handled
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdHelp:
|
||||
case cfgCmdNone:
|
||||
case cfgCmdNoop:
|
||||
THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName());
|
||||
break;
|
||||
}
|
||||
// Version
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdVersion:
|
||||
printf(PROJECT_NAME " Documentation " PROJECT_VERSION "\n");
|
||||
fflush(stdout);
|
||||
break;
|
||||
|
||||
// Error on commands that should have been handled
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdNone:
|
||||
case cfgCmdNoop:
|
||||
THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
CATCH_FATAL()
|
||||
|
60
src/main.c
60
src/main.c
@ -100,39 +100,13 @@ main(int argListSize, const char *argList[])
|
||||
// Load the configuration
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
cfgLoad((unsigned int)argListSize, argList);
|
||||
ConfigCommandRole commandRole = cfgCommandRole();
|
||||
const ConfigCommandRole commandRole = cfgCommandRole();
|
||||
|
||||
// Display help
|
||||
// Main/async commands
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
if (cfgCommandHelp())
|
||||
if (commandRole == cfgCmdRoleMain || commandRole == cfgCmdRoleAsync)
|
||||
{
|
||||
cmdHelp(BUF(helpData, sizeof(helpData)));
|
||||
}
|
||||
// Local role
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
else if (commandRole == cfgCmdRoleLocal)
|
||||
{
|
||||
String *name = strNewFmt(PROTOCOL_SERVICE_LOCAL "-%s", strZ(cfgOptionDisplay(cfgOptProcess)));
|
||||
|
||||
cmdLocal(
|
||||
protocolServerNew(
|
||||
name, PROTOCOL_SERVICE_LOCAL_STR, ioFdReadNewOpen(name, STDIN_FILENO, cfgOptionUInt64(cfgOptProtocolTimeout)),
|
||||
ioFdWriteNewOpen(name, STDOUT_FILENO, cfgOptionUInt64(cfgOptProtocolTimeout))));
|
||||
}
|
||||
// Remote role
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
else if (commandRole == cfgCmdRoleRemote)
|
||||
{
|
||||
String *name = strNewFmt(PROTOCOL_SERVICE_REMOTE "-%s", strZ(cfgOptionDisplay(cfgOptProcess)));
|
||||
|
||||
cmdRemote(
|
||||
protocolServerNew(
|
||||
name, PROTOCOL_SERVICE_REMOTE_STR, ioFdReadNewOpen(name, STDIN_FILENO, cfgOptionUInt64(cfgOptProtocolTimeout)),
|
||||
ioFdWriteNewOpen(name, STDOUT_FILENO, cfgOptionUInt64(cfgOptProtocolTimeout))));
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (cfgCommand())
|
||||
switch (cfgCommandHelp() ? cfgCmdHelp : cfgCommand())
|
||||
{
|
||||
// Annotate command
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
@ -204,8 +178,8 @@ main(int argListSize, const char *argList[])
|
||||
// Help command
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdHelp:
|
||||
case cfgCmdNone:
|
||||
THROW(AssertError, "'help' and 'none' commands should have been handled already");
|
||||
cmdHelp(BUF(helpData, sizeof(helpData)));
|
||||
break;
|
||||
|
||||
// Info command
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
@ -303,8 +277,30 @@ main(int argListSize, const char *argList[])
|
||||
printf(PROJECT_NAME " " PROJECT_VERSION "\n");
|
||||
fflush(stdout);
|
||||
break;
|
||||
|
||||
// Error on commands that should have been handled
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdNone:
|
||||
THROW(AssertError, "'none' command should have been handled");
|
||||
}
|
||||
}
|
||||
// Local/remote commands
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
else
|
||||
{
|
||||
ASSERT(commandRole == cfgCmdRoleLocal || commandRole == cfgCmdRoleRemote);
|
||||
|
||||
const String *const service = commandRole == cfgCmdRoleLocal ? PROTOCOL_SERVICE_LOCAL_STR : PROTOCOL_SERVICE_REMOTE_STR;
|
||||
const String *const name = strNewFmt("%s-%s", strZ(service), strZ(cfgOptionDisplay(cfgOptProcess)));
|
||||
const TimeMSec timeout = cfgOptionUInt64(cfgOptProtocolTimeout);
|
||||
ProtocolServer *const server = protocolServerNew(
|
||||
name, service, ioFdReadNewOpen(name, STDIN_FILENO, timeout), ioFdWriteNewOpen(name, STDOUT_FILENO, timeout));
|
||||
|
||||
if (commandRole == cfgCmdRoleLocal)
|
||||
cmdLocal(server);
|
||||
else
|
||||
cmdRemote(server);
|
||||
}
|
||||
}
|
||||
CATCH_FATAL()
|
||||
{
|
||||
|
@ -56,57 +56,55 @@ main(int argListSize, const char *argList[])
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
cfgLoad((unsigned int)argListSize, argList);
|
||||
|
||||
// Display help
|
||||
// Handle command
|
||||
// -------------------------------------------------------------------------------------------------------------------------
|
||||
if (cfgCommandHelp())
|
||||
switch (cfgCommandHelp() ? cfgCmdHelp : cfgCommand())
|
||||
{
|
||||
cmdHelp(BUF(helpData, sizeof(helpData)));
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (cfgCommand())
|
||||
// Test
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdTest:
|
||||
{
|
||||
// Test
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdTest:
|
||||
// Run a single test
|
||||
if (cfgOptionTest(cfgOptVmId))
|
||||
{
|
||||
// Run a single test
|
||||
if (cfgOptionTest(cfgOptVmId))
|
||||
{
|
||||
cmdTest(
|
||||
cfgOptionStr(cfgOptRepoPath), cfgOptionStr(cfgOptTestPath), cfgOptionStr(cfgOptVm),
|
||||
cfgOptionUInt(cfgOptVmId), cfgOptionStr(cfgOptPgVersion), strLstGet(cfgCommandParam(), 0),
|
||||
cfgOptionTest(cfgOptTest) ? cfgOptionUInt(cfgOptTest) : 0, cfgOptionUInt64(cfgOptScale),
|
||||
logLevelEnum(cfgOptionStrId(cfgOptLogLevelTest)), cfgOptionBool(cfgOptLogTimestamp),
|
||||
cfgOptionStrNull(cfgOptTz), cfgOptionBool(cfgOptCoverage), cfgOptionBool(cfgOptProfile),
|
||||
cfgOptionBool(cfgOptOptimize), cfgOptionBool(cfgOptBackTrace));
|
||||
}
|
||||
// Top-level test
|
||||
else
|
||||
{
|
||||
result = testCvgGenerate(
|
||||
cfgOptionStr(cfgOptRepoPath), cfgOptionStr(cfgOptTestPath), cfgOptionStr(cfgOptVm),
|
||||
cfgOptionBool(cfgOptCoverageSummary), cfgCommandParam()) > 0;
|
||||
}
|
||||
|
||||
break;
|
||||
cmdTest(
|
||||
cfgOptionStr(cfgOptRepoPath), cfgOptionStr(cfgOptTestPath), cfgOptionStr(cfgOptVm),
|
||||
cfgOptionUInt(cfgOptVmId), cfgOptionStr(cfgOptPgVersion), strLstGet(cfgCommandParam(), 0),
|
||||
cfgOptionTest(cfgOptTest) ? cfgOptionUInt(cfgOptTest) : 0, cfgOptionUInt64(cfgOptScale),
|
||||
logLevelEnum(cfgOptionStrId(cfgOptLogLevelTest)), cfgOptionBool(cfgOptLogTimestamp),
|
||||
cfgOptionStrNull(cfgOptTz), cfgOptionBool(cfgOptCoverage), cfgOptionBool(cfgOptProfile),
|
||||
cfgOptionBool(cfgOptOptimize), cfgOptionBool(cfgOptBackTrace));
|
||||
}
|
||||
// Top-level test
|
||||
else
|
||||
{
|
||||
result = testCvgGenerate(
|
||||
cfgOptionStr(cfgOptRepoPath), cfgOptionStr(cfgOptTestPath), cfgOptionStr(cfgOptVm),
|
||||
cfgOptionBool(cfgOptCoverageSummary), cfgCommandParam()) > 0;
|
||||
}
|
||||
|
||||
// Display version
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdVersion:
|
||||
printf(PROJECT_NAME " Test " PROJECT_VERSION "\n");
|
||||
fflush(stdout);
|
||||
break;
|
||||
|
||||
// Error on commands that should have already been handled
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdHelp:
|
||||
case cfgCmdNone:
|
||||
case cfgCmdNoop:
|
||||
THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName());
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
// Help
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdHelp:
|
||||
cmdHelp(BUF(helpData, sizeof(helpData)));
|
||||
break;
|
||||
|
||||
// Version
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdVersion:
|
||||
printf(PROJECT_NAME " Test " PROJECT_VERSION "\n");
|
||||
fflush(stdout);
|
||||
break;
|
||||
|
||||
// Error on commands that should have been handled
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
case cfgCmdNone:
|
||||
case cfgCmdNoop:
|
||||
THROW_FMT(AssertError, "'%s' command should have been handled", cfgCommandName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
CATCH_FATAL()
|
||||
|
Loading…
x
Reference in New Issue
Block a user