aboutsummaryrefslogtreecommitdiff
path: root/src/wandac.cpp
blob: e2da2a16cf2b40746fd2fd551897c9746718f449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "command.hpp"
#include "commander.hpp"
#include "environment.hpp"
#include "xdg.hpp"

#include <asio.hpp>
#include <clara.hpp>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>

#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <memory>

struct cli
{
    std::string command{};
    bool help{};

    clara::Parser parser;

    auto parse(int argc, char const *const *argv, std::ostream &error)
    {
        parser = clara::Arg{command, "command"}("The command to send to the deamon").required() |
                 clara::Help(help);

        auto result = parser.parse(clara::Args{argc, argv});

        if (!result)
        {
            error << "Error while processing command line arguments: "
                  << result.errorMessage()
                  << '\n'
                  << parser
                  << '\n';
            return false;
        }
        else if (command.empty())
        {
            error << "Missing required argument 'command'\n"
                  << parser
                  << '\n';
            return false;
        }

        return true;
    }
};

struct listener : wanda::commander::listener
{
    listener(::cli &cli, std::shared_ptr<spdlog::logger> logger)
        : m_logger{logger},
          m_cli{cli}
    {
    }

    void on_connected(wanda::commander &commander) override
    {
        if (m_cli.command == "change")
        {
            commander.send({wanda::command_id::change});
        }
    }

  private:
    std::shared_ptr<spdlog::logger> m_logger;
    ::cli &m_cli;
};

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;
    }

    auto log = spdlog::stdout_color_mt("wandac");
    auto interface = wanda::xdg_path_for(wanda::xdg_directory::runtime_dir, wanda::environment{}) / ".wanda_interface";
    auto service = asio::io_service{};
    auto listener = ::listener{cli, log};

    auto commander = wanda::commander{service, interface, listener, log};

    log->info("trying to connect to wanda control interface on '{}'", interface.native());
    commander.start();

    service.run();
}