SNode.C
Loading...
Searching...
No Matches
utils::Config Class Reference

#include <Config.h>

Collaboration diagram for utils::Config:

Public Member Functions

 Config ()=delete
 
 Config (const Config &)=delete
 
 ~Config ()=delete
 
Configoperator= (const Config &)=delete
 

Static Public Member Functions

static bool init (int argc, char *argv[])
 
static bool bootstrap ()
 
static void terminate ()
 
static CLI::App * addInstance (const std::string &name, const std::string &description, const std::string &group)
 
static CLI::App * getInstance (const std::string &name)
 
static bool removeInstance (CLI::App *instance)
 
static void required (CLI::App *instance, bool required=true)
 
static void disabled (CLI::App *instance, bool disabled=true)
 
static CLI::App * addStandardFlags (CLI::App *app)
 
static CLI::App * addSimpleHelp (CLI::App *app)
 
static CLI::App * addHelp (CLI::App *app)
 
static std::string getApplicationName ()
 
static int getLogLevel ()
 
static int getVerboseLevel ()
 
static CLI::Option * addStringOption (const std::string &name, const std::string &description, const std::string &typeName)
 
static CLI::Option * addStringOption (const std::string &name, const std::string &description, const std::string &typeName, bool configurable)
 
static CLI::Option * addStringOption (const std::string &name, const std::string &description, const std::string &typeName, const std::string &defaultValue)
 
static CLI::Option * addStringOption (const std::string &name, const std::string &description, const std::string &typeName, const std::string &defaultValue, bool configurable)
 
static CLI::Option * addStringOption (const std::string &name, const std::string &description, const std::string &typeName, const char *defaultValue)
 
static CLI::Option * addStringOption (const std::string &name, const std::string &description, const std::string &typeName, const char *defaultValue, bool configurable)
 
static std::string getStringOptionValue (const std::string &name)
 
static void addFlag (const std::string &name, bool &variable, const std::string &description, bool required, bool configurable=true, const std::string &groupName="Application Options")
 

Static Public Attributes

static std::shared_ptr< CLI::App > app = makeApp()
 
static int argc = 0
 
static char ** argv = nullptr
 

Static Private Member Functions

static bool parse1 ()
 
static bool parse2 ()
 

Static Private Attributes

static std::shared_ptr< CLI::Formatter > sectionFormatter = makeSectionFormatter()
 
static bool subParse
 
static std::string applicationName
 
static std::string configDirectory
 
static std::string logDirectory
 
static std::string pidDirectory
 
static CLI::Option * daemonizeOpt = nullptr
 
static CLI::Option * logFileOpt = nullptr
 
static CLI::Option * userNameOpt = nullptr
 
static CLI::Option * groupNameOpt = nullptr
 
static CLI::Option * enforceLogFileOpt = nullptr
 
static CLI::Option * logLevelOpt = nullptr
 
static CLI::Option * verboseLevelOpt = nullptr
 
static CLI::Option * quietOpt = nullptr
 
static CLI::App * helpApp = nullptr
 
static std::map< std::string, std::string > aliases
 
static std::map< std::string, CLI::Option * > applicationOptions
 

Detailed Description

Definition at line 61 of file Config.h.

Constructor & Destructor Documentation

◆ Config() [1/2]

utils::Config::Config ( )
delete

◆ Config() [2/2]

utils::Config::Config ( const Config )
delete

◆ ~Config()

utils::Config::~Config ( )
delete

Member Function Documentation

◆ addFlag()

void utils::Config::addFlag ( const std::string &  name,
bool &  variable,
const std::string &  description,
bool  required,
bool  configurable = true,
const std::string &  groupName = "Application Options" 
)
static

Definition at line 949 of file Config.cpp.

954 {
955 app->add_flag(name, variable, description) //
956 ->required(required) //
957 ->configurable(configurable) //
958 ->group(groupName);
959 }
static std::shared_ptr< CLI::App > app
Definition Config.h:123
static void required(CLI::App *instance, bool required=true)
Definition Config.cpp:827

◆ addHelp()

CLI::App * utils::Config::addHelp ( CLI::App *  app)
static

Definition at line 801 of file Config.cpp.

801 {
802 app->set_help_flag(
803 "-h{standard},--help{standard}",
804 [app](std::size_t) {
805 helpApp = app;
806 },
807 "Print help message and exit")
808 ->group(app->get_formatter()->get_label("Nonpersistent Options"))
809 ->check(CLI::IsMember({"standard", "exact", "expanded"}));
810
811 return app;
812 }
static CLI::App * helpApp
Definition Config.h:152

