summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/auto_connect.rs
blob: 385a2de7b6fd027aa2044a853d81d7682de9debf (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
use crate::{new_rpc_client, Command, Result};
use clap::value_t_or_exit;

pub struct AutoConnect;

impl Command for AutoConnect {
    fn name(&self) -> &'static str {
        "auto-connect"
    }

    fn clap_subcommand(&self) -> clap::App<'static, 'static> {
        clap::SubCommand::with_name(self.name())
            .about("Control the daemon auto-connect setting")
            .setting(clap::AppSettings::SubcommandRequiredElseHelp)
            .subcommand(
                clap::SubCommand::with_name("set")
                    .about("Change auto-connect setting")
                    .arg(
                        clap::Arg::with_name("policy")
                            .required(true)
                            .possible_values(&["on", "off"]),
                    ),
            )
            .subcommand(
                clap::SubCommand::with_name("get")
                    .about("Display the current auto-connect setting"),
            )
    }

    fn run(&self, matches: &clap::ArgMatches<'_>) -> Result<()> {
        if let Some(set_matches) = matches.subcommand_matches("set") {
            let auto_connect = value_t_or_exit!(set_matches.value_of("policy"), String);
            self.set(auto_connect == "on")
        } else if let Some(_matches) = matches.subcommand_matches("get") {
            self.get()
        } else {
            unreachable!("No auto-connect command given");
        }
    }
}

impl AutoConnect {
    fn set(&self, auto_connect: bool) -> Result<()> {
        let mut rpc = new_rpc_client()?;
        rpc.set_auto_connect(auto_connect)?;
        println!("Changed auto-connect sharing setting");
        Ok(())
    }

    fn get(&self) -> Result<()> {
        let mut rpc = new_rpc_client()?;
        let auto_connect = rpc.get_settings()?.auto_connect;
        println!("Autoconnect: {}", if auto_connect { "on" } else { "off" });
        Ok(())
    }
}