Handle optional JSON configuration files
Some JSON configuration files may be optionally defined, therefore this
needs to be handled when retrieving the file or loading it. When a JSON
configuration file is marked as optional and no file is found, an empty
filesystem path is returned.
Tested:
No change to current use of getting/loading JSON configuration files
Empty filesystem path is returned for an optional file not found
Only an existing JSON config files are loaded
Change-Id: I2b5c632df6df1f4cff5249be4d01c1376e0a3482
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/json_config.hpp b/json_config.hpp
index d25c04a..883720f 100644
--- a/json_config.hpp
+++ b/json_config.hpp
@@ -59,13 +59,15 @@
* @param[in] bus - The dbus bus object
* @param[in] appName - The phosphor-fan-presence application name
* @param[in] fileName - Application's configuration file's name
+ * @param[in] isOptional - Config file is optional, default to 'false'
*
* @return filesystem path
* The filesystem path to the configuration file to use
*/
static const fs::path getConfFile(sdbusplus::bus::bus& bus,
const std::string& appName,
- const std::string& fileName)
+ const std::string& fileName,
+ bool isOptional = false)
{
// Check override location
fs::path confFile = fs::path{confOverridePath} / appName / fileName;
@@ -102,12 +104,16 @@
confFile = fs::path{confBasePath} / appName / fileName;
}
- if (!fs::exists(confFile))
+ if (!fs::exists(confFile) && !isOptional)
{
log<level::ERR>("No JSON config file found",
entry("DEFAULT_FILE=%s", confFile.c_str()));
throw std::runtime_error("No JSON config file found");
}
+ else
+ {
+ confFile.clear();
+ }
return confFile;
}
@@ -125,7 +131,7 @@
std::ifstream file;
json jsonConf;
- if (fs::exists(confFile))
+ if (!confFile.empty() && fs::exists(confFile))
{
log<level::INFO>("Loading configuration",
entry("JSON_FILE=%s", confFile.c_str()));