◆ addInstance()

CLI::App * utils::Config::addInstance ( const std::string &  name,
const std::string &  description,
const std::string &  group 
)
static

Definition at line 707 of file Config.cpp.

707 {
708 CLI::App* instanceSc = app->add_subcommand(name, description) //
709 ->group(group)
710 ->fallthrough()
711 ->formatter(sectionFormatter)
712 ->configurable(false)
713 ->allow_extras(false)
714 ->disabled(name.empty());
715
716 instanceSc //
717 ->option_defaults()
718 ->configurable(!instanceSc->get_disabled());
719
720 if (!instanceSc->get_disabled()) {
721 if (aliases.contains(name)) {
722 instanceSc //
723 ->alias(aliases[name]);
724 }
725 }
726
728 utils::Config::addHelp(instanceSc);
729
730 return instanceSc;
731 }
static CLI::App * addStandardFlags(CLI::App *app)
Definition Config.cpp:737
static CLI::App * addHelp(CLI::App *app)
Definition Config.cpp:801
static std::shared_ptr< CLI::Formatter > sectionFormatter
Definition Config.h:129
static std::map< std::string, std::string > aliases
Definition Config.h:154

◆ addSimpleHelp()

CLI::App * utils::Config::addSimpleHelp ( CLI::App *  app)
static

Definition at line 814 of file Config.cpp.

814 {
815 app->set_help_flag(
816 "--help,-h",
817 [app](std::size_t) {
818 helpApp = app;
819 },
820 "Print help message and exit")
821 ->group(app->get_formatter()->get_label("Nonpersistent Options"))
822 ->disable_flag_override();
823
824 return app;
825 }

◆ addStandardFlags()

CLI::App * utils::Config::addStandardFlags ( CLI::App *  app)
static

Definition at line 737 of file Config.cpp.

737 {
738 app //
739 ->add_flag_callback(
740 "-s,--show-config",
741 [app]() {
743 },
744 "Show current configuration and exit") //
745 ->configurable(false)
746 ->disable_flag_override();
747
748 app //
749 ->add_flag(
750 "--command-line{standard}",
751 [app]([[maybe_unused]] std::int64_t count) {
752 const std::string& result = app->get_option("--command-line")->as<std::string>();
753 if (result == "standard") {
755 app,
756 "Below is a command line viewing all non-default and required options:\n"
757 "* Options show their configured value\n"
758 "* Required but not yet configured options show <REQUIRED> as value\n"
759 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
761 }
762 if (result == "required") {
764 app,
765 "Below is a command line viewing required options only:\n"
766 "* Options show either their configured or default value\n"
767 "* Required but not yet configured options show <REQUIRED> as value\n"
768 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
770 }
771 if (result == "full") {
773 app,
774 "Below is a command line viewing the full set of options with their default or configured values:\n"
775 "* Options show either their configured or default value\n"
776 "* Required but not yet configured options show <REQUIRED> as value\n"
777 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
779 }
780 if (result == "default") {
782 app,
783 "Below is a command line viewing the full set of options with their default values\n"
784 "* Options show their default value\n"
785 "* Required but not yet configured options show <REQUIRED> as value\n"
786 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
788 }
789 },
790 "Print a command line\n"
791 " standard (default): View all non-default and required options\n"
792 " required: View required options only\n"
793 " full: View the full set of options with their default or configured values\n"
794 " default: View the full set of options with their default values")
795 ->configurable(false)
796 ->check(CLI::IsMember({"standard", "required", "full", "default"}));
797
798 return app;
799 }

References CLI::CallForCommandline::CallForCommandline(), CLI::CallForShowConfig::CallForShowConfig(), CLI::CallForCommandline::DEFAULT, CLI::CallForCommandline::FULL, CLI::CallForCommandline::REQUIRED, and CLI::CallForCommandline::STANDARD.

Here is the call graph for this function:

◆ addStringOption() [1/6]

CLI::Option * utils::Config::addStringOption ( const std::string &  name,
const std::string &  description,
const std::string &  typeName 
)
static

Definition at line 884 of file Config.cpp.

884 {
885 applicationOptions[name] = app //
886 ->add_option(name, description)
887 ->type_name(typeName)
888 ->configurable()
889 ->required()
890 ->group("Application Options");
891
892 app->needs(applicationOptions[name]);
893
894 return applicationOptions[name];
895 }
static std::map< std::string, CLI::Option * > applicationOptions
Definition Config.h:155

