read config under the home directory or /etc/mqtt2LoRaWAN directory. Alternatively, a config file can also be specified via command line.

This commit is contained in:
Dominik Kuhn 2022-10-04 19:48:45 +01:00
parent 6ef0fdae55
commit a7b057abd1

View File

@ -347,7 +347,7 @@ int main(int argc, char *argv[])
("version,v", "print the version number") ("version,v", "print the version number")
("hostname,h", po::value<string>()->default_value("localhost"), "Hostname") ("hostname,h", po::value<string>()->default_value("localhost"), "Hostname")
("port,p", po::value<int>()->default_value(1883), "Port") ("port,p", po::value<int>()->default_value(1883), "Port")
("config,c", po::value<std::string>()->default_value("default.conf"), "configuration file") ("config,c", po::value<std::string>(), "configuration file")
("magic,m", po::value<magic_number>(), "magic value (in NNN-NNN format)") ("magic,m", po::value<magic_number>(), "magic value (in NNN-NNN format)")
("appeui", po::value<appeui>(), "APPEUI") ("appeui", po::value<appeui>(), "APPEUI")
("deveui", po::value<deveui>(), "DEVEUI") ("deveui", po::value<deveui>(), "DEVEUI")
@ -364,7 +364,23 @@ int main(int argc, char *argv[])
po::store(po::parse_command_line(argc, argv, command_line_options), variable_map); po::store(po::parse_command_line(argc, argv, command_line_options), variable_map);
po::notify(variable_map); po::notify(variable_map);
auto config_file = variable_map.at("config").as<std::string>(); std::string config_file;
if( variable_map.count("config") ){
config_file = variable_map.at("config").as<std::string>();
}else if( vm_env.count("home") ){
if(std::filesystem::exists( vm_env["home"].as<std::string>() + "/.config/mqtt2LoRaWAN/default.conf" )){
config_file = vm_env["home"].as<std::string>() + "/.config/mqtt2LoRaWAN/default.conf";
}else if(std::filesystem::exists("/etc/mqtt2LoRaWAN/default.conf")){
config_file = "/etc/mqtt2LoRaWAN/default.conf";
}
}else{
if(std::filesystem::exists("/etc/mqtt2LoRaWAN/default.conf")){
config_file = "/etc/mqtt2LoRaWAN/default.conf";
}
}
std::ifstream ifs(config_file.c_str()); std::ifstream ifs(config_file.c_str());
if (!ifs) { if (!ifs) {