diff options
| author | David Lönnhager <david.l@mullvad.net> | 2021-11-29 13:19:44 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2022-03-14 12:08:46 +0100 |
| commit | f412c3c3193c585e0fc842f52278030ffab47bc7 (patch) | |
| tree | 1820ef0970b5ae97ade60ba1f948147f38337460 /mullvad-cli/src | |
| parent | b2fce777fc89453a22e60b217fef8ea20735fb70 (diff) | |
| download | mullvadvpn-f412c3c3193c585e0fc842f52278030ffab47bc7.tar.xz mullvadvpn-f412c3c3193c585e0fc842f52278030ffab47bc7.zip | |
Allow devices to be revoked by name in the CLI
Diffstat (limited to 'mullvad-cli/src')
| -rw-r--r-- | mullvad-cli/src/cmds/account.rs | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/mullvad-cli/src/cmds/account.rs b/mullvad-cli/src/cmds/account.rs index ba09ed8e49..340275aebd 100644 --- a/mullvad-cli/src/cmds/account.rs +++ b/mullvad-cli/src/cmds/account.rs @@ -58,7 +58,7 @@ impl Command for Account { ) .arg( clap::Arg::new("device") - .help("ID of the device to revoke") + .help("Name or ID of the device to revoke") .required(true), ), ) @@ -162,8 +162,20 @@ impl Account { async fn revoke_device(&self, matches: &clap::ArgMatches) -> Result<()> { let mut rpc = new_rpc_client().await?; + let token = self.parse_account_else_current(&mut rpc, matches).await?; - let device_id = parse_device_id(matches); + let device_to_revoke = parse_device_name(matches); + + let device_list = rpc.list_devices(token.clone()).await?.into_inner(); + let device_id = device_list + .devices + .into_iter() + .find(|dev| { + dev.name.eq_ignore_ascii_case(&device_to_revoke) + || dev.id.eq_ignore_ascii_case(&device_to_revoke) + }) + .map(|dev| dev.id) + .ok_or_else(|| Error::CommandFailed("Device not found"))?; rpc.remove_device(types::DeviceRemoval { account_token: token, @@ -248,10 +260,14 @@ impl Account { fn parse_token_else_stdin(matches: &clap::ArgMatches) -> String { parse_from_match_else_stdin("Enter account number: ", "account", matches) + .split_whitespace() + .join("") } -fn parse_device_id(matches: &clap::ArgMatches) -> String { - parse_from_match_else_stdin("Enter device id: ", "device", matches) +fn parse_device_name(matches: &clap::ArgMatches) -> String { + parse_from_match_else_stdin("Enter device name: ", "device", matches) + .trim() + .to_string() } fn parse_from_match_else_stdin( @@ -259,7 +275,7 @@ fn parse_from_match_else_stdin( key: &'static str, matches: &clap::ArgMatches, ) -> String { - let val = match matches.value_of(key) { + match matches.value_of(key) { Some(device) => device.to_string(), None => { let mut val = String::new(); @@ -272,6 +288,5 @@ fn parse_from_match_else_stdin( .expect("Failed to read from STDIN"); val } - }; - val.split_whitespace().join("").to_string() + } } |
