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

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

◆ addHelp()

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

Definition at line 765 of file Config.cpp.

765 {
766 app //
767 ->set_help_flag();
768
769 app //
770 ->add_flag(
771 "-h{standard},--help{standard}",
772 [app]([[maybe_unused]] std::int64_t count) {
773 const std::size_t disabledCount =
774 app->get_subcommands([](CLI::App* app) -> bool {
775 return app->get_group() == "Instance" && app->get_option("--disabled")->as<bool>();
776 })
777 .size();
778 const std::size_t enabledCount =
779 app->get_subcommands([](CLI::App* app) -> bool {
780 return app->get_group() == "Instance" && !app->get_option("--disabled")->as<bool>();
781 })
782 .size();
783
784 for (auto* instance : app->get_subcommands({})) {
785 if (instance->get_group() == "Instance") {
786 if (instance->get_option("--disabled")->as<bool>()) {
787 instance->group(std::string("Instance").append((disabledCount > 1) ? "s" : "").append(" (disabled)"));
788 } else {
789 instance->group(std::string("Instance").append((enabledCount > 1) ? "s" : ""));
790 }
791 }
792 }
793
794 const std::string& result = app->get_option("--help")->as<std::string>();
795
796 if (result == "standard") {
797 throw CLI::CallForHelp();
798 }
799 if (result == "expanded") {
800 throw CLI::CallForAllHelp();
801 }
802 },
803 "Print help message")
804 ->configurable(false)
805 ->check(CLI::IsMember({"standard", "expanded"}));
806
807 return app;
808 }

◆ addInstance()

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

Definition at line 678 of file Config.cpp.

678 {
679 CLI::App* instance = app->add_subcommand(name, description) //
680 ->group(group)
681 ->fallthrough()
682 ->formatter(sectionFormatter)
683 ->configurable(false)
684 ->allow_extras(false)
685 ->disabled(name.empty());
686
687 instance //
688 ->option_defaults()
689 ->configurable(!instance->get_disabled());
690
691 if (!instance->get_disabled()) {
692 if (aliases.contains(name)) {
693 instance //
694 ->alias(aliases[name]);
695 }
696 }
697
698 return instance;
699 }
static std::shared_ptr< CLI::Formatter > sectionFormatter
Definition Config.h:126
static std::map< std::string, std::string > aliases
Definition Config.h:146

◆ addSimpleHelp()

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

Definition at line 810 of file Config.cpp.

810 {
811 app //
812 ->set_help_flag();
813
814 app //
815 ->add_flag(
816 "-h,--help",
817 []([[maybe_unused]] std::int64_t count) {
818 throw CLI::CallForHelp();
819 },
820 "Print help message")
821 ->configurable(false)
822 ->disable_flag_override();
823
824 return app;
825 }

◆ addStandardFlags()

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

Definition at line 701 of file Config.cpp.

701 {
702 app //
703 ->add_flag_callback(
704 "-s,--show-config",
705 [app]() {
707 },
708 "Show current configuration and exit") //
709 ->configurable(false)
710 ->disable_flag_override();
711
712 app //
713 ->add_flag(
714 "--command-line{standard}",
715 [app]([[maybe_unused]] std::int64_t count) {
716 const std::string& result = app->get_option("--command-line")->as<std::string>();
717 if (result == "standard") {
719 app,
720 "Below is a command line viewing all non-default and required options:\n"
721 "* Options show their configured value\n"
722 "* Required but not yet configured options show <REQUIRED> as value\n"
723 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
725 }
726 if (result == "required") {
728 app,
729 "Below is a command line viewing required options only:\n"
730 "* Options show either their configured or default value\n"
731 "* Required but not yet configured options show <REQUIRED> as value\n"
732 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
734 }
735 if (result == "full") {
737 app,
738 "Below is a command line viewing the full set of options with their default or configured values:\n"
739 "* Options show either their configured or default value\n"
740 "* Required but not yet configured options show <REQUIRED> as value\n"
741 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
743 }
744 if (result == "default") {
746 app,
747 "Below is a command line viewing the full set of options with their default values\n"
748 "* Options show their default value\n"
749 "* Required but not yet configured options show <REQUIRED> as value\n"
750 "* Options marked as <REQUIRED> need to be configured for a successful bootstrap",
752 }
753 },
754 "Print a command line\n"
755 " standard (default): View all non-default and required options\n"
756 " required: View required options only\n"
757 " full: View the full set of options with their default or configured values\n"
758 " default: View the full set of options with their default values")
759 ->configurable(false)
760 ->check(CLI::IsMember({"standard", "required", "full", "default"}));
761
762 return app;
763 }

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

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

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

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

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

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

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

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

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

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

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

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

