config.cc 2.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * Copyright (c) 2020, Irish Centre for High End Computing (ICHEC), NUI Galway
 * Authors:
 *     Ciarán O'Rourke <ciaran.orourke@ichec.ie>,
 *     Sophie Wenzel-Teuber <sophie.wenzel-teuber@ichec.ie>
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

11
12
#include "config.h"

Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
13
#include "../deimos_exception.h"
14
15
16
17

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

Ciarán Ó Rourke's avatar
Ciarán Ó Rourke committed
18
namespace deimos {
19
20
21
22
23
24
25
26
27
28
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
29
        throw DeimosException(
30
31
32
33
34
35
36
            "Returning after command line parsing. --help or invalid argument(s)");
    }
    return handle_arguments();
}

void Config::define_options()
{
37
38
39
40
41
42
43
    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")
44
        ->required();
45
    m_start->add_option(
46
        "--http", m_http_port, "port to listen on with HTTP protocol");
47
    m_start->add_option(
48
        "--http2", m_http2_port, "port to listen on with HTTP2 protocol");
49
    m_start->add_option(
50
51
        "-t,--threads", m_thread_count, "number of threads to listen on");

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

55
    m_parser.set_config("--config");
56
    m_start->add_option(
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        "-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();
    }

73
74
    return {
        m_http_port, m_http2_port, m_hostname, m_thread_count, m_log_filename};
75
76
77
}

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