Repository navigation
Expand file tree
/
Copy pathbinary.rs
More file actions
69 lines (61 loc) · 2.21 KB
/
Copy pathbinary.rs
File metadata and controls
69 lines (61 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// utils/binary.rs
//! # Binary Utility Module
//!
//! This module provides utility functions for locating and verifying the `stackql` binary.
//! It supports checking the binary's presence in the system `PATH` or the current directory
//! and retrieving the full path to the binary if it exists.
//!
//! ## Features
//! - Checks if the `stackql` binary is available in the system's `PATH`.
//! - Retrieves the full path of the `stackql` binary from the current directory or `PATH`.
//!
//! ## Example Usage
//! ```rust
//! use crate::utils::binary::{binary_exists_in_path, get_binary_path};
//!
//! if binary_exists_in_path() {
//! if let Some(path) = get_binary_path() {
//! println!("Found stackql binary at: {:?}", path);
//! }
//! }
//! ```
use std::env;
use std::path::PathBuf;
use std::process::Command;
/// Check if the stackql binary exists in PATH
///
/// Uses `output()` rather than `status()` so the child's stdout/stderr are
/// captured instead of inherited - otherwise `which`/`where` print the
/// resolved path straight to the terminal on every lookup.
pub fn binary_exists_in_path() -> bool {
let binary_name = super::platform::get_binary_name();
let output = if super::platform::get_platform() == super::platform::Platform::Windows {
Command::new("where").arg(&binary_name).output()
} else {
Command::new("which").arg(&binary_name).output()
};
output.map(|o| o.status.success()).unwrap_or(false)
}
/// Get the full path to the stackql binary
pub fn get_binary_path() -> Option<PathBuf> {
let binary_name = super::platform::get_binary_name();
// First check current directory
if let Ok(current_dir) = env::current_dir() {
let binary_path = current_dir.join(&binary_name);
if binary_path.exists() && binary_path.is_file() {
return Some(binary_path);
}
}
// Then check PATH
if binary_exists_in_path() {
if let Ok(paths) = env::var("PATH") {
for path in env::split_paths(&paths) {
let full_path = path.join(&binary_name);
if full_path.exists() && full_path.is_file() {
return Some(full_path);
}
}
}
}
None
}