◆ addStringOption() [2/6]

CLI::Option * utils::Config::addStringOption ( const std::string &  name,
const std::string &  description,
const std::string &  typeName,
bool  configurable 
)
static

Definition at line 898 of file Config.cpp.

898 {
899 addStringOption(name, description, typeName);
900 return applicationOptions[name] //
901 ->configurable(configurable);
902 }
static CLI::Option * addStringOption(const std::string &name, const std::string &description, const std::string &typeName)
Definition Config.cpp:884

◆ addStringOption() [3/6]

CLI::Option * utils::Config::addStringOption ( const std::string &  name,
const std::string &  description,
const std::string &  typeName,
const char *  defaultValue 
)
static

Definition at line 929 of file Config.cpp.

932 {
933 return addStringOption(name, description, typeName, std::string(defaultValue));
934 }

◆ addStringOption() [4/6]

CLI::Option * utils::Config::addStringOption ( const std::string &  name,
const std::string &  description,
const std::string &  typeName,
const char *  defaultValue,
bool  configurable 
)
static

Definition at line 936 of file Config.cpp.

937 {
938 return addStringOption(name, description, typeName, std::string(defaultValue), configurable);
939 }

◆ addStringOption() [5/6]

CLI::Option * utils::Config::addStringOption ( const std::string &  name,
const std::string &  description,
const std::string &  typeName,
const std::string &  defaultValue 
)
static

Definition at line 904 of file Config.cpp.

907 {
908 addStringOption(name, description, typeName);
909
910 applicationOptions[name] //
911 ->required(false)
912 ->default_str(defaultValue);
913
914 app->remove_needs(applicationOptions[name]);
915
916 return applicationOptions[name];
917 }

◆ addStringOption() [6/6]

CLI::Option * utils::Config::addStringOption ( const std::string &  name,
const std::string &  description,
const std::string &  typeName,
const std::string &  defaultValue,
bool  configurable 
)
static

Definition at line 919 of file Config.cpp.

923 {
924 addStringOption(name, description, typeName, defaultValue);
925 return applicationOptions[name] //
926 ->configurable(configurable);
927 }

◆ bootstrap()

bool utils::Config::bootstrap ( )
static

Definition at line 367 of file Config.cpp.

367 {
368 aliases.clear();
369
370 app->final_callback([]() {
371 if (daemonizeOpt->as<bool>() && (*app)["--show-config"]->count() == 0 && (*app)["--write-config"]->count() == 0 &&
372 (*app)["--command-line"]->count() == 0) {
373 std::cout << "Running as daemon (double fork)" << std::endl;
374
375 utils::Daemon::startDaemon(
376 pidDirectory + "/" + applicationName + ".pid", userNameOpt->as<std::string>(), groupNameOpt->as<std::string>());
377
378 logger::Logger::setQuiet();
379
380 const std::string logFile = logFileOpt->as<std::string>();
381 if (!logFile.empty()) {
382 logger::Logger::logToFile(logFile);
383 }
384 } else if ((*app)["--enforce-log-file"]->as<bool>()) {
385 const std::string logFile = logFileOpt->as<std::string>();
386 if (!logFile.empty()) {
387 std::cout << "Writing logs to file " << logFile << std::endl;
388
390 }
391 }
392 });
393
394 return parse2();
395 }
static void logToFile(const std::string &logFile)
Definition Logger.cpp:127
static bool parse2()
Definition Config.cpp:584
static CLI::Option * logFileOpt
Definition Config.h:144
static CLI::Option * daemonizeOpt
Definition Config.h:143

References logger::Logger::logToFile(), logger::Logger::setQuiet(), and utils::Daemon::startDaemon().

Here is the call graph for this function:

◆ disabled()

void utils::Config::disabled ( CLI::App *  instance,
bool  disabled = true 
)
static

Definition at line 850 of file Config.cpp.

850 {
851 if (disabled) {
852 app->remove_needs(instance);
853
854 for (const auto& sub : instance->get_subcommands({})) {
855 // sub->disabled();
856
857 if (sub->get_ignore_case()) {
858 instance->remove_needs(sub);
859 sub->required(false); // ### must be stored in ConfigInstance
860 }
861 }
862 } else {
863 app->needs(instance);
864
865 for (const auto& sub : instance->get_subcommands({})) {
866 // sub->disabled(false);
867
868 if (sub->get_ignore_case()) { // ### must be recalled from ConfigInstance
869 instance->needs(sub);
870 sub->required();
871 }
872 }
873 }
874
875 instance->required(disabled ? false : instance->get_ignore_case());
876 }
static void disabled(CLI::App *instance, bool disabled=true)
Definition Config.cpp:850

◆ getApplicationName()

std::string utils::Config::getApplicationName ( )
static

Definition at line 961 of file Config.cpp.

961 {
962 return applicationName;
963 }
static std::string applicationName
Definition Config.h:137

◆ getInstance()

CLI::App * utils::Config::getInstance ( const std::string &  name)
static

Definition at line 733 of file Config.cpp.

733 {
734 return app->get_subcommand(name);
735 }

◆ getLogLevel()

int utils::Config::getLogLevel ( )
static

Definition at line 965 of file Config.cpp.

965 {
966 return logLevelOpt->as<int>();
967 }
static CLI::Option * logLevelOpt
Definition Config.h:148

◆ getStringOptionValue()

std::string utils::Config::getStringOptionValue ( const std::string &  name)
static

Definition at line 941 of file Config.cpp.

941 {
942 if (app->get_option(name) == nullptr) {
943 throw CLI::OptionNotFound(name);
944 }
945
946 return (*app)[name]->as<std::string>();
947 }

◆ getVerboseLevel()

int utils::Config::getVerboseLevel ( )
static

Definition at line 969 of file Config.cpp.

969 {
970 return verboseLevelOpt->as<int>();
971 }
static CLI::Option * verboseLevelOpt
Definition Config.h:149

◆ init()

bool utils::Config::init ( int  argc,
char *  argv[] 
)
static

Definition at line 130 of file Config.cpp.

