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

fix: cannot detect vendor if tool-name and cmd-name differ #45

Merged
merged 1 commit into from
Mar 24, 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
8 changes: 4 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ macro_rules! os_vendors {
($($os:literal => $($tool:literal),+);+$(;)?) => {
pub fn detect_vendor() -> std::result::Result<$crate::Vendor, $crate::UptError> {
let os = crate::utils::detect_os().unwrap_or_default();
let tools: Vec<&str> = match os.as_str() {
let pairs: Vec<(&str, &str)> = match os.as_str() {
$(
$os => vec![$($tool),+].into_iter().filter_map(|v| which_cmd(v)).collect(),
$os => vec![$($tool),+].into_iter().filter_map(|tool| which_cmd(tool).map(|bin_name| (tool, bin_name))).collect(),
)+
_ => vec!["apt", "dnf", "pacman"],
_ => ["apt", "dnf", "pacman"].into_iter().map(|tool| (tool, tool)).collect(),
};
match $crate::utils::find_tool(&tools) {
match $crate::utils::find_tool(&pairs) {
Some(tool) => $crate::vendor::init_vendor(&tool),
None => Err(UptError::NoDetectVendor),
}
Expand Down
15 changes: 8 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
use which::which;

pub fn find_tool(tools: &[&str]) -> Option<String> {
match tools.len() {
pub fn find_tool(pairs: &[(&str, &str)]) -> Option<String> {
match pairs.len() {
0 => None,
1 => {
let tool = &tools[0];
if which(tool).is_ok() {
let (tool, bin_name) = &pairs[0];
if which(bin_name).is_ok() {
Some(tool.to_string())
} else {
None
}
}
_ => {
let handles: Vec<_> = tools
let handles: Vec<_> = pairs
.iter()
.map(|tool| {
.map(|(tool, bin_name)| {
let tool = tool.to_string();
let bin_name = bin_name.to_string();
std::thread::spawn(move || {
if which(&tool).is_ok() {
if which(&bin_name).is_ok() {
Some(tool)
} else {
None
Expand Down