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

pub struct AutoConnect;

#[mullvad_management_interface::async_trait]
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"),
            )
    }

    async 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").await
        } else if let Some(_matches) = matches.subcommand_matches("get") {
            self.get().await
        } else {
            unreachable!("No auto-connect command given");
        }
    }
}

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

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