From d1e342f04f4e1b64c998f1800be71f6eab900e76 Mon Sep 17 00:00:00 2001 From: Chip Senkbeil Date: Sun, 1 Aug 2021 19:40:44 -0500 Subject: [PATCH] Fix bug where writing file did not work from cli, add text options for write and append, bump to 0.3.2 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/data.rs | 29 +++++++++++++++++++---------- src/subcommand/action.rs | 1 + src/subcommand/listen/handler.rs | 18 +++++++++++++----- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc80267..3c8116f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -199,7 +199,7 @@ dependencies = [ [[package]] name = "distant" -version = "0.3.1" +version = "0.3.2" dependencies = [ "bytes", "derive_more", diff --git a/Cargo.toml b/Cargo.toml index b720d21..0641479 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "distant" description = "Operate on a remote computer through file and process manipulation" categories = ["command-line-utilities"] -version = "0.3.1" +version = "0.3.2" authors = ["Chip Senkbeil "] edition = "2018" homepage = "https://github.com/chipsenkbeil/distant" diff --git a/src/data.rs b/src/data.rs index db6b065..3b3fa90 100644 --- a/src/data.rs +++ b/src/data.rs @@ -53,29 +53,38 @@ pub enum RequestPayload { /// The path to the file on the remote machine path: PathBuf, - /// Source for client-side loading of content (if not provided, stdin is used) - #[serde(skip)] - input: Option, - /// Data for server-side writing of content - #[structopt(skip)] data: Vec, }, + /// Writes a file using text instead of bytes, creating it if it does not exist, + /// and overwriting any existing content on the remote machine + FileWriteText { + /// The path to the file on the remote machine + path: PathBuf, + + /// Data for server-side writing of content + text: String, + }, + /// Appends to a file, creating it if it does not exist, on the remote machine FileAppend { /// The path to the file on the remote machine path: PathBuf, - /// Source for client-side loading of content (if not provided, stdin is used) - #[serde(skip)] - input: Option, - /// Data for server-side writing of content - #[structopt(skip)] data: Vec, }, + /// Appends text to a file, creating it if it does not exist, on the remote machine + FileAppendText { + /// The path to the file on the remote machine + path: PathBuf, + + /// Data for server-side writing of content + text: String, + }, + /// Reads a directory from the specified path on the remote machine #[structopt(visible_aliases = &["ls"])] DirRead { diff --git a/src/subcommand/action.rs b/src/subcommand/action.rs index 77b99ae..ec50d32 100644 --- a/src/subcommand/action.rs +++ b/src/subcommand/action.rs @@ -47,6 +47,7 @@ async fn run_async(cmd: ActionSubcommand, _opt: CommonOpt) -> Result<(), Error> if let Some(req) = cmd.operation.map(Request::from) { is_proc_req = req.payload.is_proc_run(); + trace!("Client sending request: {:?}", req); let res = client.send(req).await?; // Store the spawned process id for using in sending stdin (if we spawned a proc) diff --git a/src/subcommand/listen/handler.rs b/src/subcommand/listen/handler.rs index 02a7823..8abf70c 100644 --- a/src/subcommand/listen/handler.rs +++ b/src/subcommand/listen/handler.rs @@ -33,8 +33,10 @@ pub(super) async fn process( match payload { RequestPayload::FileRead { path } => file_read(path).await, RequestPayload::FileReadText { path } => file_read_text(path).await, - RequestPayload::FileWrite { path, data, .. } => file_write(path, data).await, - RequestPayload::FileAppend { path, data, .. } => file_append(path, data).await, + RequestPayload::FileWrite { path, data } => file_write(path, data).await, + RequestPayload::FileWriteText { path, text } => file_write(path, text).await, + RequestPayload::FileAppend { path, data } => file_append(path, data).await, + RequestPayload::FileAppendText { path, text } => file_append(path, text).await, RequestPayload::DirRead { path, all } => dir_read(path, all).await, RequestPayload::DirCreate { path, all } => dir_create(path, all).await, RequestPayload::Remove { path, force } => remove(path, force).await, @@ -80,17 +82,23 @@ async fn file_read_text(path: PathBuf) -> Result }) } -async fn file_write(path: PathBuf, data: Vec) -> Result> { +async fn file_write( + path: PathBuf, + data: impl AsRef<[u8]>, +) -> Result> { tokio::fs::write(path, data).await?; Ok(ResponsePayload::Ok) } -async fn file_append(path: PathBuf, data: Vec) -> Result> { +async fn file_append( + path: PathBuf, + data: impl AsRef<[u8]>, +) -> Result> { let mut file = tokio::fs::OpenOptions::new() .append(true) .open(path) .await?; - file.write_all(&data).await?; + file.write_all(data.as_ref()).await?; Ok(ResponsePayload::Ok) }