130 {
131 bool proceed = true;
132
135
136 applicationName = std::filesystem::path(argv[0]).filename();
137
138 uid_t euid = 0;
139 struct passwd* pw = nullptr;
140 struct group* gr = nullptr;
141
142 if ((pw = getpwuid(getuid())) == nullptr) {
143 proceed = false;
144 } else if ((gr = getgrgid(pw->pw_gid)) == nullptr) {
145 proceed = false;
146 } else if ((euid = geteuid()) == 0) {
147 configDirectory = "/etc/snode.c";
148 logDirectory = "/var/log/snode.c";
149 pidDirectory = "/var/run/snode.c";
150 } else {
151 const char* homedir = nullptr;
152 if ((homedir = std::getenv("XDG_CONFIG_HOME")) == nullptr) {
153 if ((homedir = std::getenv("HOME")) == nullptr) {
154 homedir = pw->pw_dir;
155 }
156 }
157
158 if (homedir != nullptr) {
159 configDirectory = std::string(homedir) + "/.config/snode.c";
160 logDirectory = std::string(homedir) + "/.local/log/snode.c";
161 pidDirectory = std::string(homedir) + "/.local/run/snode.c";
162 } else {
163 proceed = false;
164 }
165 }
166
167 if (proceed && !std::filesystem::exists(configDirectory)) {
168 if (std::filesystem::create_directories(configDirectory)) {
169 std::filesystem::permissions(
171 (std::filesystem::perms::owner_all | std::filesystem::perms::group_read | std::filesystem::perms::group_exec) &
172 ~std::filesystem::perms::others_all);
173 if (geteuid() == 0) {
174 struct group* gr = nullptr;
175 if ((gr = getgrnam(XSTR(GROUP_NAME))) != nullptr) {
176 if (chown(configDirectory.c_str(), euid, gr->gr_gid) < 0) {
177 std::cout << "Warning: Can not set group ownership of '" << configDirectory
178 << "' to 'snodec':" << strerror(errno) << std::endl;
179 }
180 } else {
181 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup" << std::endl;
182 std::cout << " and add the current user to this group." << std::endl;
183 std::filesystem::remove(configDirectory);
184 proceed = false;
185 }
186 }
187 } else {
188 std::cout << "Error: Can not create directory '" << configDirectory << "'" << std::endl;
189 proceed = false;
190 }
191 }
192
193 if (proceed && !std::filesystem::exists(logDirectory)) {
194 if (std::filesystem::create_directories(logDirectory)) {
195 std::filesystem::permissions(logDirectory,
196 (std::filesystem::perms::owner_all | std::filesystem::perms::group_all) &
197 ~std::filesystem::perms::others_all);
198 if (geteuid() == 0) {
199 struct group* gr = nullptr;
200 if ((gr = getgrnam(XSTR(GROUP_NAME))) != nullptr) {
201 if (chown(logDirectory.c_str(), euid, gr->gr_gid) < 0) {
202 std::cout << "Warning: Can not set group ownership of '" << logDirectory << "' to 'snodec':" << strerror(errno)
203 << std::endl;
204 }
205 } else {
206 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup" << std::endl;
207 std::cout << " and add the current user to this group." << std::endl;
208 std::filesystem::remove(configDirectory);
209 proceed = false;
210 }
211 }
212 } else {
213 std::cout << "Error: Can not create directory '" << logDirectory << "'" << std::endl;
214 proceed = false;
215 }
216 }
217
218 if (proceed && !std::filesystem::exists(pidDirectory)) {
219 if (std::filesystem::create_directories(pidDirectory)) {
220 std::filesystem::permissions(pidDirectory,
221 (std::filesystem::perms::owner_all | std::filesystem::perms::group_all) &
222 ~std::filesystem::perms::others_all);
223 if (geteuid() == 0) {
224 struct group* gr = nullptr;
225 if ((gr = getgrnam(XSTR(GROUP_NAME))) != nullptr) {
226 if (chown(pidDirectory.c_str(), euid, gr->gr_gid) < 0) {
227 std::cout << "Warning: Can not set group ownership of '" << pidDirectory << "' to 'snodec':" << strerror(errno)
228 << std::endl;
229 }
230 } else {
231 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup." << std::endl;
232 std::cout << " and add the current user to this group." << std::endl;
233 std::filesystem::remove(configDirectory);
234 proceed = false;
235 }
236 }
237 } else {
238 std::cout << "Error: Can not create directory '" << pidDirectory << "'" << std::endl;
239 proceed = false;
240 }
241 }
242
243 if (proceed) {
244 app->description("Configuration for Application '" + applicationName + "'");
245
246 app->footer("Application '" + applicationName +
247 "' powered by SNode.C\n"
248 "(C) 2020-2025 Volker Christian <me@vchrist.at>\n"
249 "https://github.com/SNodeC/snode.c");
250
251 app->set_config( //
252 "-c,--config-file",
253 configDirectory + "/" + applicationName + ".conf",
254 "Read a config file",
255 false) //
256 ->take_all()
257 ->type_name("configfile")
258 ->check(!CLI::ExistingDirectory);
259
260 app->add_option_function<std::string>(
261 "-w,--write-config",
262 []([[maybe_unused]] const std::string& configFile) {
263 throw CLI::CallForWriteConfig(configFile);
264 },
265 "Write config file and exit")
266 ->configurable(false)
267 ->default_val(configDirectory + "/" + applicationName + ".conf")
268 ->type_name("[configfile]")
269 ->check(!CLI::ExistingDirectory)
270 ->expected(0, 1);
271
272 app->add_flag( //
273 "-k,--kill",
274 "Kill running daemon") //
275 ->configurable(false)
276 ->disable_flag_override();
277
278 app->add_option( //
279 "-i,--instance-alias",
280 "Make an instance also known as an alias in configuration files")
281 ->configurable(false)
282 ->type_name("instance=instance_alias [instance=instance_alias [...]]")
283 ->each([](const std::string& item) {
284 const auto it = item.find('=');
285 if (it != std::string::npos) {
286 aliases[item.substr(0, it)] = item.substr(it + 1);
287 } else {
288 throw CLI::ConversionError("Can not convert '" + item + "' to a 'instance=instance_alias' pair");
289 }
290 });
291
292 addStandardFlags(app.get());
293
294 logLevelOpt = app->add_option( //
295 "-l,--log-level",
296 "Log level") //
297 ->default_val(4)
298 ->type_name("level")
299 ->check(CLI::Range(0, 6))
300 ->group(app->get_formatter()->get_label("Persistent Options"));
301
302 verboseLevelOpt = app->add_option( //
303 "-v,--verbose-level",
304 "Verbose level") //
305 ->default_val(2)
306 ->type_name("level")
307 ->check(CLI::Range(0, 10))
308 ->group(app->get_formatter()->get_label("Persistent Options"));
309
310 quietOpt = app->add_flag( //
311 "-q{true},!-u,--quiet{true}",
312 "Quiet mode") //
313 ->default_val("false")
314 ->type_name("bool")
315 ->check(CLI::IsMember({"true", "false"}))
316 ->group(app->get_formatter()->get_label("Persistent Options"));
317
318 logFileOpt = app->add_option( //
319 "--log-file",
320 "Log file path") //
321 ->default_val(logDirectory + "/" + applicationName + ".log")
322 ->type_name("logfile")
323 ->check(!CLI::ExistingDirectory)
324 ->group(app->get_formatter()->get_label("Persistent Options"));
325
326 enforceLogFileOpt = app->add_flag( //
327 "-e{true},!-n,--enforce-log-file{true}",
328 "Enforce writing of logs to file for foreground applications") //
329 ->default_val("false")
330 ->type_name("bool")
331 ->check(CLI::IsMember({"true", "false"}))
332 ->group(app->get_formatter()->get_label("Persistent Options"));
333
334 daemonizeOpt = app->add_flag( //
335 "-d{true},!-f,--daemonize{true}",
336 "Start application as daemon") //
337 ->default_val("false")
338 ->type_name("bool")
339 ->check(CLI::IsMember({"true", "false"}))
340 ->group(app->get_formatter()->get_label("Persistent Options"));
341
342 userNameOpt = app->add_option( //
343 "--user-name",
344 "Run daemon under specific user permissions") //
345 ->default_val(pw->pw_name)
346 ->type_name("username")
347 ->needs(daemonizeOpt)
348 ->group(app->get_formatter()->get_label("Persistent Options"));
349
350 groupNameOpt = app->add_option( //
351 "--group-name",
352 "Run daemon under specific group permissions")
353 ->default_val(gr->gr_name)
354 ->type_name("groupname")
355 ->needs(daemonizeOpt)
356 ->group(app->get_formatter()->get_label("Persistent Options"));
357
358 proceed = parse1(); // for stopDaemon and pre init application options
359
360 app->set_version_flag("--version", "1.0-rc1", "Framework version");
361 addHelp(app.get());
362 }
363
364 return proceed;
365 }
#define XSTR(s)
static bool parse1()
Definition Config.cpp:397
static char ** argv
Definition Config.h:133
static std::string configDirectory
Definition Config.h:139
static CLI::Option * groupNameOpt
Definition Config.h:146
static CLI::Option * quietOpt
Definition Config.h:150
static int argc
Definition Config.h:132
static std::string pidDirectory
Definition Config.h:141
static CLI::Option * enforceLogFileOpt
Definition Config.h:147
static std::string logDirectory
Definition Config.h:140
static CLI::Option * userNameOpt
Definition Config.h:145

