Skip to content

Commit

Permalink
Merge branch 'cli-tiny-improvement'
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Jan 11, 2024
2 parents 6c754c3 + f5d0fa7 commit 27ed9a2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion mullvad-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
44 changes: 19 additions & 25 deletions mullvad-cli/src/cmds/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit 27ed9a2

Please sign in to comment.