diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2023-09-07 14:36:23 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2023-09-07 14:36:23 +0200 |
| commit | 152c44ffb96143683a7fdaabac1c747aaf6ad0dc (patch) | |
| tree | c03c530efa97ed721b63416ca854deec25962d20 /source/app/wandac/src | |
| parent | 13cf6fe70cc68bd4d803385f5c6d7fe9c7691247 (diff) | |
| download | wanda-152c44ffb96143683a7fdaabac1c747aaf6ad0dc.tar.xz wanda-152c44ffb96143683a7fdaabac1c747aaf6ad0dc.zip | |
apps: split apps into directories
Diffstat (limited to 'source/app/wandac/src')
| -rw-r--r-- | source/app/wandac/src/main.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/source/app/wandac/src/main.cpp b/source/app/wandac/src/main.cpp new file mode 100644 index 0000000..65af1db --- /dev/null +++ b/source/app/wandac/src/main.cpp @@ -0,0 +1,93 @@ +#include <wanda/control/commander.hpp> +#include <wanda/proto/command.hpp> +#include <wanda/system/environment.hpp> +#include <wanda/system/logging.hpp> +#include <wanda/system/xdg.hpp> + +#include <boost/asio/io_context.hpp> +#include <boost/asio/post.hpp> +#include <lyra/lyra.hpp> +#include <spdlog/sinks/stdout_color_sinks.h> +#include <spdlog/spdlog.h> + +#include <cstdlib> +#include <filesystem> +#include <iostream> +#include <memory> + +struct cli +{ + std::string command{}; + bool help{}; + + lyra::cli_parser parser{}; + + auto parse(int argc, char const * const * argv, std::ostream & error) + { + parser |= // + lyra::help(help) | // + lyra::arg{command, "command"}("The command to send to the deamon").required(); + + auto result = parser.parse({argc, argv}); + + if (!result) + { + error << "Error while processing command line arguments: " << result.message() << '\n' << parser << '\n'; + return false; + } + + return true; + } +}; + +struct listener : wanda::control::commander::listener +{ + listener(::cli & cli, boost::asio::io_context & service) + : m_cli{cli} + , m_service{service} + { + } + + void on_connected(wanda::control::commander & commander) override + { + if (m_cli.command == "change") + { + commander.send(wanda::proto::make_change_command()); + + post(m_service, [&] { commander.stop(); }); + } + } + +private: + ::cli & m_cli; + boost::asio::io_context & m_service; +}; + +int main(int argc, char const * const * argv) +{ + auto cli = ::cli{}; + if (!cli.parse(argc, argv, std::cerr)) + { + return EXIT_FAILURE; + } + else if (cli.help) + { + std::cout << cli.parser << '\n'; + return EXIT_SUCCESS; + } + + wanda::system::initialize_logger(std::make_shared<spdlog::sinks::stderr_color_sink_st>()); + + auto interface = + wanda::system::xdg_path_for(wanda::system::xdg_directory::runtime_dir, wanda::system::environment{}) / + ".wanda_interface"; + auto service = boost::asio::io_context{}; + auto listener = ::listener{cli, service}; + + auto commander = wanda::control::commander{service, interface, listener}; + + wanda::system::get_logger()->info("trying to connect to wanda control interface on '{}'", interface.native()); + commander.start(); + + service.run(); +} |
