mirror of
https://gitee.com/openharmony/third_party_rust_which-rs
synced 2024-11-23 07:29:42 +00:00
init commit
This commit is contained in:
commit
b5d073162f
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
target
|
||||
Cargo.lock
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "which"
|
||||
version = "0.1.0"
|
||||
authors = ["fangyuanziti <tiziyuanfang@gmail.com>"]
|
||||
repository = "https://github.com/fangyuanziti/which-rs.git"
|
||||
documentation = "http://fangyuanziti.github.io/which-rs/which/"
|
||||
license = "MIT"
|
||||
description= "A Rust equivalent of Unix command \"which\"."
|
19
LICENSE.txt
Normal file
19
LICENSE.txt
Normal file
@ -0,0 +1,19 @@
|
||||
Copyright (c) 2015 fangyuanziti
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
19
README.md
Normal file
19
README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# which
|
||||
|
||||
A Rust equivalent of Unix command "which".
|
||||
|
||||
## Exmaple
|
||||
|
||||
To find wihch rustc exectable binary is using.
|
||||
|
||||
``` rust
|
||||
use which::which;
|
||||
|
||||
let result = which::which("rustc").unwrap();
|
||||
assert_eq!(result, PathBuf::from("/usr/bin/rustc"));
|
||||
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
The documentation is [available online](http://fangyuanziti.github.io/which-rs/which/).
|
83
src/lib.rs
Normal file
83
src/lib.rs
Normal file
@ -0,0 +1,83 @@
|
||||
//! which
|
||||
//!
|
||||
//! A Rust equivalent of Unix command "which".
|
||||
//! # Exmaple:
|
||||
//!
|
||||
//! To find wihch rustc exectable binary is using.
|
||||
//!
|
||||
//! ``` norun
|
||||
//! use which::which;
|
||||
//!
|
||||
//! let result = which::which("rustc").unwrap();
|
||||
//! assert_eq!(result, PathBuf::from("/usr/bin/rustc"));
|
||||
//!
|
||||
//! ```
|
||||
|
||||
use std::path::PathBuf;
|
||||
use std::{env, fs};
|
||||
|
||||
fn is_exist(bin_path: &PathBuf) -> bool {
|
||||
|
||||
match fs::metadata(bin_path).map(|metadata|{
|
||||
metadata.is_file()
|
||||
}) {
|
||||
Ok(true) => true,
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Find a exectable binary's path by name.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ``` norun
|
||||
/// use which::which;
|
||||
/// use std::path::PathBuf;
|
||||
///
|
||||
/// let result = which::which("rustc").unwrap();
|
||||
/// assert_eq!(result, PathBuf::from("/usr/bin/rustc"));
|
||||
///
|
||||
/// ```
|
||||
pub fn which(binary_name: &'static str)
|
||||
-> Result<PathBuf, &'static str> {
|
||||
|
||||
let path_buf = env::var_os("PATH").and_then(
|
||||
|paths| -> Option<PathBuf> {
|
||||
for path in env::split_paths(&paths) {
|
||||
let bin_path = path.join(binary_name);
|
||||
if is_exist(&bin_path) {
|
||||
return Some(bin_path);
|
||||
}
|
||||
}
|
||||
return None;
|
||||
|
||||
});
|
||||
|
||||
match path_buf {
|
||||
Some(path) => Ok(path),
|
||||
None => Err("Can not find binary path")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
use std::process::Command;
|
||||
let result = which("rustc");
|
||||
assert!(result.is_ok());
|
||||
|
||||
let which_result = Command::new("which")
|
||||
.arg("rustc")
|
||||
.output();
|
||||
|
||||
assert_eq!(String::from(result.unwrap().to_str().unwrap()),
|
||||
String::from_utf8(which_result.unwrap().stdout).unwrap().trim());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dont_works() {
|
||||
let result = which("cargo-no-exist");
|
||||
assert_eq!(result, Err("Can not find binary path"))
|
||||
}
|
Loading…
Reference in New Issue
Block a user