diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-07-07 13:48:12 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-07-07 13:52:13 +0200 |
| commit | 0aef8955e6a67849d9559d2bae82a1ed2c2694ba (patch) | |
| tree | 2589c850dae265d65f830291f60bbc6d39e1881e /mullvad_cli/src/cmds | |
| parent | ce4cb31d0ce12f81dd2c5ed32be75725e7ee7072 (diff) | |
| download | mullvadvpn-0aef8955e6a67849d9559d2bae82a1ed2c2694ba.tar.xz mullvadvpn-0aef8955e6a67849d9559d2bae82a1ed2c2694ba.zip | |
Clean up CLI code with better subcommand structure
Diffstat (limited to 'mullvad_cli/src/cmds')
| -rw-r--r-- | mullvad_cli/src/cmds/account.rs | 56 | ||||
| -rw-r--r-- | mullvad_cli/src/cmds/mod.rs | 17 |
2 files changed, 73 insertions, 0 deletions
diff --git a/mullvad_cli/src/cmds/account.rs b/mullvad_cli/src/cmds/account.rs new file mode 100644 index 0000000000..bb4d75055f --- /dev/null +++ b/mullvad_cli/src/cmds/account.rs @@ -0,0 +1,56 @@ +use Command; +use Result; +use clap; +use rpc; +use serde_json; + +pub struct Account; + +impl Command for Account { + fn name(&self) -> &'static str { + "account" + } + + fn clap_subcommand(&self) -> clap::App<'static, 'static> { + clap::SubCommand::with_name(self.name()) + .about("Control and display information about your Mullvad account") + .setting(clap::AppSettings::SubcommandRequired) + .subcommand(clap::SubCommand::with_name("set") + .about("Change account") + .arg(clap::Arg::with_name("token") + .help("The Mullvad account token to configure the client with") + .required(true))) + .subcommand(clap::SubCommand::with_name("get") + .about("Display information about the currently configured account")) + } + + fn run(&self, matches: &clap::ArgMatches) -> Result<()> { + if let Some(set_matches) = matches.subcommand_matches("set") { + self.set(set_matches) + } else if let Some(_matches) = matches.subcommand_matches("get") { + self.get() + } else { + unreachable!("No account command given"); + } + } +} + +impl Account { + fn set(&self, matches: &clap::ArgMatches) -> Result<()> { + let token = value_t_or_exit!(matches.value_of("token"), String); + rpc::call("set_account", &[&token]).map( + |_| { + println!("Mullvad account {} set", token); + }, + ) + } + + fn get(&self) -> Result<()> { + match rpc::call("get_account", &[] as &[u8; 0])? { + serde_json::Value::String(token) => println!("Mullvad account: {:?}", token), + serde_json::Value::Null => println!("No account configured"), + _ => bail!("Unable to fetch account token"), + } + Ok(()) + } +} diff --git a/mullvad_cli/src/cmds/mod.rs b/mullvad_cli/src/cmds/mod.rs new file mode 100644 index 0000000000..3f881bcfb8 --- /dev/null +++ b/mullvad_cli/src/cmds/mod.rs @@ -0,0 +1,17 @@ +use Command; +use std::collections::HashMap; + +mod account; +pub use self::account::*; + +/// Returns a map of all available subcommands with their name as key. +pub fn get_commands() -> HashMap<&'static str, Box<Command>> { + let commands = vec![Box::new(Account) as Box<Command>]; + let mut map = HashMap::new(); + for cmd in commands { + if let Some(_) = map.insert(cmd.name(), cmd) { + panic!("Multiple commands with the same name"); + } + } + map +} |
