-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(libscoop): added portability on non-windows
Note: although this is for unix, we are not going to add non-windows support. The reason of adding the adaptor is to support build docs on linux... Signed-off-by: Chawye Hsu <[email protected]>
- Loading branch information
Showing
3 changed files
with
97 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,83 @@ | ||
use once_cell::sync::Lazy; | ||
use std::ffi::OsString; | ||
use std::path::Path; | ||
use winreg::enums::HKEY_CURRENT_USER; | ||
use winreg::RegKey; | ||
|
||
use crate::error::Fallible; | ||
|
||
/// `HKEY_CURRENT_USER` registry key handle. | ||
static HKCU: Lazy<RegKey> = Lazy::new(|| RegKey::predef(HKEY_CURRENT_USER)); | ||
|
||
/// Get the value of an environment variable. | ||
/// Returns an empty string if the variable is not set. | ||
pub fn get(key: &str) -> Fallible<OsString> { | ||
let path = Path::new("Environment"); | ||
let env = HKCU.open_subkey(path)?; | ||
Ok(env.get_value(key)?) | ||
} | ||
#[cfg(unix)] | ||
pub use unix::{get, get_env_path_list, set}; | ||
#[cfg(windows)] | ||
pub use windows::{get, get_env_path_list, set}; | ||
|
||
#[cfg(windows)] | ||
mod windows { | ||
use once_cell::sync::Lazy; | ||
use std::ffi::OsString; | ||
use std::path::Path; | ||
use winreg::enums::HKEY_CURRENT_USER; | ||
use winreg::RegKey; | ||
|
||
use crate::error::Fallible; | ||
|
||
/// `HKEY_CURRENT_USER` registry key handle. | ||
static HKCU: Lazy<RegKey> = Lazy::new(|| RegKey::predef(HKEY_CURRENT_USER)); | ||
|
||
/// Get the value of an environment variable. | ||
/// Returns an empty string if the variable is not set. | ||
pub fn get(key: &str) -> Fallible<OsString> { | ||
let path = Path::new("Environment"); | ||
let env = HKCU.open_subkey(path)?; | ||
Ok(env.get_value(key)?) | ||
} | ||
|
||
/// Set the value of an environment variable. | ||
/// If the value is an empty string, the variable is deleted. | ||
pub fn set(key: &str, value: &str) -> Fallible<()> { | ||
let path = Path::new("Environment"); | ||
let (env, _) = HKCU.create_subkey(path)?; | ||
|
||
if value.is_empty() { | ||
// ignore error of deleting non-existent value | ||
let _ = env.delete_value(key); | ||
} else { | ||
env.set_value(key, &value)?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Set the value of an environment variable. | ||
/// If the value is an empty string, the variable is deleted. | ||
pub fn set(key: &str, value: &str) -> Fallible<()> { | ||
let path = Path::new("Environment"); | ||
let (env, _) = HKCU.create_subkey(path)?; | ||
|
||
if value.is_empty() { | ||
// ignore error of deleting non-existent value | ||
let _ = env.delete_value(key); | ||
} else { | ||
env.set_value(key, &value)?; | ||
/// Get the value of the `PATH` environment variable as a list of paths. | ||
pub fn get_env_path_list() -> Fallible<Vec<String>> { | ||
let env_path = get("PATH")?; | ||
Ok(env_path | ||
.into_string() | ||
.unwrap() | ||
.split(';') | ||
.map(|s| s.to_owned()) | ||
.collect()) | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Get the value of the `PATH` environment variable as a list of paths. | ||
pub fn get_env_path_list() -> Fallible<Vec<String>> { | ||
let env_path = get("PATH")?; | ||
Ok(env_path | ||
.into_string() | ||
.unwrap() | ||
.split(';') | ||
.map(|s| s.to_owned()) | ||
.collect()) | ||
#[cfg(unix)] | ||
mod unix { | ||
use std::ffi::OsString; | ||
|
||
use crate::error::Fallible; | ||
|
||
/// Get the value of an environment variable. | ||
/// Returns an empty string if the variable is not set. | ||
pub fn get(key: &str) -> Fallible<OsString> { | ||
Ok(std::env::var_os(key).unwrap_or_default()) | ||
} | ||
|
||
/// Set the value of an environment variable. | ||
/// If the value is an empty string, the variable is deleted. | ||
pub fn set(key: &str, value: &str) -> Fallible<()> { | ||
// no-op | ||
Ok(()) | ||
} | ||
|
||
/// Get the value of the `PATH` environment variable as a list of paths. | ||
pub fn get_env_path_list() -> Fallible<Vec<String>> { | ||
let env_path = get("PATH")?; | ||
Ok(env_path | ||
.into_string() | ||
.unwrap() | ||
.split(':') | ||
.map(|s| s.to_owned()) | ||
.collect()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters