summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2024-01-11 13:59:59 +0100
committerDavid Lönnhager <david.l@mullvad.net>2024-01-11 13:59:59 +0100
commit27ed9a273d106fcd75da855bfade7d04812dd042 (patch)
treee394a256f2723403767b6b423a79c1d3d3040bb9 /mullvad-cli
parent6c754c3a45fdb93bcb6f9e615c00e70aaa8d90ba (diff)
parentf5d0fa728f0dea84db9c31f04e58f0be95541c75 (diff)
downloadmullvadvpn-27ed9a273d106fcd75da855bfade7d04812dd042.tar.xz
mullvadvpn-27ed9a273d106fcd75da855bfade7d04812dd042.zip
Merge branch 'cli-tiny-improvement'
Diffstat (limited to 'mullvad-cli')
-rw-r--r--mullvad-cli/Cargo.toml2
-rw-r--r--mullvad-cli/src/cmds/patch.rs44
2 files changed, 20 insertions, 26 deletions
diff --git a/mullvad-cli/Cargo.toml b/mullvad-cli/Cargo.toml
index fab9ac6156..d0453c85f4 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" }
diff --git a/mullvad-cli/src/cmds/patch.rs b/mullvad-cli/src/cmds/patch.rs
index bec686e56d..2aa48f2b2f 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)
-}