led-manager: add CLI11 option and config loading
To facilitate localized testing of JSON config loading, add an option
that allows the configuration to be directly passed, rather than try to
determine it from entity-manager config off dbus.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I80d7051bf312044c68f44bcdb9ac02364f9abac3
diff --git a/manager/json-config.hpp b/manager/json-config.hpp
index 4a07175..1e63bc4 100644
--- a/manager/json-config.hpp
+++ b/manager/json-config.hpp
@@ -222,5 +222,32 @@
/** DBusHandler class handles the D-Bus operations */
utils::DBusHandler dBusHandler;
};
+
+/** Blocking call to find the JSON Config from DBus. */
+auto getJsonConfig()
+{
+ // Get a new Dbus
+ auto bus = sdbusplus::bus::new_bus();
+
+ // Get a new event loop
+ auto event = sdeventplus::Event::get_new();
+
+ // Attach the bus to sd_event to service user requests
+ bus.attach_event(event.get(), SD_EVENT_PRIORITY_IMPORTANT);
+ phosphor::led::JsonConfig jsonConfig(bus, event);
+
+ // The event loop will be terminated from inside of a function in JsonConfig
+ // after finding the configuration file
+ if (jsonConfig.getConfFile().empty())
+ {
+ event.loop();
+ }
+
+ // Detach the bus from its sd_event event loop object
+ bus.detach_event();
+
+ return jsonConfig.getConfFile();
+}
+
} // namespace led
} // namespace phosphor
diff --git a/manager/json-parser.hpp b/manager/json-parser.hpp
index a18db5d..941ea4b 100644
--- a/manager/json-parser.hpp
+++ b/manager/json-parser.hpp
@@ -149,29 +149,18 @@
/** @brief Get led map from LED groups JSON config
*
+ * @param[in] config - Path to the JSON config.
* @return LedMap - Generated an std::map of LedAction
+ *
+ * @note if config is an empty string, daemon will interrogate dbus for
+ * compatible strings.
*/
-const LedMap getSystemLedMap()
+const LedMap getSystemLedMap(fs::path config)
{
- // Get a new Dbus
- auto bus = sdbusplus::bus::new_bus();
-
- // Get a new event loop
- auto event = sdeventplus::Event::get_new();
-
- // Attach the bus to sd_event to service user requests
- bus.attach_event(event.get(), SD_EVENT_PRIORITY_IMPORTANT);
- phosphor::led::JsonConfig jsonConfig(bus, event);
-
- // The event loop will be terminated from inside of a function in JsonConfig
- // after finding the configuration file
- if (jsonConfig.getConfFile().empty())
+ if (config.empty())
{
- event.loop();
+ config = phosphor::led::getJsonConfig();
}
- // Detach the bus from its sd_event event loop object
- bus.detach_event();
-
- return loadJsonConfig(jsonConfig.getConfFile());
+ return loadJsonConfig(config);
}
diff --git a/manager/led-main.cpp b/manager/led-main.cpp
index c3b2cea..8861a64 100644
--- a/manager/led-main.cpp
+++ b/manager/led-main.cpp
@@ -14,13 +14,23 @@
#include "lamptest/lamptest.hpp"
#endif
+#include <CLI/CLI.hpp>
#include <sdeventplus/event.hpp>
#include <algorithm>
#include <iostream>
-int main(void)
+int main(int argc, char** argv)
{
+ CLI::App app("phosphor-led-manager");
+
+#ifdef LED_USE_JSON
+ std::string configFile{};
+ app.add_option("-c,--config", configFile, "Path to JSON config");
+#endif
+
+ CLI11_PARSE(app, argc, argv);
+
// Get a default event loop
auto event = sdeventplus::Event::get_default();
@@ -28,7 +38,7 @@
auto& bus = phosphor::led::utils::DBusHandler::getBus();
#ifdef LED_USE_JSON
- auto systemLedMap = getSystemLedMap();
+ auto systemLedMap = getSystemLedMap(configFile);
#endif
/** @brief Group manager object */
diff --git a/meson.build b/meson.build
index 84cb8dc..3302bc1 100644
--- a/meson.build
+++ b/meson.build
@@ -64,6 +64,15 @@
)
endif
+if cpp.has_header('CLI/CLI.hpp')
+ CLI11_dep = declare_dependency()
+else
+ CLI11_dep = dependency(
+ 'CLI11',
+ fallback: [ 'CLI11', 'CLI11_dep' ],
+ )
+endif
+
# Get Cereal dependency.
cereal_dep = dependency('cereal', required: false)
has_cereal = cpp.has_header_symbol(
@@ -83,12 +92,13 @@
endif
deps = [
+ CLI11_dep,
+ cereal_dep,
+ nlohmann_json_dep,
+ phosphor_dbus_interfaces_dep,
+ phosphor_logging_dep,
sdbusplus_dep,
sdeventplus_dep,
- phosphor_logging_dep,
- phosphor_dbus_interfaces_dep,
- nlohmann_json_dep,
- cereal_dep,
]
subdir('manager')
diff --git a/subprojects/CLI11.wrap b/subprojects/CLI11.wrap
new file mode 100644
index 0000000..e695a9d
--- /dev/null
+++ b/subprojects/CLI11.wrap
@@ -0,0 +1,3 @@
+[wrap-git]
+url = https://github.com/CLIUtils/CLI11.git
+revision = HEAD