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 bool removeInstance (CLI::App *instance)
 
static void required (CLI::App *instance, bool reqired=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 Private Member Functions

static bool parse1 ()
 
static bool parse2 ()
 

Static Private Attributes

static std::shared_ptr< CLI::App > app = makeApp()
 
static std::shared_ptr< CLI::Formatter > sectionFormatter = makeSectionFormatter()
 
static int argc = 0
 
static char ** argv = nullptr
 
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 std::map< std::string, std::string > aliases
 
static std::map< std::string, CLI::Option * > applicationOptions
 

Detailed Description

Definition at line 39 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 927 of file Config.cpp.

932 {
933 app->add_flag(name, variable, description) //
934 ->required(required) //
935 ->configurable(configurable) //
936 ->group(groupName);
937 }
static std::shared_ptr< CLI::App > app
Definition Config.h:103
static void required(CLI::App *instance, bool reqired=true)
Definition Config.cpp:804

◆ addHelp()

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

Definition at line 739 of file Config.cpp.

739 {
740 app //
741 ->set_help_flag();
742
743 app //
744 ->add_flag(
745 "-h{standard},--help{standard}",
746 [app]([[maybe_unused]] std::int64_t count) {
747 const std::size_t disabledCount =
748 app->get_subcommands([](CLI::App* app) -> bool {
749 return app->get_group() == "Instance" && app->get_option("--disabled")->as<bool>();
750 })
751 .size();
752 const std::size_t enabledCount =
753 app->get_subcommands([](CLI::App* app) -> bool {
754 return app->get_group() == "Instance" && !app->get_option("--disabled")->as<bool>();
755 })
756 .size();
757
758 for (auto* instance : app->get_subcommands({})) {
759 const std::string& group = instance->get_group();
760 if (group == "Instance") {
761 if (instance->get_option("--disabled")->as<bool>()) {
762 instance->group(std::string("Instance").append((disabledCount > 1) ? "s" : "").append(" (disabled)"));
763 } else {
764 instance->group(std::string("Instance").append((enabledCount > 1) ? "s" : ""));
765 }
766 }
767 }
768
769 const std::string& result = app->get_option("--help")->as<std::string>();
770
771 if (result == "standard") {
772 throw CLI::CallForHelp();
773 }
774 if (result == "expanded") {
775 throw CLI::CallForAllHelp();
776 }
777 },
778 "Print help message")
779 ->configurable(false)
780 ->check(CLI::IsMember({"standard", "expanded"}))
781 ->trigger_on_parse();
782
783 return app;
784 }

◆ addInstance()

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

Definition at line 652 of file Config.cpp.

652 {
653 CLI::App* instance = app->add_subcommand(name, description) //
654 ->group(group)
655 ->fallthrough()
656 ->formatter(sectionFormatter)
657 ->configurable(false)
658 ->allow_extras(false)
659 ->disabled(name.empty());
660
661 instance //
662 ->option_defaults()
663 ->configurable(!instance->get_disabled());
664
665 if (!instance->get_disabled()) {
666 if (aliases.contains(name)) {
667 instance //
668 ->alias(aliases[name]);
669 }
670 }
671
672 return instance;
673 }
static std::shared_ptr< CLI::Formatter > sectionFormatter
Definition Config.h:104
static std::map< std::string, std::string > aliases
Definition Config.h:124

◆ addSimpleHelp()

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

Definition at line 786 of file Config.cpp.

786 {
787 app //
788 ->set_help_flag();
789
790 app //
791 ->add_flag(
792 "-h,--help",
793 []([[maybe_unused]] std::int64_t count) {
794 throw CLI::CallForHelp();
795 },
796 "Print help message")
797 ->configurable(false)
798 ->disable_flag_override()
799 ->trigger_on_parse();
800
801 return app;
802 }

◆ addStandardFlags()

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

Definition at line 675 of file Config.cpp.

675 {
676 app //
677 ->add_flag_callback(
678 "-s,--show-config",
679 [app]() {
681 },
682 "Show current configuration and exit") //
683 ->configurable(false)
684 ->disable_flag_override();
685
686 app //
687 ->add_flag(
688 "--command-line{standard}",
689 [app]([[maybe_unused]] std::int64_t count) {
690 const std::string& result = app->get_option("--command-line")->as<std::string>();
691 if (result == "standard") {
693 app,
694 "Below is a command line viewing all non-default and required options:\n"
695 "* Options show their configured value\n"
696 "* Required but not yet configured options show <REQUIRED> as value\n"
697 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
699 }
700 if (result == "required") {
702 app,
703 "Below is a command line viewing required options only:\n"
704 "* Options show either their configured or default value\n"
705 "* Required but not yet configured options show <REQUIRED> as value\n"
706 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
708 }
709 if (result == "full") {
711 app,
712 "Below is a command line viewing the full set of options with their default or configured values:\n"
713 "* Options show either their configured or default value\n"
714 "* Required but not yet configured options show <REQUIRED> as value\n"
715 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
717 }
718 if (result == "default") {
720 app,
721 "Below is a command line viewing the full set of options with their default values\n"
722 "* Options show their default value\n"
723 "* Required but not yet configured options show <REQUIRED> as value\n"
724 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
726 }
727 },
728 "Print a command line\n"
729 " standard (default): View all non-default and required options\n"
730 " required: View required options only\n"
731 " full: View the full set of options with their default or configured values\n"
732 " default: View the full set of options with their default values")
733 ->configurable(false)
734 ->check(CLI::IsMember({"standard", "required", "full", "default"}));
735
736 return app;
737 }

References 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 862 of file Config.cpp.

862 {
863 applicationOptions[name] = app //
864 ->add_option(name, description)
865 ->type_name(typeName)
866 ->configurable()
867 ->required()
868 ->group("Application Options");
869
870 app->needs(applicationOptions[name]);
871
872 return applicationOptions[name];
873 }
static std::map< std::string, CLI::Option * > applicationOptions
Definition Config.h:125

◆ 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 876 of file Config.cpp.

876 {
877 addStringOption(name, description, typeName);
878 return applicationOptions[name] //
879 ->configurable(configurable);
880 }
static CLI::Option * addStringOption(const std::string &name, const std::string &description, const std::string &typeName)
Definition Config.cpp:862

◆ 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 907 of file Config.cpp.

910 {
911 return addStringOption(name, description, typeName, std::string(defaultValue));
912 }

◆ 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 914 of file Config.cpp.

915 {
916 return addStringOption(name, description, typeName, std::string(defaultValue), configurable);
917 }

◆ 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 882 of file Config.cpp.

885 {
886 addStringOption(name, description, typeName);
887
888 applicationOptions[name] //
889 ->required(false)
890 ->default_val(defaultValue);
891
892 app->remove_needs(applicationOptions[name]);
893
894 return applicationOptions[name];
895 }

◆ 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 897 of file Config.cpp.

901 {
902 addStringOption(name, description, typeName, defaultValue);
903 return applicationOptions[name] //
904 ->configurable(configurable);
905 }

◆ bootstrap()

bool utils::Config::bootstrap ( )
static

Definition at line 343 of file Config.cpp.

343 {
344 aliases.clear();
345
346 app->final_callback([]() {
347 if (daemonizeOpt->as<bool>() && (*app)["--show-config"]->count() == 0 && (*app)["--write-config"]->count() == 0 &&
348 (*app)["--command-line"]->count() == 0) {
349 std::cout << "Running as daemon (double fork)" << std::endl;
350
351 utils::Daemon::startDaemon(
352 pidDirectory + "/" + applicationName + ".pid", userNameOpt->as<std::string>(), groupNameOpt->as<std::string>());
353
354 logger::Logger::setQuiet();
355
356 const std::string logFile = logFileOpt->as<std::string>();
357 if (!logFile.empty()) {
358 logger::Logger::logToFile(logFile);
359 }
360 } else if ((*app)["--enforce-log-file"]->as<bool>()) {
361 const std::string logFile = logFileOpt->as<std::string>();
362 if (!logFile.empty()) {
363 std::cout << "Writing logs to file " << logFile << std::endl;
364
366 }
367 }
368 });
369
370 return parse2();
371 }
static void logToFile(const std::string &logFile)
Definition Logger.cpp:105
static bool parse2()
Definition Config.cpp:532
static CLI::Option * logFileOpt
Definition Config.h:116
static CLI::Option * daemonizeOpt
Definition Config.h:115

References parse2().

Here is the call graph for this function:

◆ disabled()

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

Definition at line 826 of file Config.cpp.

826 {
827 if (disabled) {
828 app->remove_needs(instance);
829
830 for (const auto& sub : instance->get_subcommands([](const CLI::App* sc) -> bool {
831 return !sc->get_disabled();
832 })) {
833 sub->disabled();
834
835 if (sub->get_required()) {
836 instance->remove_needs(sub);
837 }
838 }
839 } else {
840 app->needs(instance);
841
842 for (const auto& sub : instance->get_subcommands([](const CLI::App* sc) -> bool {
843 return sc->get_disabled();
844 })) {
845 sub->disabled(false);
846
847 if (sub->get_required()) {
848 instance->needs(sub);
849 }
850 }
851 }
852
853 instance->required(!disabled);
854 }
static void disabled(CLI::App *instance, bool disabled=true)
Definition Config.cpp:826

◆ getApplicationName()

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

Definition at line 939 of file Config.cpp.

939 {
940 return applicationName;
941 }
static std::string applicationName
Definition Config.h:109

◆ getLogLevel()

int utils::Config::getLogLevel ( )
static

Definition at line 943 of file Config.cpp.

943 {
944 return logLevelOpt->as<int>();
945 }
static CLI::Option * logLevelOpt
Definition Config.h:120

◆ getStringOptionValue()

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

Definition at line 919 of file Config.cpp.

919 {
920 if (app->get_option(name) == nullptr) {
921 throw CLI::OptionNotFound(name);
922 }
923
924 return (*app)[name]->as<std::string>();
925 }

◆ getVerboseLevel()

int utils::Config::getVerboseLevel ( )
static

Definition at line 947 of file Config.cpp.

947 {
948 return verboseLevelOpt->as<int>();
949 }
static CLI::Option * verboseLevelOpt
Definition Config.h:121

◆ init()

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

Definition at line 106 of file Config.cpp.

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

References argc, argv, and parse1().

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 373 of file Config.cpp.

373 {
374 bool proceed = true;
375
376 try {
377 app->parse(argc, argv);
378 } catch (const CLI::ParseError&) {
379 // Do not process ParseError here but on second parse pass
380 }
381
382 if ((*app)["--kill"]->count() > 0) {
383 try {
384 const pid_t daemonPid = utils::Daemon::stopDaemon(pidDirectory + "/" + applicationName + ".pid");
385 std::cout << "Daemon terminated: Pid = " << daemonPid << std::endl;
386 } catch (const DaemonError& e) {
387 std::cout << "DaemonError: " << e.what() << std::endl;
388 } catch (const DaemonFailure& e) {
389 std::cout << "DaemonFailure: " << e.what() << std::endl;
390 }
391
392 proceed = false;
393 } else {
394 if ((*app)["--show-config"]->count() == 0 && (*app)["--write-config"]->count() == 0 && (*app)["--command-line"]->count() == 0) {
395 app->allow_extras(false);
396 }
397
398 if (!quietOpt->as<bool>()) {
401 }
402
404 }
405
406 return proceed;
407 }
static void setVerboseLevel(int level)
Definition Logger.cpp:99
static void setLogLevel(int level)
Definition Logger.cpp:62
static void setQuiet(bool quiet=true)
Definition Logger.cpp:114
static pid_t stopDaemon(const std::string &pidFileName)
Definition Daemon.cpp:136

References app.

Referenced by init().

Here is the caller graph for this function:

◆ parse2()

bool utils::Config::parse2 ( )
staticprivate

Definition at line 532 of file Config.cpp.

532 {
533 bool success = false;
534
535 try {
536 try {
537 app->parse(argc, argv);
538 success = true;
539 } catch (const DaemonError& e) {
540 std::cout << "Daemon error: " << e.what() << " ... exiting" << std::endl;
541 } catch (const DaemonFailure& e) {
542 std::cout << "Daemon failure: " << e.what() << " ... exiting" << std::endl;
543 } catch (const DaemonSignaled& e) {
544 std::cout << "Pid: " << getpid() << ", child pid: " << e.getPid() << ": " << e.what() << std::endl;
545 } catch (const CLI::CallForHelp&) {
546 std::cout << app->help() << std::endl;
547 } catch (const CLI::CallForAllHelp&) {
548 std::cout << app->help("", CLI::AppFormatMode::All) << std::endl;
549 } catch (const CLI::CallForVersion&) {
550 std::cout << app->version() << std::endl << std::endl;
551 } catch (const CLI::CallForCommandline& e) {
552 std::cout << e.what() << std::endl;
553 std::cout << std::endl
554 << Color::Code::FG_GREEN << "command@line" << Color::Code::FG_DEFAULT << ":" << Color::Code::FG_BLUE << "~/> "
556 << std::endl;
557 } catch (const CLI::CallForShowConfig& e) {
558 try {
559 std::cout << e.getApp()->config_to_str(true, true);
560 } catch (const CLI::ParseError& e1) {
561 std::cout << "Error showing config file: " << e.getApp() << " " << e1.get_name() << " " << e1.what() << std::endl;
562 throw;
563 }
564 } catch (const CLI::CallForWriteConfig& e) {
565 std::cout << e.what() << std::endl;
566 std::ofstream confFile(e.getConfigFile());
567 if (confFile.is_open()) {
568 try {
569 confFile << app->config_to_str(true, true);
570 confFile.close();
571 } catch (const CLI::ParseError& e1) {
572 confFile.close();
573 std::cout << "Error writing config file: " << e1.get_name() << " " << e1.what() << std::endl;
574 throw;
575 }
576 } else {
577 std::cout << "Error writing config file: " << std::strerror(errno) << std::endl;
578 }
579 } catch (const CLI::ConversionError& e) {
580 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
581 throw;
582 } catch (const CLI::ArgumentMismatch& e) {
583 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
584 throw;
585 } catch (const CLI::ConfigError& e) {
586 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
587 std::cout << " Adding '-w' on the command line may solve this problem" << std::endl;
588 throw;
589 } catch (const CLI::ParseError& e) {
590 const std::string what = e.what();
591 if (what.find("[Option Group: ") != std::string::npos) { // If CLI11 throws that error it means for us there are
592 // unconfigured anonymous instances
593 std::cout << Color::Code::FG_RED << "[BootstrapError]" << Color::Code::FG_DEFAULT
594 << " Anonymous instance(s) not configured in source code " << std::endl;
595 } else {
596 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << what << std::endl;
597 }
598 throw;
599 }
600 } catch (const CLI::ParseError&) {
601 std::cout << std::endl << "Append -h or --help to your command line for more information." << std::endl;
602 } catch (const CLI::Error& e) {
603 std::cout << "Error: " << e.get_name() << " " << e.what() << std::endl;
604 std::cout << "Append -h or --help to your command line for more information." << std::endl;
605 }
606
607 if (!success) {
609 }
610
611 return success;
612 }
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:497

References CLI::CallForShowConfig::getApp(), and logger::Logger::setQuiet().

Referenced by bootstrap().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeInstance()

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

Definition at line 856 of file Config.cpp.

856 {
857 Config::required(instance, false);
858
859 return app->remove_subcommand(instance);
860 }

References required().

Here is the call graph for this function:

◆ required()

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

Definition at line 804 of file Config.cpp.

804 {
805 if (reqired) {
806 app->needs(instance);
807
808 for (const auto& sub : instance->get_subcommands([](const CLI::App* sc) -> bool {
809 return sc->get_required();
810 })) {
811 instance->needs(sub);
812 }
813 } else {
814 app->remove_needs(instance);
815
816 for (const auto& sub : instance->get_subcommands([](const CLI::App* sc) -> bool {
817 return sc->get_required();
818 })) {
819 instance->remove_needs(sub);
820 }
821 }
822
823 instance->required(reqired);
824 }

Referenced by removeInstance().

Here is the caller graph for this function:

◆ terminate()

void utils::Config::terminate ( )
static

Definition at line 614 of file Config.cpp.

614 {
615 if ((*app)["--daemonize"]->as<bool>()) {
616 std::ifstream pidFile(pidDirectory + "/" + applicationName + ".pid", std::ifstream::in);
617
618 if (pidFile.good()) {
619 pid_t pid = 0;
620 pidFile >> pid;
621
622 if (getpid() == pid) {
624 }
625 }
626 } else if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) >= 0) {
627 char buf[1024];
628 while (read(STDIN_FILENO, buf, 1024) > 0) {
629 }
630 }
631 }
static void erasePidFile(const std::string &pidFileName)
Definition Daemon.cpp:178
ssize_t read(int fd, void *buf, std::size_t count)
Definition unistd.cpp:35