References CLI::CallForWriteConfig::CallForWriteConfig().

Here is the call graph for this function:

◆ operator=()

Config & utils::Config::operator= ( const Config )
delete

◆ parse1()

bool utils::Config::parse1 ( )
staticprivate

Definition at line 397 of file Config.cpp.

397 {
398 bool proceed = true;
399
400 try {
401 app->parse(argc, argv);
402 } catch (const CLI::ParseError&) {
403 // Do not process ParseError here but on second parse pass
404 }
405
406 if ((*app)["--kill"]->count() > 0) {
407 try {
408 const pid_t daemonPid = utils::Daemon::stopDaemon(pidDirectory + "/" + applicationName + ".pid");
409 std::cout << "Daemon terminated: Pid = " << daemonPid << std::endl;
410 } catch (const DaemonError& e) {
411 std::cout << "DaemonError: " << e.what() << std::endl;
412 } catch (const DaemonFailure& e) {
413 std::cout << "DaemonFailure: " << e.what() << std::endl;
414 }
415
416 proceed = false;
417 } else {
418 if ((*app)["--show-config"]->count() == 0 && (*app)["--write-config"]->count() == 0 && (*app)["--command-line"]->count() == 0) {
419 app->allow_extras(false);
420 }
421
422 if (!quietOpt->as<bool>()) {
425 }
426
428 }
429
430 return proceed;
431 }
static void setVerboseLevel(int level)
Definition Logger.cpp:121
static void setLogLevel(int level)
Definition Logger.cpp:84
static void setQuiet(bool quiet=true)
Definition Logger.cpp:136
static pid_t stopDaemon(const std::string &pidFileName)
Definition Daemon.cpp:158

References logger::Logger::setLogLevel(), logger::Logger::setQuiet(), logger::Logger::setVerboseLevel(), and utils::Daemon::stopDaemon().

Here is the call graph for this function:

◆ parse2()

