summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src/cmds/patch.rs
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2024-01-10 20:20:42 +0100
committerDavid Lönnhager <david.l@mullvad.net>2024-01-11 13:03:19 +0100
commitca0ad9e17da84a88621a307516eb92cad2189fa4 (patch)
tree16c0837a600b935cc520e4348588db3ca0213fd0 /mullvad-cli/src/cmds/patch.rs
parent6c754c3a45fdb93bcb6f9e615c00e70aaa8d90ba (diff)
downloadmullvadvpn-ca0ad9e17da84a88621a307516eb92cad2189fa4.tar.xz
mullvadvpn-ca0ad9e17da84a88621a307516eb92cad2189fa4.zip
Simplify CLI patch module slightly
Diffstat (limited to 'mullvad-cli/src/cmds/patch.rs')
-rw-r--r--mullvad-cli/src/cmds/patch.rs44
1 files changed, 19 insertions, 25 deletions
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)
-}