summaryrefslogtreecommitdiffhomepage
path: root/mullvad_cli/src/cmds
diff options
context:
space:
mode:
Diffstat (limited to 'mullvad_cli/src/cmds')
-rw-r--r--mullvad_cli/src/cmds/account.rs56
-rw-r--r--mullvad_cli/src/cmds/mod.rs17
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
+}