From ca0ad9e17da84a88621a307516eb92cad2189fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20L=C3=B6nnhager?= <david.l@mullvad.net> Date: Wed, 10 Jan 2024 20:20:42 +0100 Subject: [PATCH 1/2] Simplify CLI patch module slightly --- mullvad-cli/src/cmds/patch.rs | 44 +++++++++++++++-------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/mullvad-cli/src/cmds/patch.rs b/mullvad-cli/src/cmds/patch.rs index bec686e56db4..2aa48f2b2fb9 100644 --- a/mullvad-cli/src/cmds/patch.rs +++ b/mullvad-cli/src/cmds/patch.rs @@ -2,15 +2,23 @@ use anyhow::{Context, Result}; use mullvad_management_interface::MullvadProxyClient; use std::{ fs::File, - io::{stdin, BufReader, Read}, + io::{read_to_string, stdin, BufReader}, }; -/// If source is specified, read from the provided file and send it as a settings patch to the -/// daemon. Otherwise, read the patch from standard input. +/// Read a settings patch and send it to the daemon for validation and +/// application. +/// +/// * If `source` is "-", read the patch from standard input +/// * Otherwise, interpret `source` as a filepath and read from the provided +/// file pub async fn import(source: String) -> Result<()> { - let json_blob = tokio::task::spawn_blocking(|| get_blob(source)) - .await - .unwrap()?; + let json_blob = tokio::task::spawn_blocking(move || match source.as_str() { + "-" => read_to_string(BufReader::new(stdin())).context("Failed to read from stdin"), + _ => read_to_string(File::open(&source)?) + .context(format!("Failed to read from path: {source}")), + }) + .await + .unwrap()?; let mut rpc = MullvadProxyClient::new().await?; rpc.apply_json_settings(json_blob) @@ -22,8 +30,11 @@ pub async fn import(source: String) -> Result<()> { Ok(()) } -/// If source is specified, write a patch to the file. Otherwise, write the patch to standard -/// output. +/// Output a settings patch including all currently patchable settings. +/// +/// * If `source` is "-", write the patch to standard output +/// * Otherwise, interpret `source` as a filepath and write to the provided +/// file pub async fn export(dest: String) -> Result<()> { let mut rpc = MullvadProxyClient::new().await?; let blob = rpc @@ -41,20 +52,3 @@ pub async fn export(dest: String) -> Result<()> { .context(format!("Failed to write to path {dest}")), } } - -fn get_blob(source: String) -> Result<String> { - match source.as_str() { - "-" => { - read_settings_from_reader(BufReader::new(stdin())).context("Failed to read from stdin") - } - _ => read_settings_from_reader(File::open(&source)?) - .context(format!("Failed to read from path: {source}")), - } -} - -/// Read until EOF or until newline when the last pair of braces has been closed -fn read_settings_from_reader(mut reader: impl Read) -> Result<String> { - let mut s = String::new(); - reader.read_to_string(&mut s)?; - Ok(s) -} From f5d0fa728f0dea84db9c31f04e58f0be95541c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20L=C3=B6nnhager?= <david.l@mullvad.net> Date: Thu, 11 Jan 2024 10:43:57 +0100 Subject: [PATCH 2/2] Add missing tokio feature to mullvad-cli --- mullvad-cli/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mullvad-cli/Cargo.toml b/mullvad-cli/Cargo.toml index fab9ac615601..d0453c85f4d8 100644 --- a/mullvad-cli/Cargo.toml +++ b/mullvad-cli/Cargo.toml @@ -26,7 +26,7 @@ mullvad-version = { path = "../mullvad-version" } talpid-types = { path = "../talpid-types" } mullvad-management-interface = { path = "../mullvad-management-interface" } -tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } +tokio = { workspace = true, features = ["macros", "rt-multi-thread", "fs"] } [target.'cfg(all(unix, not(target_os = "android")))'.dependencies] clap_complete = { version = "4.2.1" }