◆ bootstrap()

bool utils::Config::bootstrap ( )
static

Definition at line 365 of file Config.cpp.

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

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

849 {
850 if (disabled) {
851 app->remove_needs(instance);
852
853 for (const auto& sub : instance->get_subcommands([](const CLI::App* sc) -> bool {
854 return !sc->get_disabled();
855 })) {
856 sub->disabled();
857
858 if (sub->get_required()) {
859 instance->remove_needs(sub);
860 }
861 }
862 } else {
863 app->needs(instance);
864
865 for (const auto& sub : instance->get_subcommands([](const CLI::App* sc) -> bool {
866 return sc->get_disabled();
867 })) {
868 sub->disabled(false);
869
870 if (sub->get_required()) {
871 instance->needs(sub);
872 }
873 }
874 }
875
876 instance->required(!disabled);
877 }
static void disabled(CLI::App *instance, bool disabled=true)
Definition Config.cpp:849

◆ getApplicationName()

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

Definition at line 962 of file Config.cpp.

962 {
963 return applicationName;
964 }
static std::string applicationName
Definition Config.h:131

◆ getLogLevel()

int utils::Config::getLogLevel ( )
static

Definition at line 966 of file Config.cpp.

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

◆ getStringOptionValue()

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

Definition at line 942 of file Config.cpp.

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

◆ getVerboseLevel()

int utils::Config::getVerboseLevel ( )
static

Definition at line 970 of file Config.cpp.

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

◆ init()

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

Definition at line 128 of file Config.cpp.

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

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

395 {
396 bool proceed = true;
397
398 try {
399 app->parse(argc, argv);
400 } catch (const CLI::ParseError&) {
401 // Do not process ParseError here but on second parse pass
402 }
403
404 if ((*app)["--kill"]->count() > 0) {
405 try {
406 const pid_t daemonPid = utils::Daemon::stopDaemon(pidDirectory + "/" + applicationName + ".pid");
407 std::cout << "Daemon terminated: Pid = " << daemonPid << std::endl;
408 } catch (const DaemonError& e) {
409 std::cout << "DaemonError: " << e.what() << std::endl;
410 } catch (const DaemonFailure& e) {
411 std::cout << "DaemonFailure: " << e.what() << std::endl;
412 }
413
414 proceed = false;
415 } else {
416 if ((*app)["--show-config"]->count() == 0 && (*app)["--write-config"]->count() == 0 && (*app)["--command-line"]->count() == 0) {
417 app->allow_extras(false);
418 }
419
420 if (!quietOpt->as<bool>()) {
423 }
424
426 }
427
428 return proceed;
429 }
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 utils::Daemon::stopDaemon().

Here is the call graph for this function:

◆ parse2()

bool utils::Config::parse2 ( )
staticprivate

Definition at line 558 of file Config.cpp.

