Skip to content

Commit

Permalink
bconsole: replacing custom CLI parsing with CLI11
Browse files Browse the repository at this point in the history
  • Loading branch information
alaaeddineelamri committed Jul 11, 2022
1 parent e3fcc1a commit f829cab
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 129 deletions.
6 changes: 4 additions & 2 deletions core/src/console/CMakeLists.txt
@@ -1,6 +1,6 @@
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2017-2021 Bareos GmbH & Co. KG
# Copyright (C) 2017-2022 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -36,7 +36,9 @@ add_executable(bconsole console.cc)
add_library(console_objects STATIC ${BCONSSRCS})

target_link_libraries(console_objects PRIVATE bareos)
target_link_libraries(bconsole PRIVATE console_objects ${Readline_LIBRARY})
target_link_libraries(
bconsole PRIVATE console_objects ${Readline_LIBRARY} CLI11::CLI11
)

install(
TARGETS bconsole
Expand Down
194 changes: 87 additions & 107 deletions core/src/console/console.cc
Expand Up @@ -38,6 +38,7 @@
#include "lib/bnet_network_dump.h"
#include "lib/bsock_tcp.h"
#include "lib/bstringlist.h"
#include "lib/cli.h"
#include "lib/qualified_resource_name_type_converter.h"
#include "lib/watchdog.h"
#include <stdio.h>
Expand Down Expand Up @@ -106,32 +107,6 @@ static int EolCmd(FILE* input, BareosSocket* UA_sock);
# include <regex.h>
#endif

static void usage()
{
kBareosVersionStrings.PrintCopyrightWithFsfAndPlanets(stderr, 2000);
fprintf(
stderr,
_("Usage: bconsole [-s] [-c config_file] [-d debug_level]\n"
" -D <dir> select a Director\n"
" -l list defined Directors\n"
" -c <path> specify configuration file or directory\n"
#if defined(HAVE_PAM)
" -p <file> specify pam credentials file\n"
" (first line: username, second line: password)\n"
" -o send pam credentials over unencrypted connection\n"
#endif
" -d <nn> set debug level to <nn>\n"
" -dt print timestamp in debug output\n"
" -s no signals\n"
" -u <nn> set command execution timeout to <nn> seconds\n"
" -t test - read configuration and exit\n"
" -xc print configuration and exit\n"
" -xs print configuration file schema in JSON format "
"and exit\n"
" -? print this message.\n"
"\n"));
}

extern "C" void GotSigstop(int sig) { stop = true; }

extern "C" void GotSigcontinue(int sig) { stop = false; }
Expand Down Expand Up @@ -861,15 +836,6 @@ static bool ExaminePamAuthentication(

int main(int argc, char* argv[])
{
int ch;
char* director = NULL;
bool list_directors = false;
bool no_signals = false;
bool test_config = false;
bool export_config = false;
bool export_config_schema = false;
PoolMem history_file;

setlocale(LC_ALL, "");
tzset();
bindtextdomain("bareos", LOCALEDIR);
Expand All @@ -881,86 +847,104 @@ int main(int argc, char* argv[])
working_directory = "/tmp";
args = GetPoolMemory(PM_FNAME);

while ((ch = getopt(argc, argv, program_arguments.c_str())) != -1) {
switch (ch) {
case 'D': /* Director */
if (director) { free(director); }
director = strdup(optarg);
break;
CLI::App console_app;
InitCLIApp(console_app, "The Bareos Console.", 2000);

console_app
.add_option(
"-c,--config",
[](std::vector<std::string> val) {
if (configfile != nullptr) { free(configfile); }
configfile = strdup(val.front().c_str());
return true;
},
"Use <path> as configuration file or directory")
->check(CLI::ExistingPath)
->type_name("<path>");

char* director = nullptr;
console_app
.add_option(
"-D,--director",
[&director](std::vector<std::string> val) {
if (director != nullptr) { free(director); }
director = strdup(val.front().c_str());
return true;
},
"Specify director.")
->type_name("<director>");

AddDebugOptions(console_app);

case 'l':
bool list_directors = false;
bool test_config = false;
console_app.add_flag(
"-l,--list-directors",
[&list_directors, &test_config](bool val) {
list_directors = true;
test_config = true;
break;

case 'c': /* configuration file */
if (configfile != NULL) { free(configfile); }
configfile = strdup(optarg);
break;

case 'd':
if (*optarg == 't') {
dbg_timestamp = true;
} else {
debug_level = atoi(optarg);
if (debug_level <= 0) { debug_level = 1; }
}
break;
},
"List defined Directors.");

#if defined(HAVE_PAM)
case 'p':
pam_credentials_filename = optarg;
if (pam_credentials_filename.empty()) {
Emsg0(M_ERROR_TERM, 0, _("No filename given for -p.\n"));
usage();
} else {
if (FILE* f = fopen(pam_credentials_filename.c_str(), "r+")) {
use_pam_credentials_file = true;
fclose(f);
} else { /* file cannot be opened, i.e. does not exist */
Emsg0(M_ERROR_TERM, 0, _("Could not open file for -p.\n"));
}
}
break;
console_app
.add_option(
"-p,--pam-credentials-filename",
[](std::vector<std::string> val) {
pam_credentials_filename = val.front();
if (FILE* f = fopen(pam_credentials_filename.c_str(), "r+")) {
use_pam_credentials_file = true;
fclose(f);
} else { /* file cannot be opened, i.e. does not exist */
Emsg0(M_ERROR_TERM, 0, _("Could not open file for -p.\n"));
}

return true;
},
"PAM Credentials file.")
->check(CLI::ExistingFile)
->type_name("<path>");

console_app.add_flag("-o", force_send_pam_credentials_unencrypted,
"Force sending pam credentials unencrypted.");

case 'o':
force_send_pam_credentials_unencrypted = true;
break;
#endif /* HAVE_PAM */

case 's': /* turn off signals */
no_signals = true;
break;
bool no_signals = false;
console_app.add_flag("-s,--no-signals", no_signals,
"No signals (for debugging)");

case 't':
test_config = true;
break;
console_app.add_flag("-t,--test-config", test_config,
"Test - read configuration and exit");

case 'u':
timeout = atoi(optarg);
break;
console_app
.add_option("-u,--timeout", timeout,
"Set command execution timeout to <seconds>.")
->type_name("<seconds>")
->check(CLI::PositiveNumber);

bool export_config = false;
CLI::Option* xc
= console_app.add_flag("--xc,--export-config", export_config,
"Print configuration resources and exit");

case 'x': /* export configuration/schema and exit */
if (*optarg == 's') {
export_config_schema = true;
} else if (*optarg == 'c') {
export_config = true;
} else {
usage();
bool export_config_schema = false;
console_app
.add_flag("--xs,--export-schema", export_config_schema,
"Print configuration schema in JSON format and exit")
->excludes(xc);

console_app.add_option(
"-z, --network-debugging",
[](std::vector<std::string> val) {
if (!BnetDump::EvaluateCommandLineArgs(val.front().c_str())) {
exit(1);
}
break;
case 'z': /* switch network debugging on */
if (!BnetDump::EvaluateCommandLineArgs(optarg)) { exit(1); }
break;
return true;
},
"Switch network debugging on");

case '?':
default:
usage();
exit(1);
}
}
argc -= optind;
argv += optind;
CLI11_PARSE(console_app, argc, argv);

if (!no_signals) { InitSignals(TerminateConsole); }

Expand All @@ -976,11 +960,6 @@ int main(int argc, char* argv[])

OSDependentInit();

if (argc) {
usage();
exit(1);
}

if (export_config_schema) {
PoolMem buffer;

Expand Down Expand Up @@ -1124,6 +1103,7 @@ int main(int argc, char* argv[])
}
}

PoolMem history_file;
if (me && me->history_file) {
PmStrcpy(history_file, me->history_file);
ConsoleInitHistory(history_file.c_str());
Expand Down
4 changes: 2 additions & 2 deletions docs/manuals/CMakeLists.txt
Expand Up @@ -66,9 +66,9 @@ add_custom_command(
add_custom_command(
OUTPUT
${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
COMMAND bconsole -xs >/dev/null
COMMAND bconsole --xs >/dev/null
COMMAND
bconsole -xs
bconsole --xs
>${PROJECT_SOURCE_DIR}/source/include/autogenerated/bconsole-config-schema.json
COMMAND
sed --in-place --expression="/\\\"version\\\":/d"
Expand Down
37 changes: 22 additions & 15 deletions docs/manuals/source/TasksAndConcepts/BareosConsole.rst
Expand Up @@ -31,21 +31,28 @@ The console program can be run with the following options:
.. code-block:: shell-session
:caption: bconsole command line options
root@host:~# bconsole -?
Usage: bconsole [-s] [-c config_file] [-d debug_level]
-D <dir> select a Director
-l list Directors defined
-c <path> specify configuration file or directory
-p <file> specify pam credentials file
-o send pam credentials over unencrypted connection
-d <nn> set debug level to <nn>
-dt print timestamp in debug output
-s no signals
-u <nn> set command execution timeout to <nn> seconds
-t test - read configuration and exit
-xc print configuration and exit
-xs print configuration file schema in JSON format and exit
-? print this message.
root@host:~# bconsole -h
Usage: bconsole [OPTIONS] [configuration]
Positionals:
configuration <path> Use <path> as configuration file or directory
Options:
-h,--help Print this help message and exit
-c,--config <path> Use <path> as configuration file or directory
-D,--director <director> Specify director.
-d,--debug-level <level> Set debug level to <level>.
--dt,--debug-timestamps Print timestamp in debug output.
-l,--list-directors List defined Directors.
-p,--pam-credentials-filename <path> PAM Credentials file.
-o Force sending pam credentials unencrypted.
-s,--no-signals No signals (for debugging)
-t,--test-config Test - read configuration and exit
-u,--timeout <seconds> Set command execution timeout to <seconds>.
--xc,--export-config Excludes: --xs
Print all configuration resources and exit
--xs,--export-schema Excludes: --xc Print configuration schema in JSON format and exit
-z,--network-debugging Switch network debugging on
After launching the Console program (bconsole), it will prompt you for the next command with an asterisk (*). Generally, for all commands, you can simply enter the command name and the Console program will prompt you for the necessary arguments. Alternatively, in most cases, you may enter the command followed by arguments. The general format is:

Expand Down
6 changes: 3 additions & 3 deletions systemtests/tests/bareos-acl/testrunner
Expand Up @@ -20,9 +20,9 @@ start_test

run_bareos "$@"

echo "status conf" | bin/bconsole -c etc/bareos/bconsole-acl-none.conf > ${logdir}/acl-none.log
echo "status conf" | bin/bconsole -c etc/bareos/bconsole-acl-status.conf > ${logdir}/acl-status.log
echo "status conf" | bin/bconsole -c etc/bareos/bconsole-acl-status-conf.conf > ${logdir}/acl-status-conf.log
echo "status conf" | "${BAREOS_BCONSOLE_BINARY}" -c etc/bareos/bconsole-acl-none.conf > ${logdir}/acl-none.log
echo "status conf" | "${BAREOS_BCONSOLE_BINARY}" -c etc/bareos/bconsole-acl-status.conf > ${logdir}/acl-status.log
echo "status conf" | "${BAREOS_BCONSOLE_BINARY}" -c etc/bareos/bconsole-acl-status-conf.conf > ${logdir}/acl-status-conf.log

export estat=0

Expand Down

0 comments on commit f829cab

Please sign in to comment.