Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed file redirection relative to current working directory #13

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion rust/bitbazaar/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ mod tests {
default_stdout_global_logging(tracing::Level::DEBUG).unwrap();
}

// Temp file:
fn tf() -> String {
// Using debug formatting to make sure escaped properly on windows:
format!("{:?}", NamedTempFile::new().unwrap().path())
}

static GLOB_TD: Lazy<tempfile::TempDir> = Lazy::new(|| tempfile::tempdir().unwrap());

static HOME_DIR: Lazy<String> = Lazy::new(|| {
homedir::get_my_home()
.unwrap()
Expand Down Expand Up @@ -129,7 +132,7 @@ mod tests {
// <-- redirection:
// Write:
#[case::redir_1(format!("echo foo > {fp} && rm {fp}", fp=tf()), "", 0, None, None, true)]
#[case::redir_2(format!("echo foo > {fp} && {CAT_CMD} {fp} && rm {fp}", fp=tf()), "foo", 0, None, None, true)]
#[case::redir_2(format!("cd {dp:?} && echo foo > file.txt && {CAT_CMD} file.txt", dp=GLOB_TD.path()), "foo", 0, None, None, true)]
// Write and append together:
#[case::redir_3(
format!("echo foo >> {fp} && echo bar > {fp} && echo ree >> {fp} && {CAT_CMD} {fp} && rm {fp}", fp=tf()),
Expand Down
23 changes: 16 additions & 7 deletions rust/bitbazaar/cli/redirect.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
io::{Read, Write},
path::PathBuf,
process,
};

Expand All @@ -20,23 +21,23 @@ pub fn handle_redirect(
Ok(match redirect {
ast::Redirect::Write(fd, name) => {
let dest = Target::new(shell, name)?.set_write();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::Append(fd, name) => {
let dest = Target::new(shell, name)?.set_write().set_append();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::DupWrite(fd, name) => {
let dest = Target::new(shell, name)?.set_write();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::Read(fd, name) => {
let dest = Target::new(shell, name)?.set_read();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::DupRead(fd, name) => {
let dest = Target::new(shell, name)?.set_read();
Data::new(last_out, fd)?.submit(dest)?
Data::new(last_out, fd)?.submit(shell, dest)?
}
ast::Redirect::ReadWrite(..) => {
return Err(err!(
Expand Down Expand Up @@ -203,7 +204,7 @@ impl Data {
})
}

fn submit(self, dest: Target) -> Result<RunnerCmdOut, ShellErr> {
fn submit(self, shell: &Shell, dest: Target) -> Result<RunnerCmdOut, ShellErr> {
let mut conc = ConcreteOutput::default();

match dest.variant {
Expand Down Expand Up @@ -243,7 +244,15 @@ impl Data {
opts.append(true);
}

let mut file = opts.open(name).change_context(ShellErr::InternalError)?;
// Make sure relative to current dir if it is a relative path:
let mut filepath = PathBuf::from(name);
if filepath.is_relative() {
filepath = shell.active_dir()?.join(filepath)
}

let mut file = opts
.open(filepath)
.change_context(ShellErr::InternalError)?;

if dest.write {
self.write(&mut file)?;
Expand Down
Loading