558 {
559 bool success = false;
560
561 try {
562 try {
563 app->parse(argc, argv);
564 success = true;
565 } catch (const DaemonError& e) {
566 std::cout << "Daemon error: " << e.what() << " ... exiting" << std::endl;
567 } catch (const DaemonFailure& e) {
568 std::cout << "Daemon failure: " << e.what() << " ... exiting" << std::endl;
569 } catch (const DaemonSignaled& e) {
570 std::cout << "Pid: " << getpid() << ", child pid: " << e.getPid() << ": " << e.what() << std::endl;
571 } catch (const CLI::CallForHelp&) {
572 std::cout << app->help() << std::endl;
573 } catch (const CLI::CallForAllHelp&) {
574 std::cout << app->help("", CLI::AppFormatMode::All) << std::endl;
575 } catch (const CLI::CallForVersion&) {
576 std::cout << app->version() << std::endl << std::endl;
577 } catch (const CLI::CallForCommandline& e) {
578 std::cout << e.what() << std::endl;
579 std::cout << std::endl
580 << Color::Code::FG_GREEN << "command@line" << Color::Code::FG_DEFAULT << ":" << Color::Code::FG_BLUE << "~/> "
582 << std::endl;
583 } catch (const CLI::CallForShowConfig& e) {
584 try {
585 std::cout << e.getApp()->config_to_str(true, true);
586 } catch (const CLI::ParseError& e1) {
587 std::cout << "Error showing config file: " << e.getApp() << " " << e1.get_name() << " " << e1.what() << std::endl;
588 throw;
589 }
590 } catch (const CLI::CallForWriteConfig& e) {
591 std::cout << e.what() << std::endl;
592 std::ofstream confFile(e.getConfigFile());
593 if (confFile.is_open()) {
594 try {
595 confFile << app->config_to_str(true, true);
596 confFile.close();
597 } catch (const CLI::ParseError& e1) {
598 confFile.close();
599 std::cout << "Error writing config file: " << e1.get_name() << " " << e1.what() << std::endl;
600 throw;
601 }
602 } else {
603 std::cout << "Error writing config file: " << std::strerror(errno) << std::endl;
604 }
605 } catch (const CLI::ConversionError& e) {
606 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
607 throw;
608 } catch (const CLI::ArgumentMismatch& e) {
609 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
610 throw;
611 } catch (const CLI::ConfigError& e) {
612 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
613 std::cout << " Adding '-w' on the command line may solve this problem" << std::endl;
614 throw;
615 } catch (const CLI::ParseError& e) {
616 const std::string what = e.what();
617 if (what.find("[Option Group: ") != std::string::npos) { // If CLI11 throws that error it means for us there are
618 // unconfigured anonymous instances
619 std::cout << Color::Code::FG_RED << "[BootstrapError]" << Color::Code::FG_DEFAULT
620 << " Anonymous instance(s) not configured in source code " << std::endl;
621 } else {
622 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << what << std::endl;
623 }
624 throw;
625 }
626 } catch ([[maybe_unused]] const CLI::ParseError& e) {
627 std::cout << std::endl << "Append -h or --help to your command line for more information." << std::endl;
628 } catch (const CLI::Error& e) {
629 std::cout << "[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << std::endl;
630 std::cout << "Append -h or --help to your command line for more information." << std::endl;
631 }
632
633 if (!success) {
635 }
636
637 return success;
638 }
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:523

References utils::createCommandLineTemplate(), 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 879 of file Config.cpp.

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

◆ required()

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

Definition at line 827 of file Config.cpp.

827 {
828 if (reqired) {
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(reqired);
847 }

◆ terminate()

void utils::Config::terminate ( )
static

Definition at line 640 of file Config.cpp.

640 {
641 if ((*app)["--daemonize"]->as<bool>()) {
642 std::ifstream pidFile(pidDirectory + "/" + applicationName + ".pid", std::ifstream::in);
643
644 if (pidFile.good()) {
645 pid_t pid = 0;
646 pidFile >> pid;
647
648 if (getpid() == pid) {
650 }
651 }
652 } else if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) >= 0) {
653 char buf[1024];
654 while (read(STDIN_FILENO, buf, 1024) > 0) {
655 }
656 }
657 }
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 146 of file Config.h.

◆ app

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

Definition at line 125 of file Config.h.

◆ applicationName

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

Definition at line 131 of file Config.h.

◆ applicationOptions

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

Definition at line 147 of file Config.h.

◆ argc

int utils::Config::argc = 0
staticprivate

Definition at line 128 of file Config.h.

◆ argv

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

Definition at line 129 of file Config.h.

◆ configDirectory

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

Definition at line 133 of file Config.h.

◆ daemonizeOpt

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

Definition at line 137 of file Config.h.

◆ enforceLogFileOpt

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

Definition at line 141 of file Config.h.

◆ groupNameOpt

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

Definition at line 140 of file Config.h.

◆ logDirectory

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

Definition at line 134 of file Config.h.

◆ logFileOpt

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

Definition at line 138 of file Config.h.

◆ logLevelOpt

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

Definition at line 142 of file Config.h.

◆ pidDirectory

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

Definition at line 135 of file Config.h.

◆ quietOpt

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

Definition at line 144 of file Config.h.

◆ sectionFormatter

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

Definition at line 126 of file Config.h.

◆ userNameOpt

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

Definition at line 139 of file Config.h.

◆ verboseLevelOpt

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

Definition at line 143 of file Config.h.


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