From d76a244a1a3b69954da9cbe9098ebb612ca20027 Mon Sep 17 00:00:00 2001 From: Cody Duong Date: Wed, 5 Mar 2025 16:34:29 -0600 Subject: [PATCH] fix: use `;` as delimiter on windows for `_PR_LIB` (github #37) Co-authored-by: iff --- core/src/shell.rs | 11 ++++++++++- modules.md | 10 +++++++++- utils/src/files.rs | 6 +++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/core/src/shell.rs b/core/src/shell.rs index 9ccc19e..6925f21 100644 --- a/core/src/shell.rs +++ b/core/src/shell.rs @@ -1,5 +1,7 @@ use pay_respects_utils::evals::split_command; use pay_respects_utils::files::get_path_files; +use pay_respects_utils::files::path_env_sep; + use std::process::{exit, Stdio}; use std::collections::HashMap; @@ -7,6 +9,9 @@ use std::sync::mpsc::channel; use std::thread; use std::time::Duration; +#[cfg(windows)] +use pay_respects_utils::files::path_convert; + pub const PRIVILEGE_LIST: [&str; 2] = ["sudo", "doas"]; #[derive(PartialEq)] @@ -112,8 +117,12 @@ impl Data { } } - let path = lib_dir.split(':').collect::>(); + let path = lib_dir.split(path_env_sep()).collect::>(); + for p in path { + #[cfg(windows)] + let p = path_convert(p); + let files = match std::fs::read_dir(p) { Ok(files) => files, Err(_) => continue, diff --git a/modules.md b/modules.md index b9aa6f1..574103c 100644 --- a/modules.md +++ b/modules.md @@ -51,7 +51,7 @@ Expose your module as executable (`chmod u+x`) in `PATH`, and done! ## `LIB` directories -If exposing modules in `PATH` annoys you, you can set the `_PR_LIB` environment variable to specify directories to find the modules, separated by `:` (analogous to `PATH`): +If exposing modules in `PATH` annoys you, you can set the `_PR_LIB` environment variable to specify directories to find the modules, separated by `:` or `;` (analogous to `PATH`): Example in a [FHS 3.0 compliant system](https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch04s06.html): ```shell @@ -60,6 +60,14 @@ export _DEF_PR_LIB="/usr/lib" # runtime export _PR_LIB="/usr/lib:$HOME/.local/share" ``` +Example in Windows/DOS compliant systems +```pwsh +$env:_PR_LIB = @( + (Join-Path $env:APPDATA "pay-respects" "modules"), + (Join-Path $env:USERPROFILE ".cargo" "bin") +) -join ';' +``` + This is not the default as there is no general way to know its value and depends on distribution (`/usr/lib`, `/usr/libexec`, or NixOS which isn't FHS compliant at all). System programs usually have a hard-coded path looking for `lib`. If you are a package maintainer for a distribution, setting this value when compiling, so it fits into your distribution standard. If you installed the module with `cargo install`, the binary will be placed in `bin` subdirectory under Cargo's home which should be in the `PATH` anyway. Cargo has no option to place in subdirectories with other names. diff --git a/utils/src/files.rs b/utils/src/files.rs index c9c0ada..0f207ab 100644 --- a/utils/src/files.rs +++ b/utils/src/files.rs @@ -123,7 +123,7 @@ fn path_env() -> String { } #[cfg(windows)] -fn path_env_sep() -> &'static str { +pub fn path_env_sep() -> &'static str { if is_msystem() { ":" } else { @@ -132,7 +132,7 @@ fn path_env_sep() -> &'static str { } #[cfg(windows)] -fn path_convert(path: &str) -> String { +pub fn path_convert(path: &str) -> String { if is_msystem() { msys2_conv_path(path).expect("Failed to convert path for msys") } else { @@ -164,6 +164,6 @@ fn path_env() -> String { } #[cfg(not(windows))] -fn path_env_sep() -> &'static str { +pub fn path_env_sep() -> &'static str { ":" }