bool utils::Config::parse2 ( )
staticprivate

Definition at line 584 of file Config.cpp.

584 {
585 bool success = false;
586
587 try {
588 try {
589 app->parse(argc, argv);
590 success = true;
591 } catch (const DaemonError& e) {
592 std::cout << "Daemon error: " << e.what() << " ... exiting" << std::endl;
593 } catch (const DaemonFailure& e) {
594 std::cout << "Daemon failure: " << e.what() << " ... exiting" << std::endl;
595 } catch (const DaemonSignaled& e) {
596 std::cout << "Pid: " << getpid() << ", child pid: " << e.getPid() << ": " << e.what() << std::endl;
597 } catch (const CLI::CallForHelp&) {
598 const std::string helpMode = helpApp->get_option("--help")->as<std::string>();
599 const CLI::App* helpApp = nullptr;
600 CLI::AppFormatMode mode = CLI::AppFormatMode::Normal;
601 if (helpMode == "exact") {
603 } else if (helpMode == "expanded") {
605 mode = CLI::AppFormatMode::All;
606 }
607 std::cout << app->help(helpApp, "", mode) << std::endl;
608 } catch (const CLI::CallForVersion&) {
609 std::cout << app->version() << std::endl << std::endl;
610 } catch (const CLI::CallForCommandline& e) {
611 std::cout << e.what() << std::endl;
612 std::cout << std::endl
613 << Color::Code::FG_GREEN << "command@line" << Color::Code::FG_DEFAULT << ":" << Color::Code::FG_BLUE << "~/> "
615 << std::endl;
616 } catch (const CLI::CallForShowConfig& e) {
617 try {
618 std::cout << e.getApp()->config_to_str(true, true);
619 } catch (const CLI::ParseError& e1) {
620 std::cout << "Error showing config file: " << e.getApp() << " " << e1.get_name() << " " << e1.what() << std::endl;
621 throw;
622 }
623 } catch (const CLI::CallForWriteConfig& e) {
624 std::cout << e.what() << std::endl;
625 std::ofstream confFile(e.getConfigFile());
626 if (confFile.is_open()) {
627 try {
628 confFile << app->config_to_str(true, true);
629 confFile.close();
630 } catch (const CLI::ParseError& e1) {
631 confFile.close();
632 std::cout << "Error writing config file: " << e1.get_name() << " " << e1.what() << std::endl;
633 throw;
634 }
635 } else {
636 std::cout << "Error writing config file: " << std::strerror(errno) << std::endl;
637 }
638 } catch (const CLI::ConversionError& e) {
639 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
640 throw;
641 } catch (const CLI::ArgumentMismatch& e) {
642 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
643 throw;
644 } catch (const CLI::ConfigError& e) {
645 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
646 std::cout << " Adding '-w' on the command line may solve this problem" << std::endl;
647 throw;
648 } catch (const CLI::ParseError& e) {
649 const std::string what = e.what();
650 if (what.find("[Option Group: ") != std::string::npos) { // If CLI11 throws that error it means for us there are
651 // unconfigured anonymous instances
652 std::cout << Color::Code::FG_RED << "[BootstrapError]" << Color::Code::FG_DEFAULT
653 << " Anonymous instance(s) not configured in source code " << std::endl;
654 } else {
655 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << what << std::endl;
656 }
657 throw;
658 }
659 } catch ([[maybe_unused]] const CLI::ParseError& e) {
660 std::cout << std::endl << "Append -h or --help to your command line for more information." << std::endl;
661 } catch (const CLI::Error& e) {
662 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
663 std::cout << "Append -h or --help to your command line for more information." << std::endl;
664 }
665
666 return success;
667 }
CLI::App * getApp() const
CLI::App * getApp() const
std::string getConfigFile() const
static void createCommandLineTemplate(std::stringstream &out, CLI::App *app, CLI::CallForCommandline::Mode mode)
Definition Config.cpp:549

References utils::createCommandLineTemplate(), Color::FG_BLUE, Color::FG_DEFAULT, Color::FG_GREEN, Color::FG_RED, CLI::CallForCommandline::getApp(), CLI::CallForShowConfig::getApp(), CLI::CallForWriteConfig::getConfigFile(), CLI::CallForCommandline::getMode(), and utils::DaemonSignaled::getPid().

Here is the call graph for this function:

◆ removeInstance()

bool utils::Config::removeInstance ( CLI::App *  instance)
static

Definition at line 878 of file Config.cpp.

