Skip to content

Commit

Permalink
Fix bug where writing file did not work from cli, add text options fo…
Browse files Browse the repository at this point in the history
…r write and append, bump to 0.3.2
  • Loading branch information
chipsenkbeil committed Aug 2, 2021
1 parent 3c68bb3 commit d1e342f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
edition = "2018"
homepage = "https://github.com/chipsenkbeil/distant"
Expand Down
29 changes: 19 additions & 10 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/subcommand/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 13 additions & 5 deletions src/subcommand/listen/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit d1e342f

Please sign in to comment.