summaryrefslogtreecommitdiffhomepage
path: root/mullvad-daemon/src/cli.rs
blob: 573236d797f23fd21d75605f1db64d9d1fffa19d (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
use clap::{App, Arg};
use log;

use version;

pub struct Config {
    pub log_level: log::LevelFilter,
    pub log_to_file: bool,
    pub log_stdout_timestamps: bool,
    pub run_as_service: bool,
    pub register_service: bool,
}

pub fn get_config() -> Config {
    let app = create_app();
    let matches = app.get_matches();

    let log_level = match matches.occurrences_of("v") {
        0 => log::LevelFilter::Info,
        1 => log::LevelFilter::Debug,
        _ => log::LevelFilter::Trace,
    };
    let log_to_file = !matches.is_present("disable_log_to_file");
    let log_stdout_timestamps = !matches.is_present("disable_stdout_timestamps");

    let run_as_service = cfg!(windows) && matches.is_present("run_as_service");
    let register_service = cfg!(windows) && matches.is_present("register_service");

    Config {
        log_level,
        log_to_file,
        log_stdout_timestamps,
        run_as_service,
        register_service,
    }
}

fn create_app() -> App<'static, 'static> {
    let app = App::new(crate_name!())
        .version(version::CURRENT)
        .author(crate_authors!())
        .about(crate_description!())
        .arg(
            Arg::with_name("v")
                .short("v")
                .multiple(true)
                .help("Sets the level of verbosity."),
        )
        .arg(
            Arg::with_name("disable_log_to_file")
                .long("disable-log-to-file")
                .help("Disable logging to file"),
        )
        .arg(
            Arg::with_name("disable_stdout_timestamps")
                .long("disable-stdout-timestamps")
                .help("Don't log timestamps when logging to stdout, useful when running as a systemd service")
            );

    if cfg!(windows) {
        app.arg(
            Arg::with_name("run_as_service")
                .long("run-as-service")
                .help("Run as a system service. On Windows this option must be used when running a system service"),
        ).arg(
            Arg::with_name("register_service")
                .long("register-service")
                .help("Register itself as a system service"),
        )
    } else {
        app
    }
}