878 {
879 Config::required(instance, false);
880
881 return app->remove_subcommand(instance);
882 }

◆ required()

void utils::Config::required ( CLI::App *  instance,
bool  required = true 
)
static

Definition at line 827 of file Config.cpp.

827 {
828 if (required) {
829 app->needs(instance);
830
831 for (const auto& sub : instance->get_subcommands([](const CLI::App* sc) -> bool {
832 return sc->get_required();
833 })) {
834 instance->needs(sub);
835 }
836 } else {
837 app->remove_needs(instance);
838
839 for (const auto& sub : instance->get_subcommands([](const CLI::App* sc) -> bool {
840 return sc->get_required();
841 })) {
842 instance->remove_needs(sub);
843 }
844 }
845
846 instance->required(required);
847 instance->ignore_case(required);
848 }

◆ terminate()

void utils::Config::terminate ( )
static

Definition at line 669 of file Config.cpp.

669 {
670 if ((*app)["--daemonize"]->as<bool>()) {
671 std::ifstream pidFile(pidDirectory + "/" + applicationName + ".pid", std::ifstream::in);
672
673 if (pidFile.good()) {
674 pid_t pid = 0;
675 pidFile >> pid;
676
677 if (getpid() == pid) {
679 }
680 }
681 } else if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) >= 0) {
682 char buf[1024];
683 while (read(STDIN_FILENO, buf, 1024) > 0) {
684 }
685 }
686 }
static void erasePidFile(const std::string &pidFileName)
Definition Daemon.cpp:200
ssize_t read(int fd, void *buf, std::size_t count)
Definition unistd.cpp:57

References utils::Daemon::erasePidFile().

Here is the call graph for this function:

Member Data Documentation

◆ aliases

std::map< std::string, std::string > utils::Config::aliases
staticprivate

Definition at line 154 of file Config.h.

◆ app

std::shared_ptr< CLI::App > utils::Config::app = makeApp()
static

Definition at line 123 of file Config.h.

◆ applicationName

std::string utils::Config::applicationName
staticprivate

Definition at line 137 of file Config.h.

◆ applicationOptions

std::map< std::string, CLI::Option * > utils::Config::applicationOptions
staticprivate

Definition at line 155 of file Config.h.

◆ argc

int utils::Config::argc = 0
static

Definition at line 132 of file Config.h.

◆ argv

char ** utils::Config::argv = nullptr
static

Definition at line 133 of file Config.h.

◆ configDirectory

std::string utils::Config::configDirectory
staticprivate

Definition at line 139 of file Config.h.

◆ daemonizeOpt

CLI::Option * utils::Config::daemonizeOpt = nullptr
staticprivate

Definition at line 143 of file Config.h.

◆ enforceLogFileOpt

CLI::Option * utils::Config::enforceLogFileOpt = nullptr
staticprivate

Definition at line 147 of file Config.h.

◆ groupNameOpt

CLI::Option * utils::Config::groupNameOpt = nullptr
staticprivate

Definition at line 146 of file Config.h.

◆ helpApp

CLI::App * utils::Config::helpApp = nullptr
staticprivate

Definition at line 152 of file Config.h.

◆ logDirectory

std::string utils::Config::logDirectory
staticprivate

Definition at line 140 of file Config.h.

◆ logFileOpt

CLI::Option * utils::Config::logFileOpt = nullptr
staticprivate

Definition at line 144 of file Config.h.

◆ logLevelOpt

CLI::Option * utils::Config::logLevelOpt = nullptr
staticprivate

Definition at line 148 of file Config.h.

◆ pidDirectory

std::string utils::Config::pidDirectory
staticprivate

Definition at line 141 of file Config.h.

◆ quietOpt

CLI::Option * utils::Config::quietOpt = nullptr
staticprivate

Definition at line 150 of file Config.h.

◆ sectionFormatter

std::shared_ptr< CLI::Formatter > utils::Config::sectionFormatter = makeSectionFormatter()
staticprivate

Definition at line 129 of file Config.h.

◆ subParse

bool utils::Config::subParse
staticprivate

Definition at line 136 of file Config.h.

◆ userNameOpt

CLI::Option * utils::Config::userNameOpt = nullptr
staticprivate

Definition at line 145 of file Config.h.

◆ verboseLevelOpt

CLI::Option * utils::Config::verboseLevelOpt = nullptr
staticprivate

Definition at line 149 of file Config.h.


The documentation for this class was generated from the following files: