Skip to content

Commit

Permalink
Fix bug in windows implementation of unload_profile function for prop…
Browse files Browse the repository at this point in the history
…er environment variable deletion
  • Loading branch information
humblepenguinn committed Jun 19, 2024
1 parent 01464b9 commit e94d20e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
49 changes: 37 additions & 12 deletions src/bin/envio/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,27 @@ pub fn load_profile(profile_name: &str) -> Result<()> {

/// Windows implementation of the load_profile function
#[cfg(target_family = "windows")]
pub fn load_profile(profile: Profile) {
pub fn load_profile(profile: Profile) -> envio::error::Result<()> {
for env in profile.envs {
Command::new("setx")
.arg(env.name)
.arg(env.value)
.spawn()
.expect("setx command failed");
let output = Command::new("setx").arg(&env.name).arg(&env.value).output();

match output {
Ok(output) => {
if !output.status.success() {
return Err(envio::error::Error::Msg(format!(
"Failed to execute setx for environment variable: {} with value: {}",
env.name, env.value
)));
}
}
Err(e) => {
return Err(envio::error::Error::Msg(format!("{}", e)));
}
}
}

println!("Reload your shell to apply changes");
Ok(())
}

/// Unload the environment variables of the profile from the current session
Expand All @@ -477,17 +488,31 @@ pub fn unload_profile() -> Result<()> {

/// Windows implementation of the unload_profile function
#[cfg(target_family = "windows")]
pub fn unload_profile(profile: Profile) {
pub fn unload_profile(profile: Profile) -> Result<()> {
for env in profile.envs.keys() {
Command::new("REG")
let status = Command::new("REG")
.arg("delete")
.arg("HKCU\\Environment")
.arg("/F")
.arg("/V")
.arg(format!("\"{}\"", env))
.arg("")
.spawn()
.expect("setx command failed");
.arg(&env)
.status();

match status {
Ok(status) => {
if !status.success() {
return Err(Error::Msg(format!(
"Failed to delete environment variable: {}",
env
)));
}
}
Err(e) => {
return Err(Error::Msg(format!("{}", e)));
}
}
}
println!("Reload your shell to apply changes");

Ok(())
}
8 changes: 6 additions & 2 deletions src/bin/envio/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ impl Command {
let profile = load_profile!(profile_name, get_userkey)?;
check_expired_envs(&profile);

cli::load_profile(profile);
if let Err(e) = cli::load_profile(profile) {
return Err(e);
}
}
}

Expand All @@ -430,7 +432,9 @@ impl Command {
let profile = load_profile!(profile_name, get_userkey)?;
check_expired_envs(&profile);

cli::unload_profile(profile);
if let Err(e) = cli::unload_profile(profile) {
return Err(e);
}
}
Command::Launch {
profile_name,
Expand Down

0 comments on commit e94d20e

Please sign in to comment.