Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Sep 21, 2023
1 parent 39b1d86 commit 9c531ab
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
11 changes: 3 additions & 8 deletions mullvad-cli/src/cmds/bridge.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use clap::Subcommand;
use mullvad_management_interface::MullvadProxyClient;
use mullvad_types::{
Expand Down Expand Up @@ -172,13 +172,8 @@ impl Bridge {
Self::update_bridge_settings(&mut rpc, Some(location), None, None).await
}
SetCommands::CustomList { custom_list_name } => {
let list = rpc
.get_settings()
.await?
.custom_lists
.into_iter()
.find(|list| list.name == custom_list_name)
.ok_or(anyhow!("Custom list not found"))?;
let list =
super::custom_list::find_list_by_name(&mut rpc, &custom_list_name).await?;
let location =
Constraint::Only(LocationConstraint::CustomList { list_id: list.id });
Self::update_bridge_settings(&mut rpc, Some(location), None, None).await
Expand Down
30 changes: 16 additions & 14 deletions mullvad-cli/src/cmds/custom_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use anyhow::{anyhow, Result};
use clap::Subcommand;
use mullvad_management_interface::MullvadProxyClient;
use mullvad_types::{
custom_list::CustomList as CustomListType,
relay_constraints::{Constraint, GeographicLocationConstraint},
relay_list::RelayList,
};
Expand Down Expand Up @@ -96,7 +95,7 @@ impl CustomList {
/// If the list does not exist, print an error.
async fn get(name: String) -> Result<()> {
let mut rpc = MullvadProxyClient::new().await?;
let custom_list = Self::get_list_by_name(&mut rpc, &name).await?;
let custom_list = find_list_by_name(&mut rpc, &name).await?;
let cache = rpc.get_relay_locations().await?;
Self::print_custom_list_content(&custom_list, &cache);
Ok(())
Expand All @@ -117,7 +116,7 @@ impl CustomList {

let mut rpc = MullvadProxyClient::new().await?;

let mut list = Self::get_list_by_name(&mut rpc, &name).await?;
let mut list = find_list_by_name(&mut rpc, &name).await?;
list.locations.insert(location);
rpc.update_custom_list(list).await?;

Expand All @@ -131,7 +130,7 @@ impl CustomList {

let mut rpc = MullvadProxyClient::new().await?;

let mut list = Self::get_list_by_name(&mut rpc, &name).await?;
let mut list = find_list_by_name(&mut rpc, &name).await?;
list.locations.remove(&location);
rpc.update_custom_list(list).await?;

Expand All @@ -147,22 +146,13 @@ impl CustomList {
async fn rename_list(name: String, new_name: String) -> Result<()> {
let mut rpc = MullvadProxyClient::new().await?;

let mut list = Self::get_list_by_name(&mut rpc, &name).await?;
let mut list = find_list_by_name(&mut rpc, &name).await?;
list.name = new_name;
rpc.update_custom_list(list).await?;

Ok(())
}

async fn get_list_by_name(rpc: &mut MullvadProxyClient, name: &str) -> Result<CustomListType> {
rpc.get_settings()
.await?
.custom_lists
.into_iter()
.find(|list| list.name == name)
.ok_or(anyhow!("List not found"))
}

fn print_custom_list(custom_list: &mullvad_types::custom_list::CustomList, cache: &RelayList) {
println!("{}", custom_list.name);
Self::print_custom_list_content(custom_list, cache);
Expand Down Expand Up @@ -242,3 +232,15 @@ impl<'a> std::fmt::Display for GeographicLocationConstraintFormatter<'a> {
}
}
}

pub async fn find_list_by_name(
rpc: &mut MullvadProxyClient,
name: &str,
) -> Result<mullvad_types::custom_list::CustomList> {
rpc.get_settings()
.await?
.custom_lists
.into_iter()
.find(|list| list.name == name)
.ok_or(anyhow!("List not found"))
}
14 changes: 2 additions & 12 deletions mullvad-cli/src/cmds/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,13 +516,8 @@ impl Relay {

async fn set_custom_list(custom_list_name: String) -> Result<()> {
let mut rpc = MullvadProxyClient::new().await?;
let list_id = rpc
.get_settings()
let list_id = super::custom_list::find_list_by_name(&mut rpc, &custom_list_name)
.await?
.custom_lists
.into_iter()
.find(|list| list.name == custom_list_name)
.ok_or(anyhow!("Custom list not found"))?
.id;
rpc.update_relay_settings(RelaySettingsUpdate::Normal(RelayConstraintsUpdate {
location: Some(Constraint::Only(LocationConstraint::CustomList { list_id })),
Expand Down Expand Up @@ -624,13 +619,8 @@ impl Relay {
};
}
Some(EntryLocation::CustomList { custom_list_name }) => {
let list_id = rpc
.get_settings()
let list_id = super::custom_list::find_list_by_name(&mut rpc, &custom_list_name)
.await?
.custom_lists
.into_iter()
.find(|list| list.name == custom_list_name)
.ok_or(anyhow!("Custom list not found"))?
.id;
wireguard_constraints.entry_location =
Constraint::Only(LocationConstraint::CustomList { list_id });
Expand Down
2 changes: 1 addition & 1 deletion mullvad-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ enum Cli {

/// Manage custom lists
#[clap(subcommand)]
CustomList(custom_lists::CustomList),
CustomList(custom_list::CustomList),
}

#[tokio::main]
Expand Down
16 changes: 14 additions & 2 deletions mullvad-daemon/src/custom_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ where
}

pub async fn delete_custom_list(&mut self, id: Id) -> Result<(), Error> {
let Some((list_index, _)) = self.settings.custom_lists.iter().enumerate().find(|elem| elem.1.id == id) else {
let Some((list_index, _)) = self
.settings
.custom_lists
.iter()
.enumerate()
.find(|elem| elem.1.id == id)
else {
return Err(Error::ListNotFound);
};
let settings_changed = self
Expand Down Expand Up @@ -96,7 +102,13 @@ where
}

pub async fn update_custom_list(&mut self, new_list: CustomList) -> Result<(), Error> {
let Some((list_index, old_list)) = self.settings.custom_lists.iter().enumerate().find(|elem| elem.1.id == new_list.id) else {
let Some((list_index, old_list)) = self
.settings
.custom_lists
.iter()
.enumerate()
.find(|elem| elem.1.id == new_list.id)
else {
return Err(Error::ListNotFound);
};
let id = old_list.id;
Expand Down
1 change: 0 additions & 1 deletion mullvad-daemon/src/management_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use mullvad_paths;
use mullvad_types::settings::DnsOptions;
use mullvad_types::{
account::AccountToken,
custom_list,
relay_constraints::{BridgeSettings, BridgeState, ObfuscationSettings, RelaySettingsUpdate},
relay_list::RelayList,
settings::Settings,
Expand Down

0 comments on commit 9c531ab

Please sign in to comment.