Fix bug where writing file did not work from cli, add text options for write and append, bump to 0.3.2

pull/38/head v0.3.2
Chip Senkbeil 3 years ago
parent 3c68bb3377
commit d1e342f04f
No known key found for this signature in database
GPG Key ID: 35EF1F8EC72A4131

2
Cargo.lock generated

@ -199,7 +199,7 @@ dependencies = [
[[package]]
name = "distant"
version = "0.3.1"
version = "0.3.2"
dependencies = [
"bytes",
"derive_more",

@ -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 <chip@senkbeil.org>"]
edition = "2018"
homepage = "https://github.com/chipsenkbeil/distant"

@ -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<PathBuf>,
/// Data for server-side writing of content
#[structopt(skip)]
data: Vec<u8>,
},
/// 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<PathBuf>,
/// Data for server-side writing of content
#[structopt(skip)]
data: Vec<u8>,
},
/// 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 {

@ -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)

@ -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<ResponsePayload, Box<dyn Error>
})
}
async fn file_write(path: PathBuf, data: Vec<u8>) -> Result<ResponsePayload, Box<dyn Error>> {
async fn file_write(
path: PathBuf,
data: impl AsRef<[u8]>,
) -> Result<ResponsePayload, Box<dyn Error>> {
tokio::fs::write(path, data).await?;
Ok(ResponsePayload::Ok)
}
async fn file_append(path: PathBuf, data: Vec<u8>) -> Result<ResponsePayload, Box<dyn Error>> {
async fn file_append(
path: PathBuf,
data: impl AsRef<[u8]>,
) -> Result<ResponsePayload, Box<dyn Error>> {
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)
}

Loading…
Cancel
Save