config.cc 1.78 KB
Newer Older
1
2
#include "config.h"

Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
3
#include "../deimos_exception.h"
4
5
6
7

#include <fstream>
#include <unistd.h>

Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
8
namespace deimos {
9
10
11
12
13
14
15
16
17
18
namespace util {

cli_options Config::parse(int argc, char* argv[])
{
    define_options();
    try {
        m_parser.parse(argc, argv);
    }
    catch (const CLI::ParseError& e) {
        m_parser.exit(e);
Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
19
        throw DeimosException(
20
21
22
23
24
25
26
            "Returning after command line parsing. --help or invalid argument(s)");
    }
    return handle_arguments();
}

void Config::define_options()
{
27
28
29
30
31
32
33
    m_parser.require_subcommand(1, 1);
    m_start = m_parser.add_subcommand("start", "start the server daemon");
    m_stop  = m_parser.add_subcommand("stop", "stop the server daemon");

    m_start
        ->add_option(
            "hostname,--hostname", m_hostname, "IP/Hostname to bind to")
34
        ->required();
35
    m_start->add_option(
36
        "--http", m_http_port, "port to listen on with HTTP protocol");
37
    m_start->add_option(
38
        "--http2", m_http2_port, "port to listen on with HTTP2 protocol");
39
    m_start->add_option(
40
41
        "-t,--threads", m_thread_count, "number of threads to listen on");

Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
42
43
44
    m_start->add_option(
        "-l,--logfile", m_log_filename, "name of file to write logs to");

45
    m_parser.set_config("--config");
46
    m_start->add_option(
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        "-w,--write-config", m_config_filename,
        "write configuration options to file");
}

cli_options Config::handle_arguments()
{
    if (m_thread_count <= 0) {
        m_thread_count = sysconf(_SC_NPROCESSORS_ONLN);
    }

    if (!m_config_filename.empty()) {
        std::ofstream file(m_config_filename);
        file << m_parser.config_to_str();
        file.close();
    }

Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
63
64
    return {m_http_port, m_http2_port, m_hostname, m_thread_count,
            m_log_filename};
65
66
67
}

}  // namespace util
Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
68
}  // namespace deimos