Skip to content

Commit

Permalink
add --no-inherit-env option
Browse files Browse the repository at this point in the history
  • Loading branch information
tangowithfoxtrot committed Feb 21, 2024
1 parent f3ad7f3 commit c7d29ad
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions crates/bws/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ enum Commands {
command: Vec<String>,

Check warning on line 89 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L89

Added line #L89 was not covered by tests
#[arg(long, help = "The shell to use")]
shell: Option<String>,
#[arg(
long,
help = "Don't inherit environment variables from the current shell"
)]
no_inherit_env: bool,

Check warning on line 96 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L96

Added line #L96 was not covered by tests
#[arg(long, help = "The ID of the project to use")]
project_id: Option<Uuid>,
},
Expand Down Expand Up @@ -630,6 +635,7 @@ async fn process_commands() -> Result<()> {
Commands::Run {
command,
shell,
no_inherit_env,
project_id,

Check warning on line 639 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L636-L639

Added lines #L636 - L639 were not covered by tests
} => {
let res = if let Some(project_id) = project_id {
Expand Down Expand Up @@ -664,7 +670,7 @@ async fn process_commands() -> Result<()> {
}

Check warning on line 670 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L667-L670

Added lines #L667 - L670 were not covered by tests
}

let command = if command.is_empty() {
let user_command = if command.is_empty() {
let mut buffer = String::new();
std::io::stdin().read_to_string(&mut buffer)?;
buffer

Check warning on line 676 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L673-L676

Added lines #L673 - L676 were not covered by tests
Expand All @@ -680,14 +686,23 @@ async fn process_commands() -> Result<()> {
unreachable!();

Check warning on line 686 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L686

Added line #L686 was not covered by tests
};

let mut child = process::Command::new(&shell)
let mut command = process::Command::new(&shell);
command
.arg("-c")
.arg(&command)
.envs(&environment)
.arg(&user_command)
.stdout(process::Stdio::inherit())
.stderr(process::Stdio::inherit())
.spawn()
.expect("failed to execute process");
.stderr(process::Stdio::inherit());

if no_inherit_env {
let path = std::env::var("PATH").unwrap_or_else(|_| "/bin:/usr/bin".to_string());
command.env_clear();
command.env("PATH", path); // PATH is always necessary
command.envs(&environment);
} else {
command.envs(&environment);
}

Check warning on line 703 in crates/bws/src/main.rs

View check run for this annotation

Codecov / codecov/patch

crates/bws/src/main.rs#L689-L703

Added lines #L689 - L703 were not covered by tests

let mut child = command.spawn().expect("failed to execute process");

let exit_status = child.wait().expect("process failed to execute");
let exit_code = exit_status.code().unwrap_or(1);
Expand Down

0 comments on commit c7d29ad

Please sign in to comment.