etc/
cmd.rs

1//! This module exports shortcuts of `etc`
2use crate::{Error, Etc, FileSystem, Meta};
3use std::{
4    env,
5    path::{Path, PathBuf},
6};
7
8/// unix `cp -r`
9pub fn cp_r<P>(src: P, target: P) -> Result<(), Error>
10where
11    P: AsRef<Path> + Sized,
12{
13    let mut tree = Etc::from(src).tree()?;
14    tree.load()?;
15
16    if let Some(mut children) = tree.children {
17        children.iter_mut().for_each(|t| t.redir(&target).unwrap());
18    }
19    Ok(())
20}
21
22/// unix `find <src> -name <target>`
23pub fn find_all<P>(src: P, target: &str) -> Result<Vec<PathBuf>, Error>
24where
25    P: AsRef<Path> + Sized,
26{
27    let root = Etc::from(src);
28    let mut res = vec![];
29    root.find_all(target, &mut res)?;
30    Ok(res.to_vec())
31}
32
33/// find file from upper directories till `/`
34pub fn find_up(target: &str) -> Result<PathBuf, Error> {
35    let mut cur = Etc::from(env::current_dir()?);
36    while !cur.is_root() {
37        if cur.ls()?.contains(&target.to_string()) {
38            return Ok(cur.open(target)?.into());
39        }
40        cur = cur.base()?.into();
41    }
42
43    Err(Error::Custom(format!("Could not find file {}", target)))
44}