diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2018-07-09 13:51:09 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2018-07-10 14:44:23 +0200 |
| commit | cabefbf5908cfa56449f85540d52d9f6e55150d1 (patch) | |
| tree | 8d8a78cb43b730f98eaf1029858c7d47ddc8dc64 /mullvad-cli | |
| parent | 1f1872d9ebc16c6b9d9e1641399a96806b350c67 (diff) | |
| download | mullvadvpn-cabefbf5908cfa56449f85540d52d9f6e55150d1.tar.xz mullvadvpn-cabefbf5908cfa56449f85540d52d9f6e55150d1.zip | |
Expose autoconnect option in CLI
Diffstat (limited to 'mullvad-cli')
| -rw-r--r-- | mullvad-cli/src/cmds/auto_connect.rs | 58 | ||||
| -rw-r--r-- | mullvad-cli/src/cmds/mod.rs | 4 |
2 files changed, 62 insertions, 0 deletions
diff --git a/mullvad-cli/src/cmds/auto_connect.rs b/mullvad-cli/src/cmds/auto_connect.rs new file mode 100644 index 0000000000..57c125c4b6 --- /dev/null +++ b/mullvad-cli/src/cmds/auto_connect.rs @@ -0,0 +1,58 @@ +use clap; +use {Command, Result}; + +use mullvad_ipc_client::DaemonRpcClient; + +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::SubcommandRequired) + .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 = DaemonRpcClient::new()?; + rpc.set_auto_connect(auto_connect)?; + println!("Changed auto-connect sharing setting"); + Ok(()) + } + + fn get(&self) -> Result<()> { + let mut rpc = DaemonRpcClient::new()?; + let auto_connect = rpc.get_auto_connect()?; + println!("Autoconnect: {}", if auto_connect { "on" } else { "off" }); + Ok(()) + } +} diff --git a/mullvad-cli/src/cmds/mod.rs b/mullvad-cli/src/cmds/mod.rs index 4e54aadbca..613a1e69d6 100644 --- a/mullvad-cli/src/cmds/mod.rs +++ b/mullvad-cli/src/cmds/mod.rs @@ -4,6 +4,9 @@ use Command; mod account; pub use self::account::Account; +mod auto_connect; +pub use self::auto_connect::AutoConnect; + mod status; pub use self::status::Status; @@ -29,6 +32,7 @@ pub use self::version::Version; pub fn get_commands() -> HashMap<&'static str, Box<Command>> { let commands: Vec<Box<Command>> = vec