References app.

Member Data Documentation

◆ aliases

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

Definition at line 124 of file Config.h.

◆ app

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

Definition at line 103 of file Config.h.

Referenced by parse1(), and terminate().

◆ applicationName

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

Definition at line 109 of file Config.h.

◆ applicationOptions

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

Definition at line 125 of file Config.h.

◆ argc

int utils::Config::argc = 0
staticprivate

Definition at line 106 of file Config.h.

Referenced by init().

◆ argv

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

Definition at line 107 of file Config.h.

Referenced by init().

◆ configDirectory

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

Definition at line 111 of file Config.h.

◆ daemonizeOpt

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

Definition at line 115 of file Config.h.

◆ enforceLogFileOpt

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

Definition at line 119 of file Config.h.

◆ groupNameOpt

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

Definition at line 118 of file Config.h.

◆ logDirectory

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

Definition at line 112 of file Config.h.

◆ logFileOpt

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

Definition at line 116 of file Config.h.

◆ logLevelOpt

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

Definition at line 120 of file Config.h.

◆ pidDirectory

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

Definition at line 113 of file Config.h.

◆ quietOpt

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

Definition at line 122 of file Config.h.

◆ sectionFormatter

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

Definition at line 104 of file Config.h.

◆ userNameOpt

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

Definition at line 117 of file Config.h.

◆ verboseLevelOpt

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

Definition at line 121 of file Config.h.


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