blob: 2953982b4df914804d4d2ba39967daab67a94ab4 (
plain)
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
|
use std::fs;
use std::path::Path;
/// Refresh the shell's $PATH
///
/// This function caches all valid paths within within the directories
/// specified.
///
/// # Arguments
/// * `path` - A reference to a vector that holds a list to the shell $PATHs
///
/// # Returns
/// * `bins: Vec<String>` - A new cache of all valid file paths in $PATH
///
/// # Examples
/// ```
/// let path = vec!["/bin"];
/// let path = path.into_iter().map(Path::new).collect();
/// let mut bins = path::refresh(&path);
/// ...
/// // A situation occurs where the $PATH needs to be refreshed
/// bins = path::refresh(&path)
/// ```
pub fn refresh(path: &Vec<&Path>) -> Vec<String> {
let mut bins: Vec<String> = Vec::new();
for p in path {
let files = match fs::read_dir(p) {
Ok(files) => files,
Err(_) => continue,
};
for file in files {
bins.push(file.unwrap().path().display().to_string());
}
